Files
aiops/frontend/src/utils/storage.ts
T
Logan 8d46e8f352 feat: 新增日志中心、功能优化与代码重构
- 新增 log_datasource.go / log_proxy.go / user_api_token.go 后端控制器
- 新增 log_datasource.go model
- 新增 monitor/log-center.ts API 模块
- 新增 LogCenter 前端组件及页面
- 重构 deployment 相关组件 (HPDA/UpdateImageModal/bundle-yaml 等)
- 新增 ResetPasswordDialog / code-snippet-block / copy-button 等 UI 组件
- 重构多个 k8s 相关组件 (TopologyView/ResourceVisualEditor/PodFileBrowser 等)
- 更新多个 API 和工具函数 (auth.ts/monitor/index.ts/request.ts 等)
- 更新静态资源 (index.html + 新 JS/CSS bundle)
- 删除废弃静态资源文件
2026-06-26 23:18:05 +08:00

83 lines
2.2 KiB
TypeScript

// 本地存储封装 — token / 通用 KV // Logan.Li
import { useUserStore } from '@/store';
export const STORAGE_KEYS = {
token: 'token',
theme: 'aiops-theme',
user: 'user-storage',
/** API 访问密钥明文(本机 localStorage,生成/重新生成后写入,吊销时清除)// Logan.Li */
apiAccessKey: 'aiops_api_access_key',
} 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);
}
/** 本机缓存的 API 密钥明文(用于 CI/CD curl 自动填入)// Logan.Li */
export function getSessionApiAccessKey(): string {
const cached = getItem(STORAGE_KEYS.apiAccessKey);
if (cached) return cached;
// 迁移旧版 sessionStorage 缓存 // Logan.Li
if (typeof window === 'undefined') return '';
try {
const legacy = sessionStorage.getItem(STORAGE_KEYS.apiAccessKey);
if (legacy) {
setItem(STORAGE_KEYS.apiAccessKey, legacy);
sessionStorage.removeItem(STORAGE_KEYS.apiAccessKey);
return legacy;
}
} catch {
/* ignore */
}
return '';
}
export function setSessionApiAccessKey(token: string): void {
if (token) setItem(STORAGE_KEYS.apiAccessKey, token);
else removeItem(STORAGE_KEYS.apiAccessKey);
if (typeof window === 'undefined') return;
try {
sessionStorage.removeItem(STORAGE_KEYS.apiAccessKey);
} catch {
/* ignore */
}
}