Files
BYW-VoiceHub/server/api/admin/system-settings/index.ts
T
Henry_Du 4ce4649aaf
Build VoiceHub FPK / build-fpk (push) Failing after 56m5s
Build and Push Docker Image / build-and-push (push) Canceled after 1h9m35s
初次提交
2026-09-06 08:19:02 +08:00

88 lines
2.7 KiB
TypeScript

import { db } from '~/drizzle/db'
import { systemSettings } from '~/drizzle/schema'
import { CacheService } from '../../../services/cacheService'
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 {
await cacheService.setSystemSettings(settings)
} catch (cacheError) {
console.warn('缓存系统设置失败:', cacheError)
}
// 隐藏敏感字段
if (settings.smtpPassword) {
settings.smtpPassword = '****************'
}
return settings
} catch (error) {
console.error('获取系统设置失败:', error)
throw createError({
statusCode: 500,
message: '获取系统设置失败'
})
}
})