From 6ac8a5cd1178d7582fb69a317a45330e62435efe Mon Sep 17 00:00:00 2001 From: canglan Date: Fri, 21 Aug 2026 20:31:45 +0800 Subject: [PATCH] fix: site_name not loaded + expired token not cleared - app.js init(): use native fetch (no Bearer) to call /api/install/status and /api/settings/settings, so site_name loads even before login state is confirmed; call Auth.reset() if logged but no site_name set - auth.js: add reset() method that clears TK/US without redirecting - app.js route(): remove early return for uninstalled; let install check run first so init() can fall through to normal routing when installed - app.js renderMain(): catch 401/login errors from page render, clear Auth state, show login prompt with button that redirects back after login (target hash preserved) --- public/js/app.js | 30 +++++++++++++++++++++++++++--- public/js/auth.js | 6 ++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/public/js/app.js b/public/js/app.js index 8dbdae9..3ff9938 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -37,11 +37,25 @@ const App = { ], async init() { + // 获取站点名称(无论是否安装),使用原生 fetch 避免携带 Bearer token + try { + const r = await fetch('/api/install/status'); + const s = await r.json(); + if (!s.installed) { location.hash = '#/install'; return; } + } catch { location.hash = '#/install'; return; } + try { + const nameRes = await fetch('/api/settings/settings'); + if (nameRes.ok) { + const settings = await nameRes.json(); + if (settings.site_name && settings.site_name.value) window.__SITE_NAME__ = settings.site_name.value; + } + } catch {} + // 确保登录态同步:若 cookie 还在但 token 已过期,清除 + if (Auth.logged() && !window.__SITE_NAME__) Auth.reset(); if (window.__SITE_NAME__) { document.getElementById('site-title').textContent = window.__SITE_NAME__; document.getElementById('sidebar-title').textContent = window.__SITE_NAME__ || '举报系统'; } - try { const s = await API.get('/install/status'); if (!s.installed) location.hash = '#/install'; } catch { location.hash = '#/install'; } this._footerLoaded = false; window.addEventListener('hashchange', () => this.route()); window.onerror = (msg, url, line, col, err) => { @@ -180,8 +194,18 @@ const App = { document.getElementById('page-title').textContent = title; document.getElementById('page-actions').innerHTML = ''; const ct = document.getElementById('page-content'); - ct.innerHTML = await page.render(param); - if (page.mount) await page.mount(param); + try { + ct.innerHTML = await page.render(param); + if (page.mount) await page.mount(param); + } catch (e) { + // 401 / 登录过期: 清除状态, 保留访问目标页 hash 提示重新登录 + if (e && e.message && e.message.includes('登录')) { + Auth.reset(); + const target = location.hash; + ct.innerHTML = `
登录已过期,请重新登录。
+
`; + } else throw e; + } } }; diff --git a/public/js/auth.js b/public/js/auth.js index 057fe48..87fa499 100644 --- a/public/js/auth.js +++ b/public/js/auth.js @@ -42,6 +42,12 @@ const Auth = { location.hash = '#/login'; }, + reset() { + // 清除本地登录态但不清除 URL(调用方决定是否跳转) + localStorage.removeItem(this.TK); + localStorage.removeItem(this.US); + }, + role(...r) { const u = this.user(); return u && r.includes(u.role); }, isStaff() { return this.role('owner', 'admin'); }, isAdmin() { return this.role('owner', 'admin'); },