From 03718e6a62af3ae06a795d9447570077b3df866e Mon Sep 17 00:00:00 2001 From: Nixevol Date: Fri, 15 May 2026 14:45:02 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=97=A7=E7=89=88?= =?UTF-8?q?=E5=89=8D=E7=AB=AF=E7=99=BB=E5=BD=95=E8=B7=B3=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/project_context.md | 7 ++++++ frontend_old/js/app.js | 53 +++++++++++++++++++++++++++++++++-------- frontend_old/login.html | 21 ++++++++++++---- 3 files changed, 67 insertions(+), 14 deletions(-) diff --git a/docs/project_context.md b/docs/project_context.md index 9657eda..03dc521 100644 --- a/docs/project_context.md +++ b/docs/project_context.md @@ -1,5 +1,12 @@ # 项目上下文记录 +## 2026-05-15:修复旧版前端未登录闪烁 + +- `frontend_old/js/app.js` 现在会在首页初始化前检查登录 token;未登录时直接跳到旧版登录页,避免首页继续初始化并反复请求 API 造成未授权提示闪烁。 +- 旧版前端鉴权统一兼容 `capacity_report_token` 和旧 key `token`:请求优先读取新版 key,登录页会同时写入两个 key,并继续写入 `token` cookie。 +- 旧版 API 401、XHR 上传 401 和退出登录都会清理两个本地 token key 及 cookie,并跳转到 `/login.html`;通过 `/old/...` 路径访问旧版时会跳转到 `/old/login.html`。 +- 已用 headless Chrome 验证:清空本地存储后访问 `http://127.0.0.1:9082/` 会进入 `http://127.0.0.1:9082/login.html`;按本机 `auth.ini` 登录后回到旧版首页,`/api/cache/size` 携带 token 调用返回 200。 + ## 2026-05-15:新版上传页对齐旧版布局并恢复拖拽上传 - `frontend/src/components/FileWorkflow.vue` 的上传页内容区已按旧版 `frontend_old/index.html` 的上传结构重排,保留新版侧边导航和顶部标题栏,只对齐页面主体中的上传区、文件列表、上传进度和处理日志布局。 diff --git a/frontend_old/js/app.js b/frontend_old/js/app.js index 8fdcb86..36fa39b 100644 --- a/frontend_old/js/app.js +++ b/frontend_old/js/app.js @@ -5,6 +5,38 @@ // ==================== 工具函数 ==================== +const LEGACY_TOKEN_KEY = 'token'; +const APP_TOKEN_KEY = 'capacity_report_token'; + +function getLegacyLoginPath() { + const path = window.location.pathname; + return path === '/old' || path.startsWith('/old/') ? '/old/login.html' : '/login.html'; +} + +function getAuthToken() { + return localStorage.getItem(APP_TOKEN_KEY) || localStorage.getItem(LEGACY_TOKEN_KEY) || ''; +} + +function clearAuthToken() { + localStorage.removeItem(APP_TOKEN_KEY); + localStorage.removeItem(LEGACY_TOKEN_KEY); + document.cookie = 'token=; path=/; max-age=0'; +} + +function redirectToLogin() { + const loginPath = getLegacyLoginPath(); + if (window.location.pathname !== loginPath) { + window.location.replace(loginPath); + } +} + +function requireAuthToken() { + if (getAuthToken()) return true; + clearAuthToken(); + redirectToLogin(); + return false; +} + function $(selector) { return document.querySelector(selector); } @@ -114,7 +146,7 @@ async function api(endpoint, options = {}) { // 如果没有指定 method 且没有 body,默认使用 GET const method = options.method || (options.body ? 'POST' : 'GET'); - const token = localStorage.getItem('token'); + const token = getAuthToken(); const fetchOptions = { method: method, @@ -134,7 +166,7 @@ async function api(endpoint, options = {}) { const response = await fetch(`/api${endpoint}`, fetchOptions); if (response.status === 401 && endpoint !== '/login') { - localStorage.removeItem('token'); + clearAuthToken(); showLoginModal(); throw new Error('未授权或登录已过期,请重新登录'); } @@ -148,15 +180,13 @@ async function api(endpoint, options = {}) { } function showLoginModal() { - localStorage.removeItem('token'); - document.cookie = 'token=; path=/; max-age=0'; - window.location.href = '/'; + clearAuthToken(); + redirectToLogin(); } function logout() { - localStorage.removeItem('token'); - document.cookie = 'token=; path=/; max-age=0'; - window.location.href = '/'; + clearAuthToken(); + window.location.href = getLegacyLoginPath(); } @@ -651,7 +681,7 @@ class FileUploader { resolve({ success: true }); } } else if (xhr.status === 401) { - localStorage.removeItem('token'); + clearAuthToken(); showLoginModal(); reject(new Error('未授权或登录已过期,请重新登录')); } else { @@ -666,7 +696,7 @@ class FileUploader { // 开始上传 xhr.open('POST', '/api/upload'); - const token = localStorage.getItem('token'); + const token = getAuthToken(); if (token) { xhr.setRequestHeader('Authorization', `Bearer ${token}`); } @@ -2208,6 +2238,9 @@ async function updateCacheSize() { } document.addEventListener('DOMContentLoaded', () => { + if (!requireAuthToken()) { + return; + } initApp(); }); diff --git a/frontend_old/login.html b/frontend_old/login.html index e7dc842..830c64d 100644 --- a/frontend_old/login.html +++ b/frontend_old/login.html @@ -161,6 +161,21 @@