Files
mce/templates/MetaLab-2026/static/js/login.js
Victor_Jay a62039e6bb fix: 登录/注册支持 redirect 参数,未登录跳转自动携带来源页
- login.js/register.js 登录成功后优先跳转 redirect 参数指定页面
- 新增 common.RedirectToLogin 辅助函数,自动携带当前路径
- 替换所有 9 处硬编码 /auth/login 重定向
2026-05-31 01:23:09 +08:00

136 lines
5.2 KiB
JavaScript

/* ============================================================
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;
// 读取 redirect 参数(登录成功后跳转目标)
function getRedirect() {
var m = window.location.search.match(/[?&]redirect=([^&]+)/);
if (m) return decodeURIComponent(m[1]);
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);
});
var loginInProgress = false;
function doLogin(email, password) {
if (loginInProgress) return;
loginInProgress = true;
submitBtn.disabled = true;
submitBtn.textContent = '登录中...';
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 && resp.action === 'confirm_restore') {
loginInProgress = false;
submitBtn.disabled = false;
submitBtn.textContent = '登录';
if (confirm(resp.message + '\n\n确认继续登录将中止注销流程。')) {
doConfirmRestore(email, password);
}
} else if (xhr.status === 200 && resp && resp.success) {
window.MetaLab.toast(toast, '登录成功', false);
setTimeout(function () {
window.location.href = getRedirect();
}, 800);
} else {
loginInProgress = false;
submitBtn.disabled = false;
submitBtn.textContent = '登录';
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 }));
}
function doConfirmRestore(email, password) {
if (loginInProgress) return;
loginInProgress = true;
submitBtn.disabled = true;
submitBtn.textContent = '恢复中...';
var xhr = new XMLHttpRequest();
xhr.open('POST', '/api/auth/confirm-restore', 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, resp.message || '账号已恢复,欢迎回来', false);
setTimeout(function () {
window.location.href = getRedirect();
}, 800);
} else {
loginInProgress = false;
submitBtn.disabled = false;
submitBtn.textContent = '登录';
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 }));
}
})();