zjk
2026-02-04 b4dc99a92c8fbdfefa48870fde72042e0d1e168a
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { fetchDictList } from '@/service'
import { session } from '@/utils'
 
export const useDictStore = defineStore('dict-store', {
  state: () => {
    return {
      dictMap: {} as DictMap,
      isInitDict: false,
    }
  },
  actions: {
    async dict(code: string) {
      // 调用前初始化
      if (!this.dictMap) {
        this.initDict()
      }
 
      const targetDict = await this.getDict(code)
 
      return {
        data: () => targetDict,
        enum: () => Object.fromEntries(targetDict.map(({ value, label }) => [value, label])),
        valueMap: () => Object.fromEntries(targetDict.map(({ value, ...data }) => [value, data])),
        labelMap: () => Object.fromEntries(targetDict.map(({ label, ...data }) => [label, data])),
      }
    },
    async getDict(code: string) {
      const isExist = Reflect.has(this.dictMap, code)
 
      if (isExist) {
        return this.dictMap[code]
      }
      else {
        return await this.getDictByNet(code)
      }
    },
 
    async getDictByNet(code: string) {
      const { data, isSuccess } = await fetchDictList(code)
      if (isSuccess) {
        Reflect.set(this.dictMap, code, data)
        // 同步至session
        session.set('dict', this.dictMap)
        return data
      }
      else {
        throw new Error(`Failed to get ${code} dictionary from network, check ${code} field or network`)
      }
    },
    initDict() {
      const dict = session.get('dict')
      if (dict) {
        Object.assign(this.dictMap, dict)
      }
      this.isInitDict = true
    },
  },
})