You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
3.1 KiB
105 lines
3.1 KiB
import Vue from 'vue'
|
|
import store from '@/store'
|
|
import DataDict from '@/utils/dict'
|
|
import { sysdicttypeTypesApi } from '@/api/sysdicttype'
|
|
|
|
function searchDictByKey(dict, key) {
|
|
if (key == null && key == "") {
|
|
return null
|
|
}
|
|
try {
|
|
for (let i = 0; i < dict.length; i++) {
|
|
if (dict[i].key == key) {
|
|
return dict[i].value
|
|
}
|
|
}
|
|
} catch (e) {
|
|
return null
|
|
}
|
|
}
|
|
|
|
function install() {
|
|
let waiting = [];
|
|
let isRequesting = false;
|
|
let timer = null; // 定时器
|
|
|
|
Vue.use(DataDict, {
|
|
metas: {
|
|
'*': {
|
|
labelField: 'dictLabel',
|
|
valueField: 'dictValue',
|
|
request(dictMeta) {
|
|
const storeDict = searchDictByKey(store.getters.dict, dictMeta.type)
|
|
if (storeDict) {
|
|
return new Promise(resolve => { resolve(storeDict) })
|
|
} else {
|
|
return new Promise((resolve, reject) => {
|
|
waiting.push({
|
|
type: dictMeta.type,
|
|
resolve,
|
|
reject,
|
|
});
|
|
if (timer || isRequesting) {
|
|
return;
|
|
}
|
|
const startRequest = () => {
|
|
const requesting = [...waiting];
|
|
waiting = [];
|
|
timer = null;
|
|
isRequesting = true;
|
|
// 批量获取字典
|
|
sysdicttypeTypesApi(requesting.map(item => item.type)).then(res => {
|
|
const datas = {};
|
|
(res || []).forEach(item => {
|
|
if (datas[item.dictType]) {
|
|
datas[item.dictType].push(item);
|
|
} else {
|
|
datas[item.dictType] = [item];
|
|
}
|
|
});
|
|
requesting.forEach(item => {
|
|
store.dispatch('dict/setDict', { key: item.type, value: datas[item.type] || [] });
|
|
item.resolve(datas[item.type] || []);
|
|
const wIndex = waiting.findIndex(witem => witem.type == item.type);
|
|
if (wIndex > -1) {
|
|
waiting[wIndex].resolve(datas[item.type] || []);
|
|
waiting.splice(wIndex, 1);
|
|
}
|
|
});
|
|
isRequesting = false;
|
|
if (waiting.length > 0) {
|
|
startRequest();
|
|
}
|
|
}).catch(error => {
|
|
requesting.forEach(item => {
|
|
item.reject(error)
|
|
});
|
|
isRequesting = false;
|
|
if (waiting.length > 0) {
|
|
startRequest();
|
|
}
|
|
});
|
|
}
|
|
|
|
timer = setTimeout(() => {
|
|
startRequest();
|
|
}, 300);
|
|
|
|
|
|
// getDicts(dictMeta.type).then(res => {
|
|
// store.dispatch('dict/setDict', { key: dictMeta.type, value: res.data })
|
|
// resolve(res.data)
|
|
// }).catch(error => {
|
|
// reject(error)
|
|
// })
|
|
})
|
|
}
|
|
},
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
export default {
|
|
install,
|
|
} |