|
@@ -1,134 +1,86 @@
|
|
|
import { reactive } from "vue";
|
|
import { reactive } from "vue";
|
|
|
-import { request } from "../service";
|
|
|
|
|
-import { forInObject, isNull, parse } from "../utils";
|
|
|
|
|
|
|
+import { storage, isNull, parse } from "../utils";
|
|
|
|
|
+import { type DICT_DATA, dictData } from "@/api/user";
|
|
|
|
|
+import { user } from "./user";
|
|
|
|
|
|
|
|
-// 字典项类型定义
|
|
|
|
|
-export type DictItem = {
|
|
|
|
|
- id: number; // 字典项ID
|
|
|
|
|
- typeId: number; // 字典类型ID
|
|
|
|
|
- label: string; // 显示标签
|
|
|
|
|
- name: string; // 可选名称
|
|
|
|
|
- value: any; // 字典项值
|
|
|
|
|
- orderNum: number; // 排序号
|
|
|
|
|
- parentId?: number | null; // 父级ID,可选
|
|
|
|
|
-};
|
|
|
|
|
-
|
|
|
|
|
-// 字典数据类型定义
|
|
|
|
|
-export type DictData = {
|
|
|
|
|
- key: string; // 字典key
|
|
|
|
|
- list: DictItem[]; // 字典项列表
|
|
|
|
|
-};
|
|
|
|
|
|
|
|
|
|
// 字典管理类
|
|
// 字典管理类
|
|
|
export class Dict {
|
|
export class Dict {
|
|
|
- private data: DictData[] = reactive([]); // 存储所有字典数据
|
|
|
|
|
-
|
|
|
|
|
- constructor() {}
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 获取指定key的字典数据
|
|
|
|
|
- * @param key 字典key
|
|
|
|
|
- * @returns 字典数据
|
|
|
|
|
- */
|
|
|
|
|
- find(key: string) {
|
|
|
|
|
- return this.data.find((e) => e.key == key);
|
|
|
|
|
|
|
+ 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;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * 获取指定key的字典项列表
|
|
|
|
|
- * @param key 字典key
|
|
|
|
|
- * @returns 字典项数组
|
|
|
|
|
- */
|
|
|
|
|
- get(key: string): DictItem[] {
|
|
|
|
|
- return this.find(key)?.list ?? new Array<DictItem>();
|
|
|
|
|
|
|
+ getConfigValueByType(type: string) {
|
|
|
|
|
+ return this.data['config:open:value'][type]
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 获取指定key和value的字典项
|
|
|
|
|
- * @param key 字典key
|
|
|
|
|
- * @param value 字典项值
|
|
|
|
|
- * @returns 字典项或null
|
|
|
|
|
- */
|
|
|
|
|
- getItem(key: string, value: any): DictItem | null {
|
|
|
|
|
- const item = this.get(key).find((e) => e.value == value);
|
|
|
|
|
-
|
|
|
|
|
- if (isNull(item)) {
|
|
|
|
|
- return null;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return item!;
|
|
|
|
|
|
|
+ getConfigAll() {
|
|
|
|
|
+ return this.data['config:open:children-all']
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 获取指定key和多个value的字典项数组
|
|
|
|
|
- * @param key 字典key
|
|
|
|
|
- * @param values 字典项值数组
|
|
|
|
|
- * @returns 字典项数组
|
|
|
|
|
- */
|
|
|
|
|
- getItems(key: string, values: any[]): DictItem[] {
|
|
|
|
|
- return values.map((e) => this.getItem(key, e)).filter((e) => !isNull(e)) as DictItem[];
|
|
|
|
|
|
|
+ getChildren(type: string, value: string, childValue: string) {
|
|
|
|
|
+ const children = this.data['dict:children'][type]
|
|
|
|
|
+ const child = children[value]
|
|
|
|
|
+ return child[childValue]
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 获取指定key和value的字典项的label
|
|
|
|
|
- * @param key 字典key
|
|
|
|
|
- * @param value 字典项值
|
|
|
|
|
- * @returns 字典项label字符串
|
|
|
|
|
- */
|
|
|
|
|
- getItemLabel(key: string, value: any): string {
|
|
|
|
|
- const item = this.getItem(key, value);
|
|
|
|
|
-
|
|
|
|
|
- if (isNull(item) || isNull(item?.label)) {
|
|
|
|
|
- return "";
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return item!.label;
|
|
|
|
|
|
|
+ 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数组
|
|
* @param types 可选,指定需要刷新的字典key数组
|
|
|
*/
|
|
*/
|
|
|
- async refresh(types?: string[] | null): Promise<void> {
|
|
|
|
|
- const res = await request({
|
|
|
|
|
- url: "/app/dict/info/data",
|
|
|
|
|
- method: "POST",
|
|
|
|
|
- data: { types }
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ async refresh(): Promise<void> {
|
|
|
|
|
+ const res = await dictData();
|
|
|
|
|
|
|
|
if (res == null) {
|
|
if (res == null) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- // 遍历返回的字典数据
|
|
|
|
|
- forInObject(res, (arr, key) => {
|
|
|
|
|
- let list: DictItem[] = [];
|
|
|
|
|
-
|
|
|
|
|
- (arr as UTSJSONObject[]).forEach((e) => {
|
|
|
|
|
- e["label"] = e["name"];
|
|
|
|
|
- const d = parse<DictItem>(e);
|
|
|
|
|
-
|
|
|
|
|
- if (d != null) {
|
|
|
|
|
- list.push(d);
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- const item = this.find(key);
|
|
|
|
|
-
|
|
|
|
|
- // 如果不存在则新增,否则更新
|
|
|
|
|
- if (isNull(item)) {
|
|
|
|
|
- this.data.push({
|
|
|
|
|
- key,
|
|
|
|
|
- list
|
|
|
|
|
- });
|
|
|
|
|
- } else {
|
|
|
|
|
- item!.list = list;
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- // #ifdef H5
|
|
|
|
|
- console.log("[DICT]", this.data);
|
|
|
|
|
- // #endif
|
|
|
|
|
|
|
+ this.data = res;
|
|
|
|
|
+ storage.set("dict", res, 60 * 60 * 24 * 1000);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|