Files
weiguang-jewelry-ledger/新账系统/public/login-diagnostics.js
T

53 lines
2.4 KiB
JavaScript

'use strict';
// 轻量登录页、旧版外壳与 React 共用。可复制字段不包含账号、表单或响应正文。
(function (root) {
/** @type {Readonly<Record<string, string>>} */
const steps = {
'/api/login': '提交登录',
'/api/demo-login': '进入演示',
'/api/setup': '建立账号',
'/api/session': '检查登录状态',
'/api/state': '读取账本',
};
/** @typedef {{path: string, method: string, status?: number, timedOut: boolean, elapsed: number}} Attempt */
/** @param {Error & {status?: number, loginDiagnostic?: Attempt}} error @param {string} fallbackStep */
function describe(error, fallbackStep) {
const diagnostic = error.loginDiagnostic;
const step = diagnostic ? steps[diagnostic.path] : fallbackStep;
let message;
if (!diagnostic) message = '页面处理出错,请刷新后重试。';
else if (error.status) message = error.message;
else if (diagnostic.timedOut) message = '等待超过 25 秒,浏览器已停止等待。';
else if (diagnostic.status) message = '已收到服务器响应,但未能读取完整的账本系统数据。';
else message = '浏览器未收到 HTTP 响应,具体中断原因还需对照服务器日志。';
const time = new Date().toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai', hour12: false });
const lines = ['步骤:' + step, '北京时间:' + time];
if (diagnostic) {
lines.push('接口:' + diagnostic.method + ' ' + diagnostic.path, 'HTTP 状态:' + (diagnostic.status || '未收到'));
lines.push('本次请求耗时:' + (diagnostic.elapsed / 1000).toFixed(1) + ' 秒');
}
const browserMessages = [
'Load failed',
'Failed to fetch',
'fetch is aborted',
'Fetch is aborted',
'NetworkError when attempting to fetch resource.',
'The operation was aborted.',
];
if (browserMessages.includes(error.message)) lines.push('浏览器提示:' + error.message);
return { step, message, details: lines.join('\n') };
}
/** @param {string} details */
async function copy(details) {
try {
await navigator.clipboard.writeText(details);
return '已复制';
} catch {
return '请长按或选中上方文字复制';
}
}
const api = { steps, describe, copy };
if (typeof module === 'object' && module.exports) module.exports = api;
else Object.assign(root, { LoginDiagnostics: api });
})(globalThis);