fix: dynamic site name from DB, displayed in header/title/sidebar

This commit is contained in:
2026-07-16 04:02:19 +08:00
parent abf747e3af
commit f423e1f791
3 changed files with 22 additions and 5 deletions

View File

@@ -17,6 +17,19 @@ const HTML_PATH = path.join(PUBLIC_DIR, 'index.html');
let cachedHtml = null; let cachedHtml = null;
let htmlTimestamp = 0; let htmlTimestamp = 0;
let cachedSiteName = 'MC举报系统';
function getSiteName() { return cachedSiteName; }
async function refreshSiteName() {
if (!isInstalled()) return;
try {
const { getPool } = require('./db');
const p = await getPool();
const [rows] = await p.execute("SELECT v FROM settings WHERE k='site_name'");
if (rows.length > 0) cachedSiteName = rows[0].v;
} catch {}
}
function getHtmlWithKey() { function getHtmlWithKey() {
const stat = fs.statSync(HTML_PATH); const stat = fs.statSync(HTML_PATH);
@@ -26,7 +39,7 @@ function getHtmlWithKey() {
const cfg = getConfig(); const cfg = getConfig();
const key = cfg?.api_key || ''; const key = cfg?.api_key || '';
const redirect = isInstalled() ? '' : '<script>location.hash="#/install"</script>'; const redirect = isInstalled() ? '' : '<script>location.hash="#/install"</script>';
html = html.replace('</head>', `<script>window.__API_KEY__=${JSON.stringify(key)}</script>${redirect}</head>`); html = html.replace('</head>', `<script>window.__API_KEY__=${JSON.stringify(key)};window.__SITE_NAME__=${JSON.stringify(cachedSiteName)}</script>${redirect}</head>`);
cachedHtml = html; cachedHtml = html;
htmlTimestamp = mtime; htmlTimestamp = mtime;
return html; return html;
@@ -135,6 +148,6 @@ app.use((err, req, res, next) => {
}); });
app.listen(PORT, () => { app.listen(PORT, () => {
if (isInstalled()) console.log(`Server running at http://localhost:${PORT}`); if (isInstalled()) { refreshSiteName().then(() => console.log(`Server running at http://localhost:${PORT}`)); }
else console.log(`System not installed. Visit http://localhost:${PORT}/#/install`); else console.log(`System not installed. Visit http://localhost:${PORT}/#/install`);
}).on('error', (err) => { console.error('Failed to start:', err.message); process.exit(1); }); }).on('error', (err) => { console.error('Failed to start:', err.message); process.exit(1); });

View File

@@ -3,14 +3,14 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MC举报系统</title> <title id="site-title">MC举报系统</title>
<link rel="stylesheet" href="css/style.css"> <link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
</head> </head>
<body> <body>
<div id="top-bar"> <div id="top-bar">
<div class="top-bar-inner"> <div class="top-bar-inner">
<a href="#/home" class="top-logo"><i class="fas fa-shield-halved"></i><span>MC举报系统</span></a> <a href="#/home" class="top-logo"><i class="fas fa-shield-halved"></i><span id="logo-text">MC举报系统</span></a>
<div id="top-auth"> <div id="top-auth">
<a href="#/login" class="btn btn-o btn-sm"><i class="fas fa-sign-in-alt"></i> 登录</a> <a href="#/login" class="btn btn-o btn-sm"><i class="fas fa-sign-in-alt"></i> 登录</a>
<a href="#/register" class="btn btn-p btn-sm"><i class="fas fa-user-plus"></i> 注册</a> <a href="#/register" class="btn btn-p btn-sm"><i class="fas fa-user-plus"></i> 注册</a>
@@ -22,7 +22,7 @@
<div id="public-page" class="page hidden"></div> <div id="public-page" class="page hidden"></div>
<div id="main-layout" class="hidden"> <div id="main-layout" class="hidden">
<aside id="sidebar"> <aside id="sidebar">
<div class="sidebar-header"><i class="fas fa-shield-halved"></i><span>举报系统</span></div> <div class="sidebar-header"><i class="fas fa-shield-halved"></i><span id="sidebar-title">举报系统</span></div>
<nav id="sidebar-nav"></nav> <nav id="sidebar-nav"></nav>
<div class="sidebar-footer"> <div class="sidebar-footer">
<div id="user-info"></div> <div id="user-info"></div>

View File

@@ -15,6 +15,10 @@ const App = {
], ],
async init() { async init() {
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'; return; } } catch { location.hash = '#/install'; return; } try { const s = await API.get('/install/status'); if (!s.installed) { location.hash = '#/install'; return; } } catch { location.hash = '#/install'; return; }
this._footerLoaded = false; this._footerLoaded = false;
window.addEventListener('hashchange', () => this.route()); window.addEventListener('hashchange', () => this.route());