160 lines
5.7 KiB
TypeScript
160 lines
5.7 KiB
TypeScript
import request, { getServerApiURL } from './request'
|
|
import { cache } from 'react'
|
|
|
|
export interface PublicConfigItem {
|
|
configKey: string
|
|
configValue: string
|
|
}
|
|
|
|
/** 主题强类型字段 → page.theme.* 后缀(与 theme_config 表列一一对应) */
|
|
const THEME_KEYS = [
|
|
'accentColor',
|
|
'accentSoftColor',
|
|
'bgColor',
|
|
'bgSoftColor',
|
|
'darkAccentColor',
|
|
'darkAccentSoftColor',
|
|
'darkBgColor',
|
|
'darkBgSoftColor',
|
|
'grainOpacity',
|
|
'glowsEnabled',
|
|
'bgImageUrl',
|
|
'bgImageStyle',
|
|
'bgImageBlur',
|
|
'bgImageOpacity',
|
|
'darkBgImageOpacity',
|
|
'customCss'
|
|
] as const
|
|
|
|
/** site_access 强类型字段 → page.*.enable 遗留键 */
|
|
const PAGE_KEYS: Array<[string, string]> = [
|
|
['love', 'page.love.enable'],
|
|
['letter', 'page.letter.enable'],
|
|
['now', 'page.now.enable'],
|
|
['about', 'page.about.enable'],
|
|
['footprints', 'page.footprints.enable'],
|
|
['pond', 'page.pond.enable']
|
|
]
|
|
|
|
/** 统一转字符串:null/undefined → '',布尔 → 'true'/'false',其余 String() */
|
|
function toText(value: unknown): string {
|
|
if (value === null || value === undefined) return ''
|
|
if (typeof value === 'boolean') return value ? 'true' : 'false'
|
|
return String(value)
|
|
}
|
|
|
|
/** JSON 型键:内容为空时输出空串,复现“配置缺失”语义以触发前端兜底 */
|
|
function toJson(value: unknown, isEmpty: (parsed: any) => boolean): string {
|
|
if (!isEmpty(value)) return JSON.stringify(value)
|
|
return ''
|
|
}
|
|
|
|
/** 把 /config/bootstrap 的强类型聚合摊平为遗留 {configKey, configValue} 字符串数组 */
|
|
function flattenBootstrap(payload: any): PublicConfigItem[] {
|
|
const data = payload?.data ?? {}
|
|
const site = data.site ?? {}
|
|
const beian = site.beian ?? {}
|
|
const amap = site.amap ?? {}
|
|
const theme = data.theme ?? {}
|
|
const pages = data.pages ?? {}
|
|
const love = data.love ?? {}
|
|
const couple = love.couple ?? {}
|
|
const stats = love.stats ?? {}
|
|
|
|
const items: PublicConfigItem[] = []
|
|
const push = (configKey: string, configValue: unknown) => {
|
|
items.push({ configKey, configValue: toText(configValue) })
|
|
}
|
|
|
|
// 站点品牌与全局文案
|
|
push('site.logo.url', site.logoUrl)
|
|
push('site.logo.text', site.logoText)
|
|
push('site.favicon.url', site.faviconUrl)
|
|
push('site.title.default', site.titleDefault)
|
|
push('site.title.about', site.titleAbout)
|
|
push('page.home.welcomeText', site.welcomeText)
|
|
push('page.home.welcomeSubtitle', site.welcomeSubtitle)
|
|
push('page.home.heroTitle', site.heroTitle)
|
|
push('page.home.eyebrowTag', site.eyebrowTag)
|
|
push('page.home.heroDescription', site.heroDescription)
|
|
push('page.home.heroBgUrl', site.heroBgUrl)
|
|
push('site.beian.icp', beian.icp)
|
|
push('site.beian.icpUrl', beian.icpUrl)
|
|
push('site.beian.police', beian.police)
|
|
push('site.beian.policeUrl', beian.policeUrl)
|
|
push('page.footprint.amap.key', amap.key)
|
|
push('page.footprint.amap.securityJsCode', amap.securityJsCode)
|
|
|
|
// 外观主题
|
|
push('page.theme.enabled', theme.enabled)
|
|
THEME_KEYS.forEach((key) => push(`page.theme.${key}`, theme[key]))
|
|
|
|
// 页面可见性(口令不回传:由后端 /config/verify-passcode 单独校验,避免泄露)
|
|
PAGE_KEYS.forEach(([field, configKey]) => push(configKey, pages[field]))
|
|
|
|
// 关于作者 / 此时此刻(由关系表实时拼装,保持 JSON 字符串形态)
|
|
push('page.about.profile', toJson(data.about, (v) => !v || Object.keys(v).length === 0))
|
|
push('now.content.json', toJson(data.now, (v) => !v || Object.keys(v).length === 0))
|
|
|
|
// 恋爱域(伴侣信息未配置时输出空串,前端按空态渲染,不做兜底)
|
|
push('love.couple.info', toJson(couple, (v) => !v.name && !v.avatar))
|
|
push('love.stats.startDate', stats.startDate)
|
|
push('love.stats.citiesCount', stats.citiesCount)
|
|
push('love.stats.flightDistance', stats.flightDistance)
|
|
push('love.stats.movieCount', stats.movieCount)
|
|
push('love.stats.mealCount', stats.mealCount)
|
|
push('page.love.heroBgUrl', love.heroBgUrl)
|
|
push('page.love.atmospheres', toJson(love.atmospheres, (v) => !Array.isArray(v) || v.length === 0))
|
|
|
|
return items
|
|
}
|
|
|
|
export async function getPublicConfig() {
|
|
const response = await request.get('/config/bootstrap')
|
|
return {
|
|
...response,
|
|
data: {
|
|
code: response.data?.code,
|
|
msg: response.data?.msg,
|
|
data: flattenBootstrap(response.data)
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function fetchPublicConfigForServer(init?: Parameters<typeof fetch>[1]) {
|
|
try {
|
|
const response = await fetch(getServerApiURL('/config/bootstrap'), {
|
|
...init,
|
|
// 容错超时防刷屏
|
|
signal: AbortSignal.timeout(3000)
|
|
})
|
|
if (!response.ok) {
|
|
return { code: response.status, data: [] }
|
|
}
|
|
const payload = await response.json()
|
|
return { ...payload, data: flattenBootstrap(payload) }
|
|
} catch {
|
|
// 静默兜底,避免打印庞大的 Socket 异常日志
|
|
return { code: 500, data: [] }
|
|
}
|
|
}
|
|
|
|
export const getPublicConfigSnapshot = cache(async (): Promise<PublicConfigItem[]> => {
|
|
const payload = await fetchPublicConfigForServer({ next: { revalidate: 10 } })
|
|
if (payload.code !== 200 || !Array.isArray(payload.data)) return []
|
|
return payload.data.filter((item: unknown): item is PublicConfigItem => {
|
|
if (!item || typeof item !== 'object') return false
|
|
const candidate = item as Partial<PublicConfigItem>
|
|
return typeof candidate.configKey === 'string' && typeof candidate.configValue === 'string'
|
|
})
|
|
})
|
|
|
|
/** 校验全站私密口令(服务端只校验、不回传口令,避免泄露) */
|
|
export async function verifyPasscode(code: string): Promise<{ code: number; msg?: string }> {
|
|
try {
|
|
const response = await request.post('/config/verify-passcode', { code })
|
|
return response.data ?? { code: 500 }
|
|
} catch {
|
|
return { code: 500, msg: '口令校验服务暂不可用' }
|
|
}
|
|
} |