dict.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. private windowWidth: number = 0
  18. private windowHeight: number = 0
  19. constructor() {
  20. const dictData = storage.get("dict");
  21. if (isNull(dictData)) {
  22. return;
  23. }
  24. const res = uni.getSystemInfoSync()
  25. this.windowWidth = res.windowWidth
  26. this.windowHeight = res.windowHeight
  27. this.data = dictData;
  28. }
  29. getWindowWidth() {
  30. return this.windowWidth
  31. }
  32. getWindowHeight() {
  33. return this.windowHeight
  34. }
  35. getConfigValueByType(type: string) {
  36. return this.data['config:open:value'][type]
  37. }
  38. getConfigAll() {
  39. return this.data['config:open:children-all']
  40. }
  41. getChildren(type: string, value: string, childValue: string) {
  42. const children = this.data['dict:children'][type]
  43. const child = children[value]
  44. return child[childValue]
  45. }
  46. getChildrenMapByType(type: string, value: string) {
  47. const children = this.data['dict:children'][type]
  48. return children[value]
  49. }
  50. getLabelByValue(type: string, value: string) {
  51. const child = this.data['dict:value'][type]
  52. return child[value]
  53. }
  54. getObjByValue(type: string, value: string) {
  55. const child = this.data['dict:value:obj'][type]
  56. return child[value]
  57. }
  58. getLabelByValueMapByType(type: string) {
  59. return this.data['dict:value'][type]
  60. }
  61. getValueByLabel(type: string, label: string) {
  62. const child = this.data['dict:label'][type]
  63. return child[label]
  64. }
  65. getValueByLabelMapByType(type: string) {
  66. return this.data['dict:label'][type]
  67. }
  68. getValueByCode(type: string, code: string) {
  69. const child = this.data['dict:code'][type]
  70. return child[code]
  71. }
  72. getValueByCodeMapByType(type: string) {
  73. return this.data['dict:code'][type]
  74. }
  75. getChildrenList(type: string) {
  76. return this.data['dict:children-all'][type]
  77. }
  78. /**
  79. * 刷新字典数据
  80. * @param types 可选,指定需要刷新的字典key数组
  81. */
  82. async refresh(): Promise<void> {
  83. if (!user.token) {
  84. return;
  85. }
  86. const res = await dictData();
  87. if (res == null) {
  88. return;
  89. }
  90. this.data = res;
  91. storage.set("dict", res, 60 * 60 * 24 * 1000);
  92. }
  93. }
  94. // 单例字典对象
  95. export const dict = new Dict();