Files
front/src/composables/systemParameters.ts
2026-09-09 16:43:43 +08:00

44 lines
1.6 KiB
TypeScript

import { fetchPublicSystemParameters, unwrapSystemSettings, type PublicSystemParameters } from '@/api/ops/systemSettings'
export const SystemParameterCode = {
DeviceOfflineSeconds: 'device_offline_seconds',
PageRefreshSeconds: 'page_refresh_seconds',
AlertRecoveryWaitSeconds: 'alert_recovery_wait_seconds',
MonitoringRetentionDays: 'monitoring_retention_days',
OperationLogRetentionDays: 'operation_log_retention_days',
LoginBackgroundRotationSeconds: 'login_background_rotation_seconds',
} as const
const requiredCodes = Object.values(SystemParameterCode)
let cached: PublicSystemParameters | null = null
let loading: Promise<PublicSystemParameters> | null = null
export async function loadSystemParameters(force = false): Promise<PublicSystemParameters> {
if (cached && !force) return cached
if (loading && !force) return loading
loading = fetchPublicSystemParameters().then((response) => {
const values = unwrapSystemSettings<PublicSystemParameters>(response)
if (!values || requiredCodes.some((code) => !Number.isInteger(values[code]))) {
throw new Error('系统参数未完整初始化')
}
cached = values
return values
})
try {
return await loading
} finally {
loading = null
}
}
export async function getSystemParameter(code: (typeof SystemParameterCode)[keyof typeof SystemParameterCode]): Promise<number> {
const values = await loadSystemParameters()
const value = values[code]
if (!Number.isInteger(value)) throw new Error(`系统参数 ${code} 不存在`)
return value
}
export function invalidateSystemParameters() {
cached = null
}