Files
jwl-manage-web/src/components/DictData/index.js

54 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-03-21 00:53:28 +08:00
import Vue from 'vue';
import store from '@/store';
import DataDict from '@/utils/dict';
import { getDicts as getDicts } from '@/api/system/dict/data';
2023-02-15 09:17:05 +08:00
function searchDictByKey(dict, key) {
2023-03-21 00:53:28 +08:00
if (key == null && key == '') {
return null;
2023-02-15 09:17:05 +08:00
}
try {
for (let i = 0; i < dict.length; i++) {
if (dict[i].key == key) {
2023-03-21 00:53:28 +08:00
return dict[i].value;
2023-02-15 09:17:05 +08:00
}
}
} catch (e) {
2023-03-21 00:53:28 +08:00
return null;
2023-02-15 09:17:05 +08:00
}
}
function install() {
Vue.use(DataDict, {
metas: {
'*': {
labelField: 'dictLabel',
valueField: 'dictValue',
request(dictMeta) {
2023-03-21 00:53:28 +08:00
const storeDict = searchDictByKey(store.getters.dict, dictMeta.type);
2023-02-15 09:17:05 +08:00
if (storeDict) {
2023-03-21 00:53:28 +08:00
return new Promise((resolve) => {
resolve(storeDict);
});
2023-02-15 09:17:05 +08:00
} else {
return new Promise((resolve, reject) => {
2023-03-21 00:53:28 +08:00
getDicts(dictMeta.type)
.then((res) => {
store.dispatch('dict/setDict', { key: dictMeta.type, value: res.data });
resolve(res.data);
})
.catch((error) => {
reject(error);
});
});
2023-02-15 09:17:05 +08:00
}
2023-03-21 00:53:28 +08:00
}
}
}
});
2023-02-15 09:17:05 +08:00
}
export default {
2023-03-21 00:53:28 +08:00
install
};