Files
dvcp_v2_wxcp_app/library/common/dict.js

69 lines
1.9 KiB
JavaScript
Raw Normal View History

2021-11-15 10:29:05 +08:00
/**
* 封装字典工具类
*/
const $dict = {
2022-03-15 10:03:54 +08:00
instance: null,
2021-11-15 10:29:05 +08:00
url: "/admin/dictionary/queryValsByCodeList",
2022-03-15 10:03:54 +08:00
init(config) {
this.instance = config?.instance
this.url = config?.url || this.url
},
dicts() {
2022-08-09 14:54:46 +08:00
let dicts = uni.getStorageSync('dicts') || null
typeof dicts == "string" ? (dicts = JSON.parse(dicts)) : dicts
return dicts || [];
2021-11-15 10:29:05 +08:00
},
load(...code) {
2022-03-15 10:03:54 +08:00
return this.instance && this.instance.post(this.url, null, {
withoutToken: true,
2021-11-15 10:29:05 +08:00
params: {
2022-03-15 10:03:54 +08:00
codeList: code.toString()
2021-11-15 10:29:05 +08:00
}
}).then((res) => {
2022-03-15 10:03:54 +08:00
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));
}
2021-11-15 10:29:05 +08:00
})
},
getDict(key) {
2022-03-15 10:03:54 +08:00
if (this.dicts().length) {
let dict = this.dicts().find((e) => e.key == key);
return dict ? dict.values : [];
} else return [];
2021-11-15 10:29:05 +08:00
},
getValue(key, label) {
2022-03-15 10:03:54 +08:00
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;
2021-11-15 10:29:05 +08:00
},
getLabel(key, value) {
2022-03-15 10:03:54 +08:00
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 : '';
2021-11-15 10:29:05 +08:00
},
getColor(key, value) {
2022-03-15 10:03:54 +08:00
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;
}
2021-11-15 10:29:05 +08:00
}
export default $dict