dict.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { reactive } from "vue";
  2. import { storage, isNull, parse } from "../utils";
  3. import { type DICT_DATA, dictData } from "@/services/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. if (!user.token) {
  73. return;
  74. }
  75. const res = await dictData();
  76. if (res == null) {
  77. return;
  78. }
  79. this.data = res;
  80. storage.set("dict", res, 60 * 60 * 24 * 1000);
  81. }
  82. }
  83. // 单例字典对象
  84. export const dict = new Dict();