提交 | 用户 | 时间
|
820397
|
1 |
import { defineStore } from 'pinia' |
H |
2 |
import { store } from '../index' |
|
3 |
// @ts-ignore |
|
4 |
import { DictDataVO } from '@/api/system/dict/types' |
|
5 |
import { CACHE_KEY, useCache } from '@/hooks/web/useCache' |
|
6 |
const { wsCache } = useCache('sessionStorage') |
|
7 |
import { getSimpleDictDataList } from '@/api/system/dict/dict.data' |
|
8 |
|
|
9 |
export interface DictValueType { |
|
10 |
value: any |
|
11 |
label: string |
|
12 |
clorType?: string |
|
13 |
cssClass?: string |
|
14 |
} |
|
15 |
export interface DictTypeType { |
|
16 |
dictType: string |
|
17 |
dictValue: DictValueType[] |
|
18 |
} |
|
19 |
export interface DictState { |
|
20 |
dictMap: Map<string, any> |
|
21 |
isSetDict: boolean |
|
22 |
} |
|
23 |
|
|
24 |
export const useDictStore = defineStore('dict', { |
|
25 |
state: (): DictState => ({ |
|
26 |
dictMap: new Map<string, any>(), |
|
27 |
isSetDict: false |
|
28 |
}), |
|
29 |
getters: { |
|
30 |
getDictMap(): Recordable { |
|
31 |
const dictMap = wsCache.get(CACHE_KEY.DICT_CACHE) |
|
32 |
if (dictMap) { |
|
33 |
this.dictMap = dictMap |
|
34 |
} |
|
35 |
return this.dictMap |
|
36 |
}, |
|
37 |
getIsSetDict(): boolean { |
|
38 |
return this.isSetDict |
|
39 |
} |
|
40 |
}, |
|
41 |
actions: { |
|
42 |
async setDictMap() { |
|
43 |
const dictMap = wsCache.get(CACHE_KEY.DICT_CACHE) |
|
44 |
if (dictMap) { |
|
45 |
this.dictMap = dictMap |
|
46 |
this.isSetDict = true |
|
47 |
} else { |
|
48 |
const res = await getSimpleDictDataList() |
|
49 |
// 设置数据 |
|
50 |
const dictDataMap = new Map<string, any>() |
|
51 |
res.forEach((dictData: DictDataVO) => { |
|
52 |
// 获得 dictType 层级 |
|
53 |
const enumValueObj = dictDataMap[dictData.dictType] |
|
54 |
if (!enumValueObj) { |
|
55 |
dictDataMap[dictData.dictType] = [] |
|
56 |
} |
|
57 |
// 处理 dictValue 层级 |
|
58 |
dictDataMap[dictData.dictType].push({ |
|
59 |
value: dictData.value, |
|
60 |
label: dictData.label, |
|
61 |
colorType: dictData.colorType, |
|
62 |
cssClass: dictData.cssClass |
|
63 |
}) |
|
64 |
}) |
|
65 |
this.dictMap = dictDataMap |
|
66 |
this.isSetDict = true |
|
67 |
wsCache.set(CACHE_KEY.DICT_CACHE, dictDataMap, { exp: 60 }) // 60 秒 过期 |
|
68 |
} |
|
69 |
}, |
|
70 |
getDictByType(type: string) { |
|
71 |
if (!this.isSetDict) { |
|
72 |
this.setDictMap() |
|
73 |
} |
|
74 |
return this.dictMap[type] |
|
75 |
}, |
|
76 |
async resetDict() { |
|
77 |
wsCache.delete(CACHE_KEY.DICT_CACHE) |
|
78 |
const res = await getSimpleDictDataList() |
|
79 |
// 设置数据 |
|
80 |
const dictDataMap = new Map<string, any>() |
|
81 |
res.forEach((dictData: DictDataVO) => { |
|
82 |
// 获得 dictType 层级 |
|
83 |
const enumValueObj = dictDataMap[dictData.dictType] |
|
84 |
if (!enumValueObj) { |
|
85 |
dictDataMap[dictData.dictType] = [] |
|
86 |
} |
|
87 |
// 处理 dictValue 层级 |
|
88 |
dictDataMap[dictData.dictType].push({ |
|
89 |
value: dictData.value, |
|
90 |
label: dictData.label, |
|
91 |
colorType: dictData.colorType, |
|
92 |
cssClass: dictData.cssClass |
|
93 |
}) |
|
94 |
}) |
|
95 |
this.dictMap = dictDataMap |
|
96 |
this.isSetDict = true |
|
97 |
wsCache.set(CACHE_KEY.DICT_CACHE, dictDataMap, { exp: 60 }) // 60 秒 过期 |
|
98 |
} |
|
99 |
} |
|
100 |
}) |
|
101 |
|
|
102 |
export const useDictStoreWithOut = () => { |
|
103 |
return useDictStore(store) |
|
104 |
} |