import { reactive } from "vue"; import { storage, isNull, parse } from "../utils"; import { type DICT_DATA, dictData } from "@/api/user"; import { user } from "./user"; // 字典管理类 export class Dict { private data: DICT_DATA = reactive({ 'config:open:children-all': [], 'config:open:value': {}, 'dict:value': {}, 'dict:code': {}, 'dict:label': {}, 'dict:children-all': {}, 'dict:children': {}, 'dict:value:obj': {}, }); // 存储所有字典数据 constructor() { const dictData = storage.get("dict"); if (isNull(dictData)) { return; } this.data = dictData; } getConfigValueByType(type: string) { return this.data['config:open:value'][type] } getConfigAll() { return this.data['config:open:children-all'] } getChildren(type: string, value: string, childValue: string) { const children = this.data['dict:children'][type] const child = children[value] return child[childValue] } getChildrenMapByType(type: string, value: string) { const children = this.data['dict:children'][type] return children[value] } getLabelByValue(type: string, value: string) { const child = this.data['dict:value'][type] return child[value] } getObjByValue(type: string, value: string) { const child = this.data['dict:value:obj'][type] return child[value] } getLabelByValueMapByType(type: string) { return this.data['dict:value'][type] } getValueByLabel(type: string, label: string) { const child = this.data['dict:label'][type] return child[label] } getValueByLabelMapByType(type: string) { return this.data['dict:label'][type] } getValueByCode(type: string, code: string) { const child = this.data['dict:code'][type] return child[code] } getValueByCodeMapByType(type: string) { return this.data['dict:code'][type] } getChildrenList(type: string) { return this.data['dict:children-all'][type] } /** * 刷新字典数据 * @param types 可选,指定需要刷新的字典key数组 */ async refresh(): Promise { const res = await dictData(); if (res == null) { return; } this.data = res; storage.set("dict", res, 60 * 60 * 24 * 1000); } } // 单例字典对象 export const dict = new Dict();