erp-el-element/dict/index.js

136 lines
4.3 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() {
2024-06-20 15:20:05 +08:00
let apiurl="/api/sysdict/get_dict";
2024-06-20 14:42:44 +08:00
if (this.$options === undefined || this.$options.dicts === undefined || this.$options.dicts === null) {
return {}
}
2024-06-20 14:52:37 +08:00
2024-06-20 15:53:30 +08:00
const dict = new Dict(this.$options.dictapiurl);
2024-06-20 15:20:05 +08:00
if(this.$options.dictapiurl){
apiurl=this.$options.dictapiurl;
dict.apiurl=this.$options.dictapiurl;
}
dict.owner = this;
2024-06-20 14:42:44 +08:00
return {
2024-06-20 15:20:05 +08:00
apiurl,
2024-06-20 14:42:44 +08:00
dict,
dictstatus: false,
requesttime:0,
}
},
created() {
if (!(this.dict instanceof Dict)) {
return
}
2024-06-20 15:53:30 +08:00
if(this.$options.dictapiurl){
this.dict.apiurl=this.$options.dictapiurl;
}
2024-06-20 14:42:44 +08:00
let dicttime=sessionStorage.getItem("dicttime");
if(dicttime&&new Date().getTime()-Number(dicttime)>1000*60*720){
2024-06-20 15:53:30 +08:00
options.onCreated && options.onCreated(this.dict)
this.dict.init(this.$options.dicts).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)
}
})
})
2024-06-20 15:53:30 +08:00
}else{
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 {
2024-06-20 14:42:44 +08:00
options.onCreated && options.onCreated(this.dict)
this.dict.init(this.$options.dicts).then(() => {
2024-06-20 15:53:30 +08:00
this.dictstatus = true;
2024-06-20 14:42:44 +08:00
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)
}
})
})
2024-06-20 15:53:30 +08:00
}
}
2024-06-20 14:42:44 +08:00
},
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) {
2024-06-20 16:17:29 +08:00
return new Promise((r)=>{
2024-06-20 15:53:30 +08:00
this.dict = new Dict(this.apiurl);
2024-06-20 14:42:44 +08:00
this.dict.owner = this;
2024-06-20 15:53:30 +08:00
if(this.apiurl){
this.dict.apiurl=this.apiurl;
}
2024-06-20 14:42:44 +08:00
this.dict.getDict(name).then(() => {
r(this.dict);
})
});
}
}
})
}