<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>记事本登录注册</title>
<link rel="stylesheet" href="../css/login.css">
</head>
<body>
<div id="app">
<!-- 登录页面 -->
<div v-show="isLoginPage">
<h2>登录</h2>
<form>
<input v-model="loginUsername" placeholder="用户名" />
<input v-model="loginPassword" placeholder="密码" />
<button type="button" @click="login">登录</button>
<button type="button" @click="showRegisterPage">注册</button>
</form>
</div>
<!-- 注册页面 -->
<div v-show="isRegisterPage">
<h2>注册</h2>
<form>
<input v-model="registerUsername" placeholder="用户名" />
<input v-model="registerPassword" placeholder="密码" type="password" />
<button type="button" @click="register">注册</button>
<button type="button" @click="showLoginPage">返回登录</button>
</form>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
<script>
new Vue({
el: '#app',
data: {
isLoginPage: true,
isRegisterPage: false,
isLoggedIn: false,
loginUsername: '',
loginPassword: '',
registerUsername: '',
registerPassword: '',
registeredUsers: [], // 存储已注册用户信息的数组
usernamesSet: new Set(), // 用于确保用户名唯一性的Set
notes: [],
},
mounted() {
const storedUsers = localStorage.getItem('registeredUsers');
if (storedUsers) {
this.registeredUsers = JSON.parse(storedUsers);
this.registeredUsers.forEach(user => {
this.usernamesSet.add(user.username);
});
}
},
methods: {
login() {
const inputUsername = this.loginUsername;
const inputPassword = this.loginPassword;
const user = this.registeredUsers.find(u => u.username === inputUsername && u.password === inputPassword);
if (user) {
console.log('登录成功,跳转到指定页面');
window.location.href = '../html/index.html';
} else {
alert('用户名或密码错误,请重试!');
}
},
register() {
const usernamePattern = /^[a-zA-Z0-9]+$/;
const passwordPattern = /.{6,}/;
if (!this.registerUsername.match(usernamePattern)) {
alert('用户名只能包含字母和数字');
return;
}
if (!this.registerPassword.match(passwordPattern)) {
alert('密码长度至少为6位');
return;
}
if (this.usernamesSet.has(this.registerUsername)) {
alert('该用户名已注册,请更换用户名');
return;
}
const userId = Date.now(); // 使用时间戳作为唯一ID
const newUser = {
id: userId,
username: this.registerUsername,
password: this.registerPassword
};
this.registeredUsers.push(newUser);
this.usernamesSet.add(this.registerUsername);
localStorage.setItem('registeredUsers', JSON.stringify(this.registeredUsers));
console.log('注册成功');
console.log('已注册用户数组:', this.registeredUsers);
this.isRegisterPage = false;
this.isLoginPage = true;
},
showRegisterPage() {
this.isLoginPage = false;
this.isRegisterPage = true;
},
showLoginPage() {
this.isRegisterPage = false;
this.isLoginPage = true;
}
}
});
</script>
</body>
</html>