feat: 注册增加手机号验证和邮箱
- 手机号输入(必填,11位中国手机号格式验证) - 验证码机制(模拟,6位随机码,5分钟有效期) - 发送验证码按钮(60秒倒计时) - 验证码输入框 - 邮箱输入(可选,格式验证) - 手机号唯一性检查(防止重复注册)
This commit is contained in:
149
www/app.js
149
www/app.js
@@ -1391,7 +1391,7 @@ function renderProfilePage() {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="profile-footer">
|
<div class="profile-footer">
|
||||||
<p>AI助手 v3.3.1</p>
|
<p>AI助手 v3.4.0</p>
|
||||||
<p>基于智谱 GLM-4.5-Air</p>
|
<p>基于智谱 GLM-4.5-Air</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -1560,6 +1560,21 @@ function showRegisterPage() {
|
|||||||
<label>用户名</label>
|
<label>用户名</label>
|
||||||
<input type="text" id="registerUsername" placeholder="请输入用户名(3-20字符)" autocomplete="username">
|
<input type="text" id="registerUsername" placeholder="请输入用户名(3-20字符)" autocomplete="username">
|
||||||
</div>
|
</div>
|
||||||
|
<div class="auth-input-group">
|
||||||
|
<label>手机号 <span class="required">*</span></label>
|
||||||
|
<div class="phone-input-wrapper">
|
||||||
|
<input type="tel" id="registerPhone" placeholder="请输入手机号" maxlength="11" autocomplete="tel">
|
||||||
|
<button class="send-code-btn" id="sendCodeBtn">获取验证码</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="auth-input-group">
|
||||||
|
<label>验证码 <span class="required">*</span></label>
|
||||||
|
<input type="text" id="registerCode" placeholder="请输入验证码" maxlength="6" autocomplete="one-time-code">
|
||||||
|
</div>
|
||||||
|
<div class="auth-input-group">
|
||||||
|
<label>邮箱 <span class="optional">(可选)</span></label>
|
||||||
|
<input type="email" id="registerEmail" placeholder="请输入邮箱(可选)" autocomplete="email">
|
||||||
|
</div>
|
||||||
<div class="auth-input-group">
|
<div class="auth-input-group">
|
||||||
<label>密码</label>
|
<label>密码</label>
|
||||||
<input type="password" id="registerPassword" placeholder="请输入密码(6-20字符)" autocomplete="new-password">
|
<input type="password" id="registerPassword" placeholder="请输入密码(6-20字符)" autocomplete="new-password">
|
||||||
@@ -1600,22 +1615,139 @@ function showRegisterPage() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 发送验证码按钮
|
||||||
|
const sendCodeBtn = document.getElementById('sendCodeBtn');
|
||||||
|
if (sendCodeBtn) {
|
||||||
|
sendCodeBtn.addEventListener('click', handleSendCode);
|
||||||
|
}
|
||||||
|
|
||||||
// 回车注册
|
// 回车注册
|
||||||
document.getElementById('registerPasswordConfirm')?.addEventListener('keydown', (e) => {
|
document.getElementById('registerPasswordConfirm')?.addEventListener('keydown', (e) => {
|
||||||
if (e.key === 'Enter') handleRegister();
|
if (e.key === 'Enter') handleRegister();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 手机号输入限制(只允许数字)
|
||||||
|
const phoneInput = document.getElementById('registerPhone');
|
||||||
|
if (phoneInput) {
|
||||||
|
phoneInput.addEventListener('input', (e) => {
|
||||||
|
e.target.value = e.target.value.replace(/\D/g, '');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证码状态
|
||||||
|
let verifyCode = null;
|
||||||
|
let verifyCodePhone = null;
|
||||||
|
let verifyCodeExpire = null;
|
||||||
|
|
||||||
|
// 发送验证码(模拟)
|
||||||
|
function handleSendCode() {
|
||||||
|
const phoneInput = document.getElementById('registerPhone');
|
||||||
|
const phone = phoneInput?.value.trim();
|
||||||
|
const sendCodeBtn = document.getElementById('sendCodeBtn');
|
||||||
|
|
||||||
|
// 验证手机号格式
|
||||||
|
if (!validatePhone(phone)) {
|
||||||
|
showToast('请输入正确的手机号');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查手机号是否已注册
|
||||||
|
const users = JSON.parse(localStorage.getItem('registeredUsers') || '[]');
|
||||||
|
if (users.find(u => u.phone === phone)) {
|
||||||
|
showToast('该手机号已注册');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成6位验证码
|
||||||
|
const code = Math.floor(100000 + Math.random() * 900000).toString();
|
||||||
|
verifyCode = code;
|
||||||
|
verifyCodePhone = phone;
|
||||||
|
verifyCodeExpire = Date.now() + 5 * 60 * 1000; // 5分钟有效期
|
||||||
|
|
||||||
|
// 显示验证码(模拟,实际应发送短信)
|
||||||
|
console.log('验证码:', code); // 调试用
|
||||||
|
showToast(`验证码已发送:${code}`); // 实际项目中应隐藏
|
||||||
|
|
||||||
|
// 禁用按钮60秒
|
||||||
|
if (sendCodeBtn) {
|
||||||
|
sendCodeBtn.disabled = true;
|
||||||
|
let countdown = 60;
|
||||||
|
const timer = setInterval(() => {
|
||||||
|
countdown--;
|
||||||
|
if (countdown > 0) {
|
||||||
|
sendCodeBtn.textContent = `${countdown}s`;
|
||||||
|
} else {
|
||||||
|
clearInterval(timer);
|
||||||
|
sendCodeBtn.disabled = false;
|
||||||
|
sendCodeBtn.textContent = '获取验证码';
|
||||||
|
}
|
||||||
|
}, 1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证手机号格式
|
||||||
|
function validatePhone(phone) {
|
||||||
|
// 中国手机号:11位,以1开头
|
||||||
|
const phoneRegex = /^1[3-9]\d{9}$/;
|
||||||
|
return phoneRegex.test(phone);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证邮箱格式
|
||||||
|
function validateEmail(email) {
|
||||||
|
if (!email) return true; // 可选,空值有效
|
||||||
|
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||||
|
return emailRegex.test(email);
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleRegister() {
|
function handleRegister() {
|
||||||
const username = document.getElementById('registerUsername')?.value.trim();
|
const username = document.getElementById('registerUsername')?.value.trim();
|
||||||
|
const phone = document.getElementById('registerPhone')?.value.trim();
|
||||||
|
const code = document.getElementById('registerCode')?.value.trim();
|
||||||
|
const email = document.getElementById('registerEmail')?.value.trim();
|
||||||
const password = document.getElementById('registerPassword')?.value;
|
const password = document.getElementById('registerPassword')?.value;
|
||||||
const passwordConfirm = document.getElementById('registerPasswordConfirm')?.value;
|
const passwordConfirm = document.getElementById('registerPasswordConfirm')?.value;
|
||||||
|
|
||||||
|
// 验证用户名
|
||||||
if (!username || username.length < 3 || username.length > 20) {
|
if (!username || username.length < 3 || username.length > 20) {
|
||||||
showToast('用户名需要3-20个字符');
|
showToast('用户名需要3-20个字符');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 验证手机号
|
||||||
|
if (!validatePhone(phone)) {
|
||||||
|
showToast('请输入正确的手机号');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证验证码
|
||||||
|
if (!code) {
|
||||||
|
showToast('请输入验证码');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!verifyCode || verifyCodePhone !== phone) {
|
||||||
|
showToast('请先获取验证码');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (code !== verifyCode) {
|
||||||
|
showToast('验证码错误');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Date.now() > verifyCodeExpire) {
|
||||||
|
showToast('验证码已过期,请重新获取');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证邮箱(可选)
|
||||||
|
if (!validateEmail(email)) {
|
||||||
|
showToast('请输入正确的邮箱格式');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证密码
|
||||||
if (!password || password.length < 6 || password.length > 20) {
|
if (!password || password.length < 6 || password.length > 20) {
|
||||||
showToast('密码需要6-20个字符');
|
showToast('密码需要6-20个字符');
|
||||||
return;
|
return;
|
||||||
@@ -1633,9 +1765,17 @@ function handleRegister() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 检查手机号是否已注册
|
||||||
|
if (users.find(u => u.phone === phone)) {
|
||||||
|
showToast('该手机号已注册');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// 注册新用户
|
// 注册新用户
|
||||||
const newUser = {
|
const newUser = {
|
||||||
username,
|
username,
|
||||||
|
phone,
|
||||||
|
email: email || '',
|
||||||
password,
|
password,
|
||||||
registeredAt: Date.now()
|
registeredAt: Date.now()
|
||||||
};
|
};
|
||||||
@@ -1643,8 +1783,13 @@ function handleRegister() {
|
|||||||
users.push(newUser);
|
users.push(newUser);
|
||||||
localStorage.setItem('registeredUsers', JSON.stringify(users));
|
localStorage.setItem('registeredUsers', JSON.stringify(users));
|
||||||
|
|
||||||
|
// 清除验证码
|
||||||
|
verifyCode = null;
|
||||||
|
verifyCodePhone = null;
|
||||||
|
verifyCodeExpire = null;
|
||||||
|
|
||||||
// 自动登录
|
// 自动登录
|
||||||
currentUser = { username: newUser.username, registeredAt: newUser.registeredAt };
|
currentUser = { username: newUser.username, phone: newUser.phone, registeredAt: newUser.registeredAt };
|
||||||
saveCurrentUser();
|
saveCurrentUser();
|
||||||
|
|
||||||
showToast('注册成功');
|
showToast('注册成功');
|
||||||
|
|||||||
@@ -8,12 +8,12 @@
|
|||||||
<meta http-equiv="Pragma" content="no-cache">
|
<meta http-equiv="Pragma" content="no-cache">
|
||||||
<meta http-equiv="Expires" content="0">
|
<meta http-equiv="Expires" content="0">
|
||||||
<title>AI助手</title>
|
<title>AI助手</title>
|
||||||
<link rel="stylesheet" href="style.css?v=3.3.1">
|
<link rel="stylesheet" href="style.css?v=3.4.0">
|
||||||
<link rel="manifest" href="manifest.json">
|
<link rel="manifest" href="manifest.json">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="app"></div>
|
<div id="app"></div>
|
||||||
<script src="marked.min.js?v=3.3.1"></script>
|
<script src="marked.min.js?v=3.4.0"></script>
|
||||||
<script src="app.js?v=3.3.1"></script>
|
<script src="app.js?v=3.4.0"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
@@ -753,6 +753,47 @@ body {
|
|||||||
border-color: var(--primary);
|
border-color: var(--primary);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.auth-input-group .required {
|
||||||
|
color: #e53e3e;
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.auth-input-group .optional {
|
||||||
|
color: var(--text-light);
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 手机号输入框布局 */
|
||||||
|
.phone-input-wrapper {
|
||||||
|
display: flex;
|
||||||
|
gap: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.phone-input-wrapper input {
|
||||||
|
flex: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.send-code-btn {
|
||||||
|
padding: 12px 16px;
|
||||||
|
background: linear-gradient(135deg, var(--primary) 0%, #764ba2 100%);
|
||||||
|
color: white;
|
||||||
|
border: none;
|
||||||
|
border-radius: 10px;
|
||||||
|
font-size: 14px;
|
||||||
|
cursor: pointer;
|
||||||
|
white-space: nowrap;
|
||||||
|
transition: opacity 0.2s;
|
||||||
|
}
|
||||||
|
|
||||||
|
.send-code-btn:hover {
|
||||||
|
opacity: 0.9;
|
||||||
|
}
|
||||||
|
|
||||||
|
.send-code-btn:disabled {
|
||||||
|
opacity: 0.6;
|
||||||
|
cursor: not-allowed;
|
||||||
|
}
|
||||||
|
|
||||||
.auth-submit-btn {
|
.auth-submit-btn {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
padding: 14px;
|
padding: 14px;
|
||||||
|
|||||||
Reference in New Issue
Block a user