初始化项目:基础设施 + 用户认证 + 后台管理系统 + AGPL 3.0 许可
This commit is contained in:
80
templates/MetaLab-2026/static/js/login.js
Normal file
80
templates/MetaLab-2026/static/js/login.js
Normal file
@ -0,0 +1,80 @@
|
||||
/* ============================================================
|
||||
MetaLab Login JS — 登录页面专属脚本
|
||||
============================================================ */
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
var loginForm = document.getElementById('loginForm');
|
||||
var emailInput = document.getElementById('email');
|
||||
var passwordInput = document.getElementById('password');
|
||||
var rememberMeCheckbox = document.getElementById('rememberMe');
|
||||
var emailError = document.getElementById('emailError');
|
||||
var passwordError = document.getElementById('passwordError');
|
||||
var submitBtn = document.getElementById('submitBtn');
|
||||
var toast = document.getElementById('toast');
|
||||
|
||||
if (!loginForm) return;
|
||||
|
||||
function checkFormValidity() {
|
||||
var email = emailInput.value.trim();
|
||||
var password = passwordInput.value;
|
||||
submitBtn.disabled = !(email && password);
|
||||
}
|
||||
|
||||
emailInput.addEventListener('input', function () {
|
||||
var v = emailInput.value.trim();
|
||||
if (v && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(v)) {
|
||||
emailInput.classList.add('error');
|
||||
emailError.style.display = 'block';
|
||||
} else {
|
||||
emailInput.classList.remove('error');
|
||||
emailError.style.display = 'none';
|
||||
}
|
||||
checkFormValidity();
|
||||
});
|
||||
|
||||
passwordInput.addEventListener('input', checkFormValidity);
|
||||
|
||||
loginForm.addEventListener('submit', function (e) {
|
||||
e.preventDefault();
|
||||
|
||||
var email = emailInput.value.trim();
|
||||
var password = passwordInput.value;
|
||||
|
||||
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
|
||||
emailInput.classList.add('error');
|
||||
emailError.style.display = 'block';
|
||||
return;
|
||||
}
|
||||
if (!password) {
|
||||
passwordInput.classList.add('error');
|
||||
passwordError.style.display = 'block';
|
||||
return;
|
||||
}
|
||||
|
||||
doLogin(email, password);
|
||||
});
|
||||
|
||||
function doLogin(email, password) {
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open('POST', '/api/auth/login', true);
|
||||
xhr.setRequestHeader('Content-Type', 'application/json');
|
||||
xhr.onreadystatechange = function () {
|
||||
if (xhr.readyState === 4) {
|
||||
var resp;
|
||||
try { resp = JSON.parse(xhr.responseText); } catch (err) { resp = null; }
|
||||
|
||||
if (xhr.status === 200 && resp && resp.success) {
|
||||
window.MetaLab.toast(toast, '登录成功', false);
|
||||
setTimeout(function () {
|
||||
window.location.href = '/';
|
||||
}, 800);
|
||||
} else {
|
||||
var msg = (resp && resp.message) ? resp.message : '登录失败,请稍后重试';
|
||||
window.MetaLab.toast(toast, msg, true);
|
||||
}
|
||||
}
|
||||
};
|
||||
xhr.send(JSON.stringify({ email: email, password: password, remember_me: rememberMeCheckbox.checked }));
|
||||
}
|
||||
})();
|
||||
Reference in New Issue
Block a user