/** * 封装字典工具类 */ const $dictHlj = { instance: null, url: "/system/dictData/types/", init(config) { this.instance = config?.instance this.url = config?.url || this.url }, dicts() { let dicts = uni.getStorageSync('dicts') || null typeof dicts == "string" ? (dicts = JSON.parse(dicts)) : dicts return dicts || []; }, load(...code) { var url = this.url + code.toString() return this.instance && this.instance.get(url, { withoutToken: true, }).then((res) => { console.log(res) if (res && res.data) { let dicts=[] for (var i in res.data) { var e = { key: res.data[i][0].dictType, values: res.data[res.data[i][0].dictType] } dicts.push(e) } this.dicts() uni.setStorageSync('dicts', dicts); } }).catch((err) => { console.log(err) }) }, getDict(key) { if (this.dicts().length) { let dict = this.dicts().find((e) => e.key == key); return dict ? dict.values : []; } else return []; }, getValue(key, label) { if (this.dicts().length) { let dict = this.dicts().find((e) => e.key == key); if (dict) { let item = dict.values.find((v) => v.label == label); return item ? item.value : label; } else return label; } else return label; }, getLabel(key, value) { if (this.dicts().length) { let dict = this.dicts().find((e) => e.key == key); if (dict) { let item = dict.values.find((v) => v.value == value); return item ? item.label : value; } else return value ? value : ''; } else return value ? value : ''; }, getColor(key, value) { if (this.dicts().length) { let dict = this.dicts().find((e) => e.key == key); if (dict) { let item = dict.values.find((v) => v.dictValue == value); return item ? item.dictColor : value; } else return value; } else return value; } } export default $dictHlj