import { ACCESSTOKEN, config } from '@/common/config.js' class Dict { constructor(dict) { this.dict = dict; } async init(names, vm) { const ps = []; names.forEach((name) => { if (vm.prototype.$u.vuex('dictList')) { vm.set(this.dict, name, vm.prototype.$u.vuex('dictList')); } else { vm.set(this.dict, name, []); ps.push( uni.request({ url: config.baseUrl + '/system/data/list', header: { 'Accept': 'application/json, text/plain, */*', [ACCESSTOKEN]: 'Bearer' + " " + uni.getStorageSync("sessionToken") }, data: { dictType: name, status: 0 }, complete: (res) => { if (res.statusCode === 200) { if (res.data.code === 401) { // vm.prototype.$u.toast(res.data.msg || "未知错误"); uni.removeStorageSync("sessionToken") setTimeout(() => { uni.reLaunch({ url: '/pages/index/index' }) }, 500) } else if (res.data.code === 200) { this.dict[name] = Object.freeze(res.data.rows); vm.prototype.$u.vuex('dictList', { [name]: Object.freeze(res.data.rows) }) } else { vm.$u.toast(res.data.msg || "未知错误"); } } } }) ); } }); 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, Vue); } }, methods: { getDicLabel(key, value) { let cur = this.dict[key]?.find(item => item.dictValue == value); if (!cur) { return ''; } return cur.dictLabel }, getDict(key, value) { let cur = this.dict[key]?.find(item => item.dictValue == value); if (!cur) { return { dictLabel: '' }; } return cur } } }); }; export default { install };