- 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 单机挂或旧脏值导致配置变更长时间不生效
91 lines
2.9 KiB
TypeScript
91 lines
2.9 KiB
TypeScript
import { db } from '~/drizzle/db'
|
|
import { systemSettings } from '~/drizzle/schema'
|
|
import { CacheService } from '../../../services/cacheService'
|
|
import { SYSTEM_SETTINGS_DEFAULTS } from '~~/server/utils/system-settings-defaults'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
// 检查用户认证和权限
|
|
const user = event.context.user
|
|
|
|
if (!user) {
|
|
throw createError({
|
|
statusCode: 401,
|
|
message: '未授权访问'
|
|
})
|
|
}
|
|
|
|
if (!['ADMIN', 'SUPER_ADMIN'].includes(user.role)) {
|
|
throw createError({
|
|
statusCode: 403,
|
|
message: '只有管理员才能查看系统设置'
|
|
})
|
|
}
|
|
|
|
try {
|
|
// 尝试从缓存获取系统设置
|
|
const cacheService = CacheService.getInstance()
|
|
let settings = await cacheService.getSystemSettings()
|
|
|
|
if (settings) {
|
|
return settings
|
|
}
|
|
|
|
// 缓存中没有,从数据库获取系统设置,如果不存在则创建默认设置
|
|
const settingsResult = await db.select().from(systemSettings).limit(1)
|
|
settings = settingsResult[0] || null
|
|
|
|
if (!settings) {
|
|
const newSettingsResult = await db
|
|
.insert(systemSettings)
|
|
.values({
|
|
enablePlayTimeSelection: false,
|
|
siteTitle: process.env.NUXT_PUBLIC_SITE_TITLE || 'VoiceHub',
|
|
siteLogoUrl: process.env.NUXT_PUBLIC_SITE_LOGO || '/images/site-logo.png',
|
|
schoolLogoHomeUrl: '/images/school-logo-white.png',
|
|
schoolLogoPrintUrl: '/images/school-logo-transparent.png',
|
|
siteDescription:
|
|
process.env.NUXT_PUBLIC_SITE_DESCRIPTION || '校园广播站点歌系统 - 让你的声音被听见',
|
|
siteVersion: process.env.NUXT_PUBLIC_SITE_VERSION || null,
|
|
submissionGuidelines: '请遵守校园规定,提交健康向上的歌曲。',
|
|
icpNumber: null,
|
|
gonganNumber: null,
|
|
enableSubmissionLimit: false,
|
|
dailySubmissionLimit: null,
|
|
weeklySubmissionLimit: null,
|
|
monthlySubmissionLimit: null,
|
|
showBlacklistKeywords: false,
|
|
enableCollaborativeSubmission: true,
|
|
enableSubmissionRemarks: false,
|
|
enableRequestTimeLimitation: false,
|
|
forceBlockAllRequests: false,
|
|
enableReplayRequests: false,
|
|
smtpEnabled: false
|
|
})
|
|
.returning()
|
|
settings = newSettingsResult[0]
|
|
}
|
|
|
|
// 将设置存入缓存
|
|
try {
|
|
const merged: any = { ...SYSTEM_SETTINGS_DEFAULTS, ...(settings || {}) }
|
|
settings = merged
|
|
await cacheService.setSystemSettings(settings)
|
|
} catch (cacheError) {
|
|
console.warn('缓存系统设置失败:', cacheError)
|
|
}
|
|
|
|
// 隐藏敏感字段
|
|
if (settings && settings.smtpPassword) {
|
|
settings.smtpPassword = '****************'
|
|
}
|
|
|
|
return settings
|
|
} catch (error) {
|
|
console.error('获取系统设置失败:', error)
|
|
throw createError({
|
|
statusCode: 500,
|
|
message: '获取系统设置失败'
|
|
})
|
|
}
|
|
})
|