修复无法保存配置的问题
This commit is contained in:
@@ -588,34 +588,67 @@ app.get('/api/settings', async (req, res) => {
|
||||
|
||||
// Update site settings
|
||||
app.post('/api/settings', requireAuth, async (req, res) => {
|
||||
const { page_name, title, logo_url, logo_url_dark, favicon_url, default_theme, show_95_bandwidth, p95_type, blackbox_source_id, latency_source, latency_dest, latency_target, icp_filing, ps_filing } = req.body;
|
||||
try {
|
||||
await db.query(
|
||||
`INSERT INTO site_settings (id, page_name, title, logo_url, logo_url_dark, favicon_url, default_theme, show_95_bandwidth, p95_type, blackbox_source_id, latency_source, latency_dest, latency_target, icp_filing, ps_filing)
|
||||
VALUES (1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
page_name = VALUES(page_name),
|
||||
title = VALUES(title),
|
||||
logo_url = VALUES(logo_url),
|
||||
logo_url_dark = VALUES(logo_url_dark),
|
||||
favicon_url = VALUES(favicon_url),
|
||||
default_theme = VALUES(default_theme),
|
||||
show_95_bandwidth = VALUES(show_95_bandwidth),
|
||||
p95_type = VALUES(p95_type),
|
||||
blackbox_source_id = VALUES(blackbox_source_id),
|
||||
latency_source = VALUES(latency_source),
|
||||
latency_dest = VALUES(latency_dest),
|
||||
latency_target = VALUES(latency_target),
|
||||
icp_filing = VALUES(icp_filing),
|
||||
ps_filing = VALUES(ps_filing)`,
|
||||
[
|
||||
page_name, title, logo_url, logo_url_dark, favicon_url, default_theme,
|
||||
show_95_bandwidth ? 1 : 0, p95_type || 'tx',
|
||||
blackbox_source_id || null, latency_source || null,
|
||||
latency_dest || null, latency_target || null,
|
||||
icp_filing || null, ps_filing || null
|
||||
]
|
||||
);
|
||||
try {
|
||||
// 1. Fetch current settings first to preserve fields not sent by the UI
|
||||
const [rows] = await db.query('SELECT * FROM site_settings WHERE id = 1');
|
||||
let current = rows.length > 0 ? rows[0] : {};
|
||||
|
||||
// 2. Destructure fields from body
|
||||
const {
|
||||
page_name, title, logo_url, logo_url_dark, favicon_url,
|
||||
default_theme, show_95_bandwidth, p95_type,
|
||||
icp_filing, ps_filing
|
||||
} = req.body;
|
||||
|
||||
// 3. Prepare parameters, prioritizing body but falling back to current
|
||||
const settings = {
|
||||
page_name: page_name !== undefined ? page_name : (current.page_name || '数据可视化展示大屏'),
|
||||
title: title !== undefined ? title : (current.title || '数据可视化展示大屏'),
|
||||
logo_url: logo_url !== undefined ? logo_url : (current.logo_url || null),
|
||||
logo_url_dark: logo_url_dark !== undefined ? logo_url_dark : (current.logo_url_dark || null),
|
||||
favicon_url: favicon_url !== undefined ? favicon_url : (current.favicon_url || null),
|
||||
default_theme: default_theme !== undefined ? default_theme : (current.default_theme || 'dark'),
|
||||
show_95_bandwidth: show_95_bandwidth !== undefined ? (show_95_bandwidth ? 1 : 0) : (current.show_95_bandwidth || 0),
|
||||
p95_type: p95_type !== undefined ? p95_type : (current.p95_type || 'tx'),
|
||||
blackbox_source_id: current.blackbox_source_id || null, // UI doesn't send this
|
||||
latency_source: current.latency_source || null, // UI doesn't send this
|
||||
latency_dest: current.latency_dest || null, // UI doesn't send this
|
||||
latency_target: current.latency_target || null, // UI doesn't send this
|
||||
icp_filing: icp_filing !== undefined ? icp_filing : (current.icp_filing || null),
|
||||
ps_filing: ps_filing !== undefined ? ps_filing : (current.ps_filing || null)
|
||||
};
|
||||
|
||||
// 4. Update database
|
||||
await db.query(
|
||||
`INSERT INTO site_settings (
|
||||
id, page_name, title, logo_url, logo_url_dark, favicon_url,
|
||||
default_theme, show_95_bandwidth, p95_type,
|
||||
blackbox_source_id, latency_source, latency_dest, latency_target,
|
||||
icp_filing, ps_filing
|
||||
) VALUES (1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
page_name = VALUES(page_name),
|
||||
title = VALUES(title),
|
||||
logo_url = VALUES(logo_url),
|
||||
logo_url_dark = VALUES(logo_url_dark),
|
||||
favicon_url = VALUES(favicon_url),
|
||||
default_theme = VALUES(default_theme),
|
||||
show_95_bandwidth = VALUES(show_95_bandwidth),
|
||||
p95_type = VALUES(p95_type),
|
||||
blackbox_source_id = VALUES(blackbox_source_id),
|
||||
latency_source = VALUES(latency_source),
|
||||
latency_dest = VALUES(latency_dest),
|
||||
latency_target = VALUES(latency_target),
|
||||
icp_filing = VALUES(icp_filing),
|
||||
ps_filing = VALUES(ps_filing)`,
|
||||
[
|
||||
settings.page_name, settings.title, settings.logo_url, settings.logo_url_dark, settings.favicon_url,
|
||||
settings.default_theme, settings.show_95_bandwidth, settings.p95_type,
|
||||
settings.blackbox_source_id, settings.latency_source, settings.latency_dest, settings.latency_target,
|
||||
settings.icp_filing, settings.ps_filing
|
||||
]
|
||||
);
|
||||
|
||||
res.json({ success: true });
|
||||
} catch (err) {
|
||||
console.error('Error updating settings:', err);
|
||||
|
||||
Reference in New Issue
Block a user