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 | null = null export async function loadSystemParameters(force = false): Promise { if (cached && !force) return cached if (loading && !force) return loading loading = fetchPublicSystemParameters().then((response) => { const values = unwrapSystemSettings(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 { const values = await loadSystemParameters() const value = values[code] if (!Number.isInteger(value)) throw new Error(`系统参数 ${code} 不存在`) return value } export function invalidateSystemParameters() { cached = null }