dataDict.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { defineStore } from 'pinia';
  2. import { ref } from 'vue';
  3. import { getAllDataDictList as getList } from '@/api';
  4. export const useDataDictStore = defineStore('dataDict', () => {
  5. const dataDict = ref([]);
  6. const map = new Map();
  7. let isLoaded = false;
  8. let loadingPromise = null;
  9. /**
  10. * 加载数据字典
  11. */
  12. const loadDataDictList = async () => {
  13. if (isLoaded) {
  14. return dataDict.value;
  15. }
  16. // 应用启动和页面初始化可能同时触发,复用同一个请求,避免重复加载。
  17. if (loadingPromise) {
  18. return loadingPromise;
  19. }
  20. loadingPromise = getList()
  21. .then(response => {
  22. if (Array.isArray(response?.data)) {
  23. dataDict.value = response.data;
  24. map.clear();
  25. isLoaded = true;
  26. }
  27. return dataDict.value;
  28. })
  29. .catch(() => dataDict.value)
  30. .finally(() => {
  31. loadingPromise = null;
  32. });
  33. return loadingPromise;
  34. };
  35. /**
  36. * 获取type对应字典列表
  37. * @param type
  38. */
  39. const getDataDictList = type => {
  40. if (map.has(type) && map.get(type).length > 0) {
  41. return map.get(type);
  42. }
  43. const list = dataDict.value.filter(item => item.dictType === type);
  44. // const locale = uni.getLocale()
  45. // // label格式为'xxxx~~en**aaaa~~ru**bbbb', 根据当前语言环境进行处理
  46. // const list = dataDict.value.filter(item => item.dictType === type)
  47. // .map(item => {
  48. // if (item.label.includes('~~') && item.label.includes('**')) {
  49. // const s = item.label.split('~~')
  50. // if (locale.startsWith('zh')) {
  51. // item.label = s[0]
  52. // } else if (locale.startsWith('ru')) {
  53. // const s2 = s[2].split('**')
  54. // item.label = s2[1]
  55. // } else {
  56. // const s1 = s[1].split('**')
  57. // item.label = s1[1]
  58. // }
  59. // }
  60. // return JSON.parse(JSON.stringify(item))
  61. // })
  62. map.set(type, list);
  63. return list;
  64. };
  65. const getStrDictOptions = dictType => {
  66. // 获得通用的 DictDataType 列表
  67. const dictOptions = getDataDictList(dictType);
  68. // 转换成 string 类型的 StringDictDataType 类型
  69. // why 需要特殊转换:避免 IDEA 在 v-for="dict in getStrDictOptions(...)" 时,el-option 的 key 会告警
  70. const dictOption = [];
  71. dictOptions.forEach(dict => {
  72. dictOption.push({
  73. ...dict,
  74. value: dict.value + '',
  75. });
  76. });
  77. return dictOption;
  78. };
  79. const getIntDictOptions = dictType => {
  80. // 获得通用的 DictDataType 列表
  81. const dictOptions = getDataDictList(dictType);
  82. // 转换成 number 类型的 NumberDictDataType 类型
  83. // why 需要特殊转换:避免 IDEA 在 v-for="dict in getIntDictOptions(...)" 时,el-option 的 key 会告警
  84. const dictOption = [];
  85. dictOptions.forEach(dict => {
  86. dictOption.push({
  87. ...dict,
  88. value: parseInt(dict.value + ''),
  89. });
  90. });
  91. return dictOption;
  92. };
  93. return {
  94. dataDict,
  95. getDataDictList,
  96. loadDataDictList,
  97. getStrDictOptions,
  98. getIntDictOptions,
  99. };
  100. });