erp-el-element/dict/index.js

125 lines
3.9 KiB
JavaScript
Raw Normal View History

2024-06-20 14:42:44 +08:00
import Dict from './Dict'
import { mergeOptions } from './DictOptions'
export default function (Vue, options) {
mergeOptions(options)
Vue.mixin({
data() {
if (this.$options === undefined || this.$options.dicts === undefined || this.$options.dicts === null) {
return {}
}
2024-06-20 14:52:37 +08:00
2024-06-20 14:42:44 +08:00
const dict = new Dict()
dict.owner = this
return {
dict,
dictstatus: false,
requesttime:0,
}
},
created() {
if (!(this.dict instanceof Dict)) {
return
}
let dicttime=sessionStorage.getItem("dicttime");
if(dicttime&&new Date().getTime()-Number(dicttime)>1000*60*720){
options.onCreated && options.onCreated(this.dict)
2024-06-20 14:53:19 +08:00
this.dict.init(this.$options.dicts,this.$options.dictapiurl).then(() => {
2024-06-20 14:42:44 +08:00
this.dictstatus = true;
sessionStorage.setItem("dicttime",new Date().getTime());
options.onReady && options.onReady(this.dict)
this.$nextTick(() => {
this.$emit('dictReady', this.dict);
if (this.$options.methods && this.$options.methods.onDictReady instanceof Function) {
this.$options.methods.onDictReady.call(this, this.dict)
}
})
})
}
const dictdata = sessionStorage.getItem("dictdata");
if (dictdata) {
let sedata = JSON.parse(dictdata);
const find=this.$options.dicts.find((f)=>{
let findchild=Object.keys(sedata).find((h)=>h.indexOf(f)>-1);
return !findchild;
});
if(find){
options.onCreated && options.onCreated(this.dict)
this.dict.init(this.$options.dicts).then(() => {
this.dictstatus = true;
sessionStorage.setItem("dicttime",new Date().getTime());
options.onReady && options.onReady(this.dict)
this.$nextTick(() => {
this.$emit('dictReady', this.dict)
if (this.$options.methods && this.$options.methods.onDictReady instanceof Function) {
this.$options.methods.onDictReady.call(this, this.dict)
}
})
})
}else{
this.dict.doSession(sedata).then(() => {
this.dictstatus = true;
this.$nextTick(() => {
this.$emit('dictReady', this.dict)
})
});
}
} else {
options.onCreated && options.onCreated(this.dict)
2024-06-20 14:53:19 +08:00
this.dict.init(this.$options.dicts,this.$options.dictapiurl).then(() => {
2024-06-20 14:42:44 +08:00
this.dictstatus = true;
sessionStorage.setItem("dicttime",new Date().getTime());
options.onReady && options.onReady(this.dict)
this.$nextTick(() => {
this.$emit('dictReady', this.dict)
if (this.$options.methods && this.$options.methods.onDictReady instanceof Function) {
this.$options.methods.onDictReady.call(this, this.dict)
}
})
})
}
},
methods: {
formatDict(datas, othertype, type, startype) {
let newdatas = {};
if (this.isObject(datas)) {
newdatas = datas
} else if(this.$isArray(datas)){
newdatas = datas[0];
}else{
return '-';
}
if (!othertype || typeof (othertype) == "undefined") {
return '-'
}
let filters = null;
if (newdatas[type]) {
filters = newdatas[type].find((f) => f.value == othertype);
} else if (newdatas[startype + '_' + type]) {
filters = newdatas[startype + '_' + type].find((f) => f.value == othertype);
}
return filters&&filters.label || '-'
},
getDict(name) {
new Promise((r)=>{
this.dict = new Dict();
this.dict.owner = this;
this.dict.getDict(name).then(() => {
r(this.dict);
})
});
}
}
})
}