88 lines
2.8 KiB
JavaScript
88 lines
2.8 KiB
JavaScript
// 登录页面特定的JavaScript
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
const loginForm = document.getElementById('loginForm');
|
|
const alertBox = document.getElementById('alertBox');
|
|
const passwordInput = document.getElementById('password');
|
|
const togglePassword = document.getElementById('togglePassword');
|
|
|
|
// 显示/隐藏密码
|
|
if (togglePassword) {
|
|
togglePassword.addEventListener('click', function() {
|
|
const type = passwordInput.getAttribute('type') === 'password' ? 'text' : 'password';
|
|
passwordInput.setAttribute('type', type);
|
|
|
|
// 切换图标
|
|
togglePassword.classList.toggle('fa-eye');
|
|
togglePassword.classList.toggle('fa-eye-slash');
|
|
});
|
|
}
|
|
|
|
// 登录表单提交
|
|
if (loginForm) {
|
|
loginForm.addEventListener('submit', function(e) {
|
|
e.preventDefault();
|
|
|
|
const email = document.getElementById('email').value;
|
|
const password = passwordInput.value;
|
|
const submitBtn = document.querySelector('button[type="submit"]');
|
|
const currentLang = localStorage.getItem('language') || 'zh';
|
|
|
|
// 表单验证
|
|
if (!email || !password) {
|
|
showAlert('danger', currentLang === 'zh' ? '请填写所有必填字段' : 'Please fill in all required fields');
|
|
return;
|
|
}
|
|
|
|
// 显示加载状态
|
|
const originalBtnText = submitBtn.innerHTML;
|
|
const loadingText = currentLang === 'zh' ? '登录中...' : 'Logging in...';
|
|
submitBtn.disabled = true;
|
|
submitBtn.innerHTML = `<span class="spinner"></span>${loadingText}`;
|
|
|
|
// 发送登录请求
|
|
$.ajax({
|
|
url: '/auth/login',
|
|
type: 'POST',
|
|
contentType: 'application/json',
|
|
data: JSON.stringify({
|
|
email: email,
|
|
password: password
|
|
}),
|
|
success: function(response) {
|
|
if (response.success) {
|
|
// 登录成功,跳转到主页
|
|
window.location.href = '/dashboard';
|
|
} else {
|
|
// 显示错误信息
|
|
showAlert('danger', response.message);
|
|
submitBtn.disabled = false;
|
|
submitBtn.innerHTML = originalBtnText;
|
|
}
|
|
},
|
|
error: function(xhr) {
|
|
let errorMsg = currentLang === 'zh' ? '登录失败,请稍后再试' : 'Login failed, please try again later';
|
|
if (xhr.responseJSON && xhr.responseJSON.message) {
|
|
errorMsg = xhr.responseJSON.message;
|
|
}
|
|
showAlert('danger', errorMsg);
|
|
submitBtn.disabled = false;
|
|
submitBtn.innerHTML = originalBtnText;
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
// 显示提示信息
|
|
function showAlert(type, message) {
|
|
alertBox.className = `alert alert-${type}`;
|
|
alertBox.textContent = message;
|
|
alertBox.classList.remove('d-none');
|
|
|
|
// 滚动到顶部
|
|
window.scrollTo({
|
|
top: 0,
|
|
behavior: 'smooth'
|
|
});
|
|
}
|
|
});
|