dict.ts 2.8 KB

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