erp-el-element/dict/Dict.js

218 lines
6.5 KiB
JavaScript
Raw Normal View History

2024-06-20 14:42:44 +08:00
import Vue from 'vue'
import { mergeRecursive } from "./../utils/ruoyi";
import DictMeta from './DictMeta'
import DictData from './DictData'
import { getDicts as getDicts } from './../api/InventoryManagement';
2024-06-20 14:52:37 +08:00
import request from './../utils/request';
2024-06-20 14:42:44 +08:00
const DEFAULT_DICT_OPTIONS = {
types: [],
}
/**
* @classdesc 字典
* @property {Object} label 标签对象内部属性名为字典类型名称
* @property {Object} dict 字段数组内部属性名为字典类型名称
* @property {Array.<DictMeta>} _dictMetas 字典元数据数组
*/
export default class Dict {
constructor() {
this.owner = null
this.label = {}
this.type = {}
this.raw={}
2024-06-20 15:20:05 +08:00
this.apiurl="/api/sysdict/get_dict";
2024-06-20 14:42:44 +08:00
}
2024-06-20 15:20:05 +08:00
init(options,url) {
2024-06-20 14:42:44 +08:00
return new Promise((r)=>{
let params=Object.assign([],options);
if (options instanceof Array) {
options = { types: options }
}
const opts = mergeRecursive(DEFAULT_DICT_OPTIONS, options)
if (opts.types === undefined) {
r();
throw new Error('need dict types')
}
2024-06-20 15:20:05 +08:00
if(url){
this.apiurl=url;
}
2024-06-20 15:34:10 +08:00
getRequest(params,this.apiurl).then((res)=>{
2024-06-20 14:42:44 +08:00
if(res&&res.datas){
const response=res.datas;
sessionStorage.setItem("dictdata",JSON.stringify(response));
Object.keys(response).map((k,i)=>{
let dictMeta=DictMeta.parse(k)
const type = dictMeta.type
Vue.set(this.label, type, {})
Vue.set(this.type, type, [])
Vue.set(this.raw, type, [])
if (dictMeta.lazy) {
return
}
let dicts = dictMeta.responseConverter(response, dictMeta)
if (!(dicts instanceof Array)) {
console.error('the return of responseConverter must be Array.<DictData>')
dicts = []
} else if (dicts.filter(d => d instanceof DictData).length !== dicts.length) {
console.error('the type of elements in dicts must be DictData')
dicts = []
}
this.type[type].splice(0, Number.MAX_SAFE_INTEGER, ...dicts) ;
let newraw=[];
dicts.forEach(d => {
newraw.push(d.raw);
Vue.set(this.label[type], d.value, d.label);
});
Vue.set(this.raw, type, newraw);
if(Object.keys(response).length-1==i){
r();
}
});
}
});
});
}
getDict(name){
return new Promise((r)=>{
let options=Array.isArray(name)?name:[name];
let params=Object.assign([],options);
if (options instanceof Array) {
options = { types: options }
}
const opts = mergeRecursive(DEFAULT_DICT_OPTIONS, options)
if (opts.types === undefined) {
r();
throw new Error('need dict types')
}
2024-06-20 15:34:10 +08:00
getRequest(params,this.apiurl).then((res)=>{
2024-06-20 14:42:44 +08:00
if(res&&res.datas){
const response=res.datas;
Object.keys(response).map((k,i)=>{
let dictMeta=DictMeta.parse(k)
const type = dictMeta.type
Vue.set(this.label, type, {})
Vue.set(this.type, type, [])
Vue.set(this.raw, type, [])
if (dictMeta.lazy) {
return
}
let dicts = dictMeta.responseConverter(response, dictMeta)
if (!(dicts instanceof Array)) {
console.error('the return of responseConverter must be Array.<DictData>')
dicts = []
} else if (dicts.filter(d => d instanceof DictData).length !== dicts.length) {
console.error('the type of elements in dicts must be DictData')
dicts = []
}
this.type[type].splice(0, Number.MAX_SAFE_INTEGER, ...dicts)
let newraw=[];
dicts.forEach(d => {
newraw.push(d.raw);
Vue.set(this.label[type], d.value, d.label)
});
Vue.set(this.raw, type, newraw);
if(Object.keys(response).length-1==i){
r(dicts);
}
});
}
});
});
}
doSession(response){
return new Promise((r)=>{
Object.keys(response).map((k,i)=>{
let dictMeta=DictMeta.parse(k)
const type = dictMeta.type
Vue.set(this.label, type, {})
Vue.set(this.type, type, [])
Vue.set(this.raw, type, [])
if (dictMeta.lazy) {
return
}
let dicts = dictMeta.responseConverter(response, dictMeta)
if (!(dicts instanceof Array)) {
console.error('the return of responseConverter must be Array.<DictData>')
dicts = []
} else if (dicts.filter(d => d instanceof DictData).length !== dicts.length) {
console.error('the type of elements in dicts must be DictData')
dicts = []
}
this.type[type].splice(0, Number.MAX_SAFE_INTEGER, ...dicts);
let newraw=[];
dicts.forEach(d => {
newraw.push(d.raw);
Vue.set(this.label[type], d.value, d.label);
});
Vue.set(this.raw, type, newraw);
if(Object.keys(response).length-1==i){
r();
}
});
});
}
/**
* 重新加载字典
* @param {String} type 字典类型
*/
reloadDict(type) {
const dictMeta = this._dictMetas.find(e => e.type === type)
if (dictMeta === undefined) {
return Promise.reject(`the dict meta of ${type} was not found`)
}
return loadDict(this, dictMeta)
}
}
2024-06-20 15:34:10 +08:00
function getRequest(options,apiurl) {
2024-06-20 14:52:37 +08:00
return request({
2024-06-20 15:34:10 +08:00
url: apiurl||"/api/sysdict/get_dict",
2024-06-20 14:52:37 +08:00
method: 'post',
2024-06-20 15:40:20 +08:00
data: {dict_code:Array.isArray(options)?options:[options]}
2024-06-20 15:09:57 +08:00
});
2024-06-20 14:42:44 +08:00
}
/**
* 加载字典
* @param {Dict} dict 字典
* @param {DictMeta} dictMeta 字典元数据
* @returns {Promise}
*/
function loadDict(dict, dictMeta) {
return dictMeta.request(dictMeta)
.then(response => {
const type = dictMeta.type
let dicts = dictMeta.responseConverter(response, dictMeta)
if (!(dicts instanceof Array)) {
console.error('the return of responseConverter must be Array.<DictData>')
dicts = []
} else if (dicts.filter(d => d instanceof DictData).length !== dicts.length) {
console.error('the type of elements in dicts must be DictData')
dicts = []
}
dict.type[type].splice(0, Number.MAX_SAFE_INTEGER, ...dicts)
dicts.forEach(d => {
Vue.set(dict.label[type], d.value, d.label)
})
return dicts
})
}