fix(system-settings): 修复线上主题/排期/聚合登录等配置缺失,对新 DB 字段用 SYSTEM_SETTINGS_DEFAULTS 兜底 + 更新接口允许写入新字段 + 缩短 Redis 缓存 TTL
- site-config.get.ts:filterPublicSettings 和 Redis/DB 命中分支全部先合并 SYSTEM_SETTINGS_DEFAULTS,解决老库无 schema 新字段导致 public 返回 defaultTheme=light、enabledThemes=light,dark 等旧值的问题 - admin/system-settings/index.post.ts:新增 ~40 个白名单字段(主题/排期可见范围/留言自动审查/OAuth 聚合/首次强制改密/投稿限制/卡密/注册审核/自动备份/统计代码等),支持管理面板保存生效;更新返回结果用 fillDefaults(DEFAULTS) 兜底 - admin/system-settings/index.ts (GET):返回 DB 记录前合并 SYSTEM_SETTINGS_DEFAULTS,避免管理后台 UI 拿不到新字段 - cacheService.ts:CACHE_TTL.SYSTEM_SETTINGS 3600s → 900s(60min→15min),防止 Redis 单机挂或旧脏值导致配置变更长时间不生效
This commit is contained in:
@@ -2,6 +2,7 @@ import { db } from '~/drizzle/db'
|
||||
import { systemSettings } from '~/drizzle/schema'
|
||||
import { cacheService } from '../services/cacheService'
|
||||
import { isRedisReady } from '../utils/redis'
|
||||
import { SYSTEM_SETTINGS_DEFAULTS } from '~~/server/utils/system-settings-defaults'
|
||||
|
||||
// 过滤敏感字段,只返回公开配置
|
||||
const publicFields = [
|
||||
@@ -36,13 +37,11 @@ const publicFields = [
|
||||
]
|
||||
|
||||
const filterPublicSettings = (data: any) => {
|
||||
if (!data) {
|
||||
return {}
|
||||
}
|
||||
const merged: any = { ...SYSTEM_SETTINGS_DEFAULTS, ...(data || {}) }
|
||||
const result: Record<string, any> = {}
|
||||
for (const key of publicFields) {
|
||||
if (Object.prototype.hasOwnProperty.call(data, key)) {
|
||||
result[key] = data[key]
|
||||
if (Object.prototype.hasOwnProperty.call(merged, key)) {
|
||||
result[key] = merged[key]
|
||||
}
|
||||
}
|
||||
return result
|
||||
@@ -55,7 +54,7 @@ export default defineEventHandler(async (event) => {
|
||||
const cachedSettings = await cacheService.getSystemSettings()
|
||||
if (cachedSettings) {
|
||||
console.log('[API] 系统设置缓存命中')
|
||||
return filterPublicSettings(cachedSettings)
|
||||
return filterPublicSettings({ ...SYSTEM_SETTINGS_DEFAULTS, ...cachedSettings })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,7 +99,7 @@ export default defineEventHandler(async (event) => {
|
||||
settings = newSettings[0]
|
||||
}
|
||||
|
||||
const publicSettings = filterPublicSettings(settings)
|
||||
const publicSettings = filterPublicSettings({ ...SYSTEM_SETTINGS_DEFAULTS, ...(settings || {}) })
|
||||
|
||||
// 将结果缓存到Redis(如果可用)- 永久缓存
|
||||
if (settings && isRedisReady()) {
|
||||
|
||||
Reference in New Issue
Block a user