//src\store\modules\dicts.js
import { getDictParamTableBatch } from '@/api/common/index'
import store from '@/store'
import Vue from 'vue'
class Dict {
constructor(dict) {
this.dict = dict
}
async init(names) {
const ps = []
const hash = {}
names.forEach((name) => {
if (store.getters.dicts[name]) {
Vue.set(this.dict, name, store.getters.dicts[name])
} else {
Vue.set(this.dict, name, [])
hash[name] = name
}
})
const dictCodeList = Object.keys(hash).map(v => hash[v])
ps.push(
getDictParamTableBatch({ dictCodeList }).then((res) => {
res.data.forEach(item => {
this.dict[item.dictCode] = Object.freeze(item.dictParamTables)
store.commit('dicts/SET_DICTS', {
label: item.dictCode,
value: Object.freeze(item.dictParamTables)
})
})
})
)
await Promise.all(ps)
}
}
const install = function(Vue) {
Vue.mixin({
data() {
if (
this.$options.dicts instanceof Array &&
this.$options.dicts.length > 0
) {
return { dict: {}}
} else {
return {}
}
},
created() {
if (
this.$options.dicts instanceof Array &&
this.$options.dicts.length > 0
) {
new Dict(this.dict).init(this.$options.dicts)
}
}
})
}
export default { install }