refactor(frontend): P1-P2 架构统一 — utils/hooks/api 模块化
P1: - utils/storage.ts、theme.ts、validate.ts + index 统一出口 - request/store/useTheme 接入 storage;清零 confirm/alert - PipelineManager、TerminalAuditTab 拆分子模块(<700行红线) P2: - api/monitor、api/deploy 子目录化 - hooks/api 扩展:servers/pipelines/alerts/audit/sessionCommands - PipelineManager、AuditOperationsTab 接入 TanStack Query
This commit is contained in:
1 parent
d19a03d427
commit
f45e203e21
196 files changed
+3228
-2018
No files matched your search
@@ -0,0 +1,51 @@
|
||||
// 本地存储封装 — token / 通用 KV // Logan.Li
|
||||
|
||||
import { useUserStore } from '@/store';
|
||||
|
||||
export const STORAGE_KEYS = {
|
||||
token: 'token',
|
||||
theme: 'aiops-theme',
|
||||
user: 'user-storage',
|
||||
} as const;
|
||||
|
||||
export function getItem(key: string): string | null {
|
||||
if (typeof window === 'undefined') return null;
|
||||
try {
|
||||
return localStorage.getItem(key);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function setItem(key: string, value: string): void {
|
||||
if (typeof window === 'undefined') return;
|
||||
try {
|
||||
localStorage.setItem(key, value);
|
||||
} catch {
|
||||
/* ignore quota errors */
|
||||
}
|
||||
}
|
||||
|
||||
export function removeItem(key: string): void {
|
||||
if (typeof window === 'undefined') return;
|
||||
try {
|
||||
localStorage.removeItem(key);
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
/** 读取 JWT — zustand 优先,localStorage 兜底 // Logan.Li */
|
||||
export function getToken(): string {
|
||||
return useUserStore.getState().token || getItem(STORAGE_KEYS.token) || '';
|
||||
}
|
||||
|
||||
export function setToken(token: string): void {
|
||||
setItem(STORAGE_KEYS.token, token);
|
||||
useUserStore.getState().setToken(token);
|
||||
}
|
||||
|
||||
export function clearToken(): void {
|
||||
removeItem(STORAGE_KEYS.token);
|
||||
useUserStore.getState().setToken(null);
|
||||
}
|
||||
Reference in new issue
Block a user