- 重命名 13 个 API 文件,去掉 -api 后缀 * src/api/auth-api.ts auth.ts * src/api/codegen-api.ts codegen.ts * src/api/file-api.ts file.ts * src/api/system/*.ts (9个文件) - 批量更新 50+ 处 API 引用路径 - 修复 websocket 和枚举导入路径 - 确保零编码问题(使用 UTF-8 编码)
80 lines
2.0 KiB
TypeScript
80 lines
2.0 KiB
TypeScript
import { store } from "@/store";
|
|
import DictAPI, { type DictItemOption } from "@/api/system/dict";
|
|
import { STORAGE_KEYS } from "@/constants";
|
|
|
|
export const useDictStore = defineStore("dict", () => {
|
|
// 字典数据缓存
|
|
const dictCache = useStorage<Record<string, DictItemOption[]>>(STORAGE_KEYS.DICT_CACHE, {});
|
|
|
|
// 请求队列(防止重复请求)
|
|
const requestQueue: Record<string, Promise<void>> = {};
|
|
|
|
/**
|
|
* 缓存字典数据
|
|
* @param dictCode 字典编码
|
|
* @param data 字典项列表
|
|
*/
|
|
const cacheDictItems = (dictCode: string, data: DictItemOption[]) => {
|
|
dictCache.value[dictCode] = data;
|
|
};
|
|
|
|
/**
|
|
* 加载字典数据(如果缓存中没有则请求)
|
|
* @param dictCode 字典编码
|
|
*/
|
|
const loadDictItems = async (dictCode: string) => {
|
|
if (dictCache.value[dictCode]) return;
|
|
// 防止重复请求
|
|
if (!requestQueue[dictCode]) {
|
|
requestQueue[dictCode] = DictAPI.getDictItems(dictCode)
|
|
.then((data) => {
|
|
cacheDictItems(dictCode, data);
|
|
Reflect.deleteProperty(requestQueue, dictCode);
|
|
})
|
|
.catch((error) => {
|
|
// 请求失败,清理队列,允许重试
|
|
Reflect.deleteProperty(requestQueue, dictCode);
|
|
throw error;
|
|
});
|
|
}
|
|
await requestQueue[dictCode];
|
|
};
|
|
|
|
/**
|
|
* 获取字典项列表
|
|
* @param dictCode 字典编码
|
|
* @returns 字典项列表
|
|
*/
|
|
const getDictItems = (dictCode: string): DictItemOption[] => {
|
|
return dictCache.value[dictCode] || [];
|
|
};
|
|
|
|
/**
|
|
* 移除指定字典项
|
|
* @param dictCode 字典编码
|
|
*/
|
|
const removeDictItem = (dictCode: string) => {
|
|
if (dictCache.value[dictCode]) {
|
|
Reflect.deleteProperty(dictCache.value, dictCode);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 清空字典缓存
|
|
*/
|
|
const clearDictCache = () => {
|
|
dictCache.value = {};
|
|
};
|
|
|
|
return {
|
|
loadDictItems,
|
|
getDictItems,
|
|
removeDictItem,
|
|
clearDictCache,
|
|
};
|
|
});
|
|
|
|
export function useDictStoreHook() {
|
|
return useDictStore(store);
|
|
}
|