dict.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { reactive } from "vue";
  2. import { storage, isNull, parse } from "../utils";
  3. import { type DICT_DATA, dictData } from "@/api/user";
  4. import { user } from "./user";
  5. // 字典管理类
  6. export class Dict {
  7. private data: DICT_DATA = reactive({
  8. 'config:open:children-all': [],
  9. 'config:open:value': {},
  10. 'dict:value': {},
  11. 'dict:code': {},
  12. 'dict:label': {},
  13. 'dict:children-all': {},
  14. 'dict:children': {},
  15. 'dict:value:obj': {},
  16. }); // 存储所有字典数据
  17. constructor() {
  18. const dictData = storage.get("dict");
  19. if (isNull(dictData)) {
  20. return;
  21. }
  22. this.data = dictData;
  23. }
  24. getConfigValueByType(type: string) {
  25. return this.data['config:open:value'][type]
  26. }
  27. getConfigAll() {
  28. return this.data['config:open:children-all']
  29. }
  30. getChildren(type: string, value: string, childValue: string) {
  31. const children = this.data['dict:children'][type]
  32. const child = children[value]
  33. return child[childValue]
  34. }
  35. getChildrenMapByType(type: string, value: string) {
  36. const children = this.data['dict:children'][type]
  37. return children[value]
  38. }
  39. getLabelByValue(type: string, value: string) {
  40. const child = this.data['dict:value'][type]
  41. return child[value]
  42. }
  43. getObjByValue(type: string, value: string) {
  44. const child = this.data['dict:value:obj'][type]
  45. return child[value]
  46. }
  47. getLabelByValueMapByType(type: string) {
  48. return this.data['dict:value'][type]
  49. }
  50. getValueByLabel(type: string, label: string) {
  51. const child = this.data['dict:label'][type]
  52. return child[label]
  53. }
  54. getValueByLabelMapByType(type: string) {
  55. return this.data['dict:label'][type]
  56. }
  57. getValueByCode(type: string, code: string) {
  58. const child = this.data['dict:code'][type]
  59. return child[code]
  60. }
  61. getValueByCodeMapByType(type: string) {
  62. return this.data['dict:code'][type]
  63. }
  64. getChildrenList(type: string) {
  65. return this.data['dict:children-all'][type]
  66. }
  67. /**
  68. * 刷新字典数据
  69. * @param types 可选,指定需要刷新的字典key数组
  70. */
  71. async refresh(): Promise<void> {
  72. const res = await dictData();
  73. if (res == null) {
  74. return;
  75. }
  76. this.data = res;
  77. storage.set("dict", res, 60 * 60 * 24 * 1000);
  78. }
  79. }
  80. // 单例字典对象
  81. export const dict = new Dict();