曹睿
2025-04-25 ec1bef3a37e8dcdf22f1bf52e7c272a18306f4b9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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,
  };
});