logwire-wms-ui-test
Version:
运匠科技wms-PDA基础组件库
90 lines (76 loc) • 2.3 kB
JavaScript
/*
* 字典数据
* @author 张文
* @date 2022-02-10
* */
import {dict} from "../api/common/Dict";
import {LocalStorage} from "./LocalStorage";
function checkKey(key){
return key.indexOf('dict_') === 0 ? key : 'dict_' + key
}
export const Dict = {
/**
* 设置字典数据
* @author 张文
* @param {Object|Array} data 需要设置的数据
* @param {String} key 需要设置字典的某一项,为空时将修改全部数据
* */
set(data, key = null) {
if (key) {
// window.lwWindowGlobalData.set('dict.'+key, data)
LocalStorage.set(checkKey(key), data)
}else {
for (let dataKey in data) {
LocalStorage.set(checkKey(dataKey), data[dataKey])
}
// window.lwWindowGlobalData.set('dict', data)
}
},
/**
* 根据字典key获取数据
* @author 张文
* @param {String} key 字典类型名称
* @return {*} 字典项的数据
*/
get(key = null) {
if (!key){
console.error("获取字典数据失败,传入字典key不能为空")
return ""
}
// return key ? window.lwWindowGlobalData.get('dict.'+key) : window.lwWindowGlobalData.get('dict')
return LocalStorage.get(checkKey(key))
},
/**
* 根据字典项的值获取对应项的名称
* @author 张文
* @param key 字典类型名称
* @param value 字典项的值
* @param def 无法获取值的缺省显示值
* @returns {String} 字典项的名称,如果未找到且默认值为空,则返回字典项的值
*/
getDictLabel(key, value, def) {
const list = this.get(key)
if (Array.isArray(list)) {
const item = list.find(i => i.value === value)
if (item && item.label) {
return item.label
}
}
return def || value
},
/**
* 异步获取字典数据
* @author 张文
* @param {String} key 字典类型名称,为空时返回全部数据
* @param {Boolean} loading 是否显示加载
* @returns {Promise} 字典项的数据
*/
asyncGet(key = null, loading) {
return new Promise((PR, PE) => {
dict.get(loading).then(res=>{
this.set(res)
PR(key ? this.get(key) : '')
}).catch(err => PE(err))
})
},
}