69 lines
1.9 KiB
JavaScript
69 lines
1.9 KiB
JavaScript
/**
|
|
* 封装字典工具类
|
|
*/
|
|
const $dict = {
|
|
instance: null,
|
|
url: "/admin/dictionary/queryValsByCodeList",
|
|
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) {
|
|
return this.instance && this.instance.post(this.url, null, {
|
|
withoutToken: true,
|
|
params: {
|
|
codeList: code.toString()
|
|
}
|
|
}).then((res) => {
|
|
if (res && res.data) {
|
|
let cacheDicts = {},
|
|
meta = {};
|
|
this.dicts().map((e) => (cacheDicts[e.key] = e));
|
|
res.data.map((e) => (meta[e.key] = e));
|
|
let dicts = {...cacheDicts, ...meta};
|
|
uni.setStorageSync('dicts', Object.values(dicts));
|
|
}
|
|
})
|
|
},
|
|
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.dictName == label);
|
|
return item ? item.dictValue : 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.dictValue == value);
|
|
return item ? item.dictName : 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 $dict
|