refactor: ♻️ 规范常量文件

This commit is contained in:
Ray.Hao
2025-09-19 21:03:38 +08:00
parent 4972173cf9
commit 7bda1a7cc4
2 changed files with 92 additions and 88 deletions

View File

@@ -1,3 +1,5 @@
import { STORAGE_KEYS, APP_PREFIX } from "@/constants";
/**
* 存储工具类
* 提供localStorage和sessionStorage操作方法
@@ -48,4 +50,52 @@ export class Storage {
static sessionRemove(key: string): void {
sessionStorage.removeItem(key);
}
/**
* 存储清理工具方法
*/
// 清理指定键的存储localStorage + sessionStorage
static clear(key: string): void {
localStorage.removeItem(key);
sessionStorage.removeItem(key);
}
// 批量清理存储
static clearMultiple(keys: string[]): void {
keys.forEach((key) => {
localStorage.removeItem(key);
sessionStorage.removeItem(key);
});
}
// 清理指定前缀的存储
static clearByPrefix(prefix: string): void {
// localStorage 清理
const localKeys = Object.keys(localStorage).filter((key) => key.startsWith(prefix));
localKeys.forEach((key) => localStorage.removeItem(key));
// sessionStorage 清理
const sessionKeys = Object.keys(sessionStorage).filter((key) => key.startsWith(prefix));
sessionKeys.forEach((key) => sessionStorage.removeItem(key));
}
/**
* 项目特定的清理便利方法
*/
// 清理所有项目相关的存储
static clearAllProject(): void {
const keys = Object.values(STORAGE_KEYS);
this.clearMultiple(keys);
}
// 清理特定分类的存储
static clearByCategory(category: "auth" | "system" | "ui" | "app"): void {
const prefix = `${APP_PREFIX}:${category}:`;
this.clearByPrefix(prefix);
}
// 获取所有项目相关的存储键
static getAllProjectKeys(): string[] {
return Object.values(STORAGE_KEYS);
}
}