refactor(storage): ♻️ 简化缓存管理方式,统一使用Storage类直接操作token和缓存

This commit is contained in:
Ray.Hao
2025-05-19 14:09:46 +08:00
parent a6d76d17d8
commit 2a3d2543ee
10 changed files with 71 additions and 47 deletions

37
src/utils/storage.ts Normal file
View File

@@ -0,0 +1,37 @@
/**
* 存储工具类
* 提供localStorage和sessionStorage操作方法
*/
export class Storage {
/**
* localStorage 存储
*/
static set(key: string, value: any): void {
localStorage.setItem(key, JSON.stringify(value));
}
static get<T>(key: string, defaultValue?: T): T {
const value = localStorage.getItem(key);
return value ? JSON.parse(value) : defaultValue;
}
static remove(key: string): void {
localStorage.removeItem(key);
}
/**
* sessionStorage 存储
*/
static sessionSet(key: string, value: any): void {
sessionStorage.setItem(key, JSON.stringify(value));
}
static sessionGet<T>(key: string, defaultValue?: T): T {
const value = sessionStorage.getItem(key);
return value ? JSON.parse(value) : defaultValue;
}
static sessionRemove(key: string): void {
sessionStorage.removeItem(key);
}
}