dict.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { reactive } from "vue";
  2. import { service } from "../service";
  3. import type { DictKey } from "../types";
  4. import { forInObject, isNull, parse } from "../utils";
  5. // 字典项类型定义
  6. export type DictItem = {
  7. id: number; // 字典项ID
  8. typeId: number; // 字典类型ID
  9. label: string; // 显示标签
  10. name: string; // 可选名称
  11. value: any; // 字典项值
  12. orderNum: number; // 排序号
  13. parentId?: number | null; // 父级ID,可选
  14. };
  15. // 字典数据类型定义
  16. export type DictData = {
  17. key: string; // 字典key
  18. list: DictItem[]; // 字典项列表
  19. };
  20. // 字典管理类
  21. export class Dict {
  22. private data: DictData[] = reactive([]); // 存储所有字典数据
  23. constructor() {}
  24. /**
  25. * 获取指定key的字典数据
  26. * @param key 字典key
  27. * @returns 字典数据
  28. */
  29. find(key: string) {
  30. return this.data.find((e) => e.key == key);
  31. }
  32. /**
  33. * 获取指定key的字典项列表
  34. * @param key 字典key
  35. * @returns 字典项数组
  36. */
  37. get(key: DictKey): DictItem[] {
  38. return this.find(key)?.list ?? new Array<DictItem>();
  39. }
  40. /**
  41. * 获取指定key和value的字典项
  42. * @param key 字典key
  43. * @param value 字典项值
  44. * @returns 字典项或null
  45. */
  46. getItem(key: DictKey, value: any): DictItem | null {
  47. const item = this.get(key).find((e) => e.value == value);
  48. if (isNull(item)) {
  49. return null;
  50. }
  51. return item!;
  52. }
  53. /**
  54. * 获取指定key和多个value的字典项数组
  55. * @param key 字典key
  56. * @param values 字典项值数组
  57. * @returns 字典项数组
  58. */
  59. getItems(key: DictKey, values: any[]): DictItem[] {
  60. return values.map((e) => this.getItem(key, e)).filter((e) => !isNull(e)) as DictItem[];
  61. }
  62. /**
  63. * 获取指定key和value的字典项的label
  64. * @param key 字典key
  65. * @param value 字典项值
  66. * @returns 字典项label字符串
  67. */
  68. getItemLabel(key: DictKey, value: any): string {
  69. const item = this.getItem(key, value);
  70. if (isNull(item) || isNull(item?.label)) {
  71. return "";
  72. }
  73. return item!.label;
  74. }
  75. /**
  76. * 刷新字典数据
  77. * @param types 可选,指定需要刷新的字典key数组
  78. */
  79. async refresh(types?: DictKey[] | null): Promise<void> {
  80. const res = await service.dict.info.data({ types });
  81. // 遍历返回的字典数据
  82. forInObject(res, (arr, key) => {
  83. let list: DictItem[] = [];
  84. (arr as UTSJSONObject[]).forEach((e) => {
  85. e["label"] = e["name"];
  86. const d = parse<DictItem>(e);
  87. if (d != null) {
  88. list.push(d);
  89. }
  90. });
  91. const item = this.find(key);
  92. // 如果不存在则新增,否则更新
  93. if (isNull(item)) {
  94. this.data.push({
  95. key,
  96. list
  97. });
  98. } else {
  99. item!.list = list;
  100. }
  101. });
  102. // #ifdef H5
  103. console.log("[DICT]", this.data);
  104. // #endif
  105. }
  106. }
  107. // 单例字典对象
  108. export const dict = new Dict();
  109. /**
  110. * 获取字典实例
  111. * @returns Dict实例
  112. */
  113. export function useDict() {
  114. return dict;
  115. }