Files
wzdj/app/admin/screen/_lib/services/client-config-service.ts

102 lines
2.6 KiB
TypeScript

export interface DashboardConfig {
title: string
theme: "light" | "dark"
layout: "default" | "compact" | "wide"
refreshInterval: number
showQRCode: boolean
qrCodeUrl: string
customColors: {
primary: string
secondary: string
accent: string
}
}
export class ClientConfigService {
private static instance: ClientConfigService
private storageKey = "dashboard-config"
static getInstance(): ClientConfigService {
if (!ClientConfigService.instance) {
ClientConfigService.instance = new ClientConfigService()
}
return ClientConfigService.instance
}
private getDefaultConfig(): DashboardConfig {
return {
title: "数字大屏系统",
theme: "dark",
layout: "default",
refreshInterval: 5000,
showQRCode: true,
qrCodeUrl: "/qrcode.png",
customColors: {
primary: "#3b82f6",
secondary: "#64748b",
accent: "#06b6d4",
},
}
}
/* ---------- NEW STATIC WRAPPERS ---------- */
/** Return persisted or default config (static). */
static getConfig(): DashboardConfig {
return this.getInstance().getConfig()
}
/** Persist the given partial config (static). */
static async saveConfig(updates: Partial<DashboardConfig>): Promise<DashboardConfig> {
this.getInstance().updateConfig(updates)
return this.getConfig()
}
/** Clear stored config and return defaults (static). */
static async resetConfig(): Promise<DashboardConfig> {
this.getInstance().resetConfig()
return this.getConfig()
}
/* ---------- END NEW WRAPPERS ---------- */
getConfig(): DashboardConfig {
if (typeof window === "undefined") {
return this.getDefaultConfig()
}
try {
const stored = localStorage.getItem(this.storageKey)
if (stored) {
return { ...this.getDefaultConfig(), ...JSON.parse(stored) }
}
} catch (error) {
console.error("Failed to load config from localStorage:", error)
}
return this.getDefaultConfig()
}
updateConfig(updates: Partial<DashboardConfig>): void {
if (typeof window === "undefined") return
try {
const currentConfig = this.getConfig()
const newConfig = { ...currentConfig, ...updates }
localStorage.setItem(this.storageKey, JSON.stringify(newConfig))
} catch (error) {
console.error("Failed to save config to localStorage:", error)
}
}
resetConfig(): void {
if (typeof window === "undefined") return
try {
localStorage.removeItem(this.storageKey)
} catch (error) {
console.error("Failed to reset config:", error)
}
}
}
export const clientConfigService = ClientConfigService.getInstance()