125 lines
3.9 KiB
JavaScript
125 lines
3.9 KiB
JavaScript
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 {}
|
|
}
|
|
|
|
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)
|
|
this.dict.init(this.$options.dicts,this.$options.dictapiurl).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)
|
|
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
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)
|
|
this.dict.init(this.$options.dicts,this.$options.dictapiurl).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)
|
|
|
|
}
|
|
})
|
|
})
|
|
}
|
|
},
|
|
|
|
|
|
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);
|
|
})
|
|
});
|
|
}
|
|
}
|
|
})
|
|
}
|