-
Notifications
You must be signed in to change notification settings - Fork 807
/
dict.ts
55 lines (48 loc) · 1.81 KB
/
dict.ts
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
import { ref } from 'vue';
import { defineStore } from 'pinia';
import { type DictType, getDictData } from '@/api/demo/dict';
export const useDictStore = defineStore('dict', () => {
const dictMap = ref(new Map<string, LabelValueOptions>());
const dictValueLabelMap = ref(new Map<string, Map<any, string>>());
const dictPendingMap = ref(new Map<string, boolean>());
const fetchDict = (dictType: DictType | DictType[]) => {
const dictTypes = Array.isArray(dictType) ? dictType : [dictType];
const promises = dictTypes.map(async (type) => {
if (dictMap.value.has(type) && !dictPendingMap.value.has(type)) {
return dictMap.value.get(type)!;
}
dictMap.value.set(type, []);
dictPendingMap.value.set(type, true);
const res = await getDictData({ type }).finally(() => dictPendingMap.value.delete(type));
dictMap.value.set(type, res);
dictValueLabelMap.value.set(type, new Map(res.map((item) => [item.value, item.label])));
return res;
});
return Promise.all(promises);
};
const dictData = new Proxy({} as Record<DictType, LabelValueOptions>, {
get(_, prop: DictType) {
if (prop.startsWith('__v_')) {
// console.trace('get', prop);
return;
}
if (dictMap.value.has(prop)) {
return dictMap.value.get(prop);
}
if (!dictPendingMap.value.has(prop)) {
fetchDict(prop);
}
return dictMap.value.get(prop);
},
});
const showDictLabel = (dictType: DictType, code: any) => {
dictData[dictType];
return dictValueLabelMap.value.get(dictType)?.get(code) || '';
};
const dictPending = new Proxy({} as Record<DictType, boolean>, {
get(_, prop: DictType) {
return dictPendingMap.value.get(prop) || false;
},
});
return { dictData, dictPending, fetchDict, showDictLabel };
});