dict.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { getAllChildren } from "@/api/system";
  2. let cache = {}
  3. export const refresh = () => {
  4. getAllChildren()
  5. }
  6. export const getDict = (enumid, refresh = false) => {
  7. if(refresh || (! cache[enumid])) {
  8. return getAllChildren({
  9. enumid: enumid,
  10. isQueryFristNode: false,
  11. }).then(resp => {
  12. const list = resp.data || []
  13. cache[enumid] = list
  14. return Promise.resolve(cache[enumid])
  15. })
  16. }else {
  17. return Promise.resolve(cache[enumid])
  18. }
  19. }
  20. export const getDict2 = (enumid, firstNode = false, refresh = false) => {
  21. const key = `${enumid}_${firstNode}`
  22. if(refresh || (! cache[key])) {
  23. return getAllChildren({
  24. enumid: enumid,
  25. isQueryFristNode: firstNode,
  26. }).then(resp => {
  27. const list = resp.data || []
  28. cache[key] = list
  29. return Promise.resolve(cache[key])
  30. })
  31. }else {
  32. return Promise.resolve(cache[key])
  33. }
  34. }
  35. export const dictNameByParentId = (parentId, enumid, def = '', forceRefresh = false) => {
  36. return getDict(parentId, forceRefresh).then(list => {
  37. const name = list.find(d => d.enumid == enumid)?.enumname
  38. return name ? name : def
  39. })
  40. }
  41. export const dictNameByParentIdBatch = (dataList, parentId, field, def = '', forceRefresh = false) => {
  42. const map = {}
  43. dataList.forEach(d => {
  44. map[d[field]] = def
  45. })
  46. return getDict(parentId, forceRefresh).then(list => {
  47. Object.keys(map).forEach(key => {
  48. const name = list.find(d => d.enumid == key)?.enumname
  49. map[key] = name ? name : def
  50. })
  51. return map
  52. })
  53. }