Files
mce/templates/MetaLab-2026/static/js/login.js
Victor_Jay c73b131b83 feat: prompt/confirm 替换为自定义弹窗
- shared/utils.js 新增 showConfirm()/showPrompt() 通用函数(inline CSS,无需额外样式)
- admin/common.js 移除重复 showConfirm(),统一使用 utils.js
- admin/posts.js: 3 处 confirm/prompt 替换(审核/退回/锁定)
- admin/comments.js: 1 处 confirm 替换
- settings/index.html: 4 处 confirm + 1 处 prompt 替换
- studio/posts.html + drafts.html: 3 处 confirm 替换
- post-view.js + login.js + post-favorite.js: 3 处 confirm 替换
2026-06-02 20:11:48 +08:00

149 lines
5.8 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]);
// 回退:如果来源页是本站页面,跳回来源页
try {
var ref = document.referrer;
if (ref) {
var a = document.createElement('a');
a.href = ref;
if (a.hostname === window.location.hostname &&
a.pathname !== window.location.pathname &&
a.pathname.indexOf('/auth/') !== 0) {
return a.pathname + a.search + a.hash;
}
}
} catch (e) {}
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 = '登录';
showConfirm('确认', resp.message + '\n\n确认登录将撤销注销并恢复账号。').then(function(ok) {
if (ok) 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 }));
}
})();