dict.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import { reactive } from 'vue';
  2. import { dictData } from "@/api/user";
  3. // 初始化全局响应式字典缓存
  4. let dictCache = uni.getStorageSync("dictCache");
  5. const reactiveDictCache = reactive(dictCache ? JSON.parse(dictCache) : {});
  6. /**
  7. * 获取字典数据(带缓存)
  8. */
  9. export const getDictOptions = async (dictType) => {
  10. if (!reactiveDictCache[dictType]) {
  11. const response = await dictData(dictType);
  12. reactiveDictCache[dictType] = response.data;
  13. // 将更新后的缓存保存回本地存储
  14. uni.setStorageSync('dictCache', JSON.stringify(reactiveDictCache));
  15. }
  16. return reactiveDictCache[dictType];
  17. };
  18. /**
  19. * 同步获取字典标签
  20. */
  21. export const getDictLabelSync = (dictType, value) => {
  22. const dictOptions = reactiveDictCache[dictType] || [];
  23. const item = dictOptions.find(item => item.value === String(value));
  24. return item?.label || '';
  25. };
  26. export const DICT_TYPE = {
  27. SYSTEM_CAZT: 'system_cazt',
  28. SYSTEM_LSDJ: 'system_lsdj',
  29. SYSTEM_CFLX: 'system_cflx',
  30. SYSTEM_QYXZ: 'system_qyxz',
  31. SYSTEM_LSPZ: 'system_lspz'
  32. };