78 lines
3.3 KiB
JavaScript
78 lines
3.3 KiB
JavaScript
'use strict';
|
|
function escapeLoginText(value) {
|
|
return String(value ?? '').replace(
|
|
/[&<>"']/g,
|
|
c => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[c],
|
|
);
|
|
}
|
|
function loginErrorHTML(error, fallbackStep) {
|
|
const { step, message, details } = LoginDiagnostics.describe(error, fallbackStep);
|
|
return `<section class="error" role="alert"><strong>${escapeLoginText(step)}失败</strong><p>${escapeLoginText(message)}</p><div data-login-diagnostic style="white-space:pre-line;overflow-wrap:anywhere">${escapeLoginText(details)}</div><button type="button" data-action="copy-login-diagnostic" class="quiet">复制诊断信息</button></section>`;
|
|
}
|
|
|
|
async function copyLoginDiagnostic(button) {
|
|
const details = button.parentElement.querySelector('[data-login-diagnostic]');
|
|
button.textContent = await LoginDiagnostics.copy(details.innerText);
|
|
}
|
|
|
|
function bindStandaloneLogin() {
|
|
const form = document.querySelector('form');
|
|
if (!form) return;
|
|
const status = document.getElementById('login-status');
|
|
const notice = document.getElementById('login-error');
|
|
const buttons = [...form.querySelectorAll('button[type=submit]')];
|
|
const url = form.getAttribute('action');
|
|
let saving = false;
|
|
buttons.forEach(button => (button.disabled = false));
|
|
notice.addEventListener('click', event => {
|
|
const button = event.target.closest('[data-action="copy-login-diagnostic"]');
|
|
if (button) copyLoginDiagnostic(button);
|
|
});
|
|
form.addEventListener('submit', async event => {
|
|
event.preventDefault();
|
|
if (saving) return;
|
|
const data = Object.fromEntries(new FormData(form));
|
|
if (url === '/api/demo-login') data.member = event.submitter.value;
|
|
saving = true;
|
|
buttons.forEach(button => (button.disabled = true));
|
|
notice.replaceChildren();
|
|
status.textContent = '正在' + LoginDiagnostics.steps[url] + '…';
|
|
const controller = new AbortController(),
|
|
started = Date.now();
|
|
const timer = setTimeout(() => controller.abort(), 25000);
|
|
let response;
|
|
try {
|
|
response = await fetch(url, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json', 'X-Requested-With': 'maimai-ledger' },
|
|
body: JSON.stringify(data),
|
|
signal: controller.signal,
|
|
});
|
|
if (!response.ok) {
|
|
const result = await response.json().catch(() => null);
|
|
throw Object.assign(new Error(result?.error || '服务器返回了错误,请稍后再试。'), { status: response.status });
|
|
}
|
|
const result = await response.json();
|
|
if (!result?.user?.id) throw new Error('响应缺少登录结果');
|
|
status.textContent = '已登录,正在打开账本…';
|
|
location.reload();
|
|
} catch (error) {
|
|
error.loginDiagnostic = {
|
|
path: url,
|
|
method: 'POST',
|
|
status: response?.status,
|
|
timedOut: controller.signal.aborted,
|
|
elapsed: Date.now() - started,
|
|
};
|
|
notice.innerHTML = loginErrorHTML(error, LoginDiagnostics.steps[url]);
|
|
status.textContent = '';
|
|
saving = false;
|
|
buttons.forEach(button => (button.disabled = false));
|
|
} finally {
|
|
clearTimeout(timer);
|
|
}
|
|
});
|
|
}
|
|
if (typeof document !== 'undefined' && document.body?.hasAttribute('data-standalone-login')) bindStandaloneLogin();
|
|
if (typeof module !== 'undefined' && module.exports) module.exports = { escapeLoginText };
|