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 = `