feat: 新增字典和字典展示功能

新增字典和字典展示功能
This commit is contained in:
Theo
2024-11-24 15:46:25 +08:00
parent 93507af63a
commit 1f8cda13b9
12 changed files with 431 additions and 16 deletions

38
src/store/modules/dict.ts Normal file
View 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,
};
});