209 lines
6.5 KiB
JavaScript
209 lines
6.5 KiB
JavaScript
// Login Page JavaScript
|
||
|
||
$(document).ready(function() {
|
||
initLoginPage();
|
||
initTestAccount();
|
||
});
|
||
|
||
function initLoginPage() {
|
||
bindLoginEvents();
|
||
checkLoginStatus();
|
||
}
|
||
|
||
// 初始化测试账号
|
||
function initTestAccount() {
|
||
const users = JSON.parse(localStorage.getItem('users') || '[]');
|
||
|
||
// 检查是否已存在测试账号
|
||
const testUser = users.find(u => u.email === 'test@vizee.com');
|
||
|
||
if (!testUser) {
|
||
// 创建测试账号
|
||
const defaultTestUser = {
|
||
firstName: '测试',
|
||
lastName: '用户',
|
||
email: 'test@vizee.com',
|
||
password: '123456',
|
||
registerTime: new Date().toISOString()
|
||
};
|
||
|
||
users.push(defaultTestUser);
|
||
localStorage.setItem('users', JSON.stringify(users));
|
||
|
||
console.log('测试账号已创建:test@vizee.com / 123456');
|
||
}
|
||
}
|
||
|
||
// 绑定事件
|
||
function bindLoginEvents() {
|
||
// 切换到注册表单
|
||
$('#showRegister').on('click', function(e) {
|
||
e.preventDefault();
|
||
$('#loginForm').hide();
|
||
$('#registerForm').show();
|
||
});
|
||
|
||
// 切换到登录表单
|
||
$('#showLogin').on('click', function(e) {
|
||
e.preventDefault();
|
||
$('#registerForm').hide();
|
||
$('#loginForm').show();
|
||
});
|
||
|
||
// 登录表单提交
|
||
$('#loginForm form').on('submit', function(e) {
|
||
e.preventDefault();
|
||
handleLogin();
|
||
});
|
||
|
||
// 注册表单提交
|
||
$('#registerForm form').on('submit', function(e) {
|
||
e.preventDefault();
|
||
handleRegister();
|
||
});
|
||
|
||
// 社交登录
|
||
$('.btn-google').on('click', function() {
|
||
Toast.info(i18n.t('feature_coming_soon') || '功能开发中...');
|
||
});
|
||
|
||
$('.btn-wechat').on('click', function() {
|
||
Toast.info(i18n.t('feature_coming_soon') || '功能开发中...');
|
||
});
|
||
}
|
||
|
||
// 检查登录状态
|
||
function checkLoginStatus() {
|
||
const user = localStorage.getItem('currentUser');
|
||
if (user) {
|
||
// 已登录,检查是否有重定向URL
|
||
const redirectUrl = localStorage.getItem('redirectUrl');
|
||
if (redirectUrl) {
|
||
localStorage.removeItem('redirectUrl');
|
||
window.location.href = redirectUrl;
|
||
} else {
|
||
// 跳转到用户中心
|
||
window.location.href = 'user-center.html';
|
||
}
|
||
}
|
||
}
|
||
|
||
// 处理登录
|
||
function handleLogin() {
|
||
const email = $('#loginEmail').val().trim();
|
||
const password = $('#loginPassword').val().trim();
|
||
|
||
if (!email || !password) {
|
||
Toast.warning(i18n.t('please_fill_all_fields') || '请填写所有字段');
|
||
return;
|
||
}
|
||
|
||
if (!isValidEmail(email)) {
|
||
Toast.error(i18n.t('invalid_email') || '请输入有效的邮箱地址');
|
||
return;
|
||
}
|
||
|
||
// 显示加载状态
|
||
const $btn = $('#loginForm button[type="submit"]');
|
||
const originalText = $btn.text();
|
||
$btn.prop('disabled', true).text(i18n.t('logging_in') || '登录中...');
|
||
|
||
// 调用后端登录API
|
||
UserAPI.login(email, password)
|
||
.then(function(data) {
|
||
// 登录成功 - 保存后端返回的完整用户信息
|
||
const currentUser = {
|
||
...data.user, // 展开后端返回的完整用户对象(包括phone, avatar, gender等)
|
||
token: data.token,
|
||
loginTime: new Date().toISOString()
|
||
};
|
||
|
||
localStorage.setItem('currentUser', JSON.stringify(currentUser));
|
||
|
||
// 更新用户图标显示
|
||
if (typeof window.updateUserIcon === 'function') {
|
||
window.updateUserIcon();
|
||
}
|
||
|
||
Toast.success(i18n.t('login_success') || '登录成功!');
|
||
|
||
// 检查是否有重定向URL
|
||
setTimeout(() => {
|
||
const redirectUrl = localStorage.getItem('redirectUrl');
|
||
if (redirectUrl) {
|
||
localStorage.removeItem('redirectUrl');
|
||
window.location.href = redirectUrl;
|
||
} else {
|
||
window.location.href = 'user-center.html';
|
||
}
|
||
}, 500);
|
||
})
|
||
.catch(function(error) {
|
||
// 登录失败
|
||
console.error('登录失败:', error);
|
||
Toast.error(error.message || i18n.t('login_failed') || '登录失败,请检查邮箱和密码');
|
||
$btn.prop('disabled', false).text(originalText);
|
||
});
|
||
}
|
||
|
||
// 处理注册
|
||
function handleRegister() {
|
||
const firstName = $('#registerFirstName').val().trim();
|
||
const lastName = $('#registerLastName').val().trim();
|
||
const email = $('#registerEmail').val().trim();
|
||
const password = $('#registerPassword').val().trim();
|
||
const confirmPassword = $('#registerConfirmPassword').val().trim();
|
||
|
||
if (!firstName || !lastName || !email || !password || !confirmPassword) {
|
||
Toast.warning(i18n.t('please_fill_all_fields') || '请填写所有字段');
|
||
return;
|
||
}
|
||
|
||
if (!isValidEmail(email)) {
|
||
Toast.error(i18n.t('invalid_email') || '请输入有效的邮箱地址');
|
||
return;
|
||
}
|
||
|
||
if (password.length < 6) {
|
||
Toast.error(i18n.t('password_too_short') || '密码长度至少6个字符');
|
||
return;
|
||
}
|
||
|
||
if (password !== confirmPassword) {
|
||
Toast.error(i18n.t('password_not_match') || '两次输入的密码不一致');
|
||
return;
|
||
}
|
||
|
||
// 显示加载状态
|
||
const $btn = $('#registerForm button[type="submit"]');
|
||
const originalText = $btn.text();
|
||
$btn.prop('disabled', true).text(i18n.t('registering') || '注册中...');
|
||
|
||
// 调用后端注册API
|
||
const nickname = firstName + ' ' + lastName;
|
||
UserAPI.register(email, password, nickname)
|
||
.then(function(data) {
|
||
// 注册成功
|
||
Toast.success(i18n.t('register_success') || '注册成功!请登录');
|
||
|
||
// 切换到登录表单
|
||
$('#registerForm').hide();
|
||
$('#loginForm').show();
|
||
$('#loginEmail').val(email);
|
||
|
||
$btn.prop('disabled', false).text(originalText);
|
||
})
|
||
.catch(function(error) {
|
||
// 注册失败
|
||
console.error('注册失败:', error);
|
||
Toast.error(error.message || i18n.t('register_failed') || '注册失败,请稍后重试');
|
||
$btn.prop('disabled', false).text(originalText);
|
||
});
|
||
}
|
||
|
||
// 验证邮箱格式
|
||
function isValidEmail(email) {
|
||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||
return emailRegex.test(email);
|
||
}
|