index.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { defHttp } from '/@/utils/http/axios';
  2. import { useUserStore } from '/@/store/modules/user';
  3. import { getAuthCache } from '/@/utils/auth';
  4. import { DB_DICT_DATA_KEY } from '/@/enums/cacheEnum';
  5. /**
  6. * 从缓存中获取字典配置
  7. * @param code
  8. */
  9. export const getDictItemsByCode = (code) => {
  10. // update-begin--author:liaozhiyang---date:20230908---for:【QQYUN-6417】生产环境字典慢的问题
  11. const userStore = useUserStore();
  12. const dictItems = userStore.getAllDictItems;
  13. if (typeof dictItems === 'object' && dictItems[code]) {
  14. return dictItems[code];
  15. }
  16. //update-begin-author:liusq---date:2023-10-13--for: 【issues/777】列表 分类字典不显示
  17. //兼容以前的旧写法
  18. if (getAuthCache(DB_DICT_DATA_KEY) && getAuthCache(DB_DICT_DATA_KEY)[code]) {
  19. return getAuthCache(DB_DICT_DATA_KEY)[code];
  20. }
  21. //update-end-author:liusq---date:2023-10-13--for:【issues/777】列表 分类字典不显示
  22. // update-end--author:liaozhiyang---date:20230908---for:【QQYUN-6417】生产环境字典慢的问题
  23. };
  24. /**
  25. * 获取字典数组
  26. * @param dictCode 字典Code
  27. * @return List<Map>
  28. */
  29. export const initDictOptions = (code) => {
  30. //1.优先从缓存中读取字典配置
  31. if (getDictItemsByCode(code)) {
  32. return new Promise((resolve, reject) => {
  33. resolve(getDictItemsByCode(code));
  34. });
  35. }
  36. //2.获取字典数组
  37. //update-begin-author:taoyan date:2022-6-21 for: 字典数据请求前将参数编码处理,但是不能直接编码,因为可能之前已经编码过了
  38. if (code.indexOf(',') > 0 && code.indexOf(' ') > 0) {
  39. // 编码后类似sys_user%20where%20username%20like%20xxx' 是不包含空格的,这里判断如果有空格和逗号说明需要编码处理
  40. code = encodeURI(code);
  41. }
  42. //update-end-author:taoyan date:2022-6-21 for: 字典数据请求前将参数编码处理,但是不能直接编码,因为可能之前已经编码过了
  43. return defHttp.get({ url: `/sys/dict/getDictItems/${code}` });
  44. };
  45. /**
  46. * 获取字典数组
  47. * @param code 字典Code
  48. * @param params 查询参数
  49. * @param options 查询配置
  50. * @return List<Map>
  51. */
  52. export const ajaxGetDictItems = (code, params, options?) => defHttp.get({ url: `/sys/dict/getDictItems/${code}`, params }, options);