懒羊羊
2024-01-31 e57a8990ae56f657a59c435a0613c5f7a8728003
提交 | 用户 | 时间
e57a89 1 import Vue from 'vue'
2 import store from '@/store'
3 import DataDict from '@/utils/dict'
4 import { getDicts as getDicts } from '@/api/system/dict/data'
5
6 function searchDictByKey(dict, key) {
7   if (key == null && key == "") {
8     return null
9   }
10   try {
11     for (let i = 0; i < dict.length; i++) {
12       if (dict[i].key == key) {
13         return dict[i].value
14       }
15     }
16   } catch (e) {
17     return null
18   }
19 }
20
21 function install() {
22   Vue.use(DataDict, {
23     metas: {
24       '*': {
25         labelField: 'dictLabel',
26         valueField: 'dictValue',
27         request(dictMeta) {
28           const storeDict = searchDictByKey(store.getters.dict, dictMeta.type)
29           if (storeDict) {
30             return new Promise(resolve => { resolve(storeDict) })
31           } else {
32             return new Promise((resolve, reject) => {
33               getDicts(dictMeta.type).then(res => {
34                 store.dispatch('dict/setDict', { key: dictMeta.type, value: res.data })
35                 resolve(res.data)
36               }).catch(error => {
37                 reject(error)
38               })
39             })
40           }
41         },
42       },
43     },
44   })
45 }
46
47 export default {
48   install,
49 }