feat: ✨ 新增字典和字典展示功能
新增字典和字典展示功能
This commit is contained in:
38
src/store/modules/dict.ts
Normal file
38
src/store/modules/dict.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { defineStore } from "pinia";
|
||||
import DictAPI, { type DictVO, type DictData } from "@/api/system/dict";
|
||||
import { setDictCache, getDictCache } from "@/utils/cache";
|
||||
export const useDictStore = defineStore("dict", () => {
|
||||
const dictionary = ref<Record<string, DictData[]>>(getDictCache());
|
||||
|
||||
const setDictionary = (dict: DictVO) => {
|
||||
dictionary.value[dict.dictCode] = dict.dictDataList;
|
||||
setDictCache(dictionary.value);
|
||||
};
|
||||
|
||||
const loadDictionaries = async () => {
|
||||
const dictList = await DictAPI.getList();
|
||||
dictList.forEach(setDictionary);
|
||||
};
|
||||
|
||||
const getDictionary = (dictCode: string): DictData[] => {
|
||||
return dictionary.value[dictCode] || [];
|
||||
};
|
||||
|
||||
const clearDictionaryCache = () => {
|
||||
dictionary.value = {};
|
||||
};
|
||||
|
||||
const updateDictionaryCache = async () => {
|
||||
clearDictionaryCache(); // 先清除旧缓存
|
||||
await loadDictionaries(); // 重新加载最新字典数据
|
||||
};
|
||||
|
||||
return {
|
||||
dictionary,
|
||||
setDictionary,
|
||||
loadDictionaries,
|
||||
getDictionary,
|
||||
clearDictionaryCache,
|
||||
updateDictionaryCache,
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user