dict.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { getDataDictList } from '../api/dict.js'
  2. import { storage } from './index.js'
  3. const DICT_KEY = 'dicts'
  4. /**
  5. * 字典工具模块 — 对标 PC 端 admin-web-new/src/utils/dict.ts
  6. */
  7. const dictUtil = {
  8. /**
  9. * 加载全部字典数据,按 code 分组后存入 storage
  10. * @returns {Promise<Object>} 分组后的字典数据
  11. */
  12. loadDicts: async () => {
  13. try {
  14. const res = await getDataDictList()
  15. if (res && res.code === 200) {
  16. const list = res.data
  17. const dictGroup = dictUtil.groupByKey(list, 'code')
  18. storage.set(DICT_KEY, dictGroup)
  19. return dictGroup
  20. }
  21. return {}
  22. } catch (e) {
  23. console.error('加载字典数据失败:', e)
  24. return {}
  25. }
  26. },
  27. /**
  28. * 获取缓存的全部字典数据
  29. * @returns {Object} { code: [items] }
  30. */
  31. getDicts: () => {
  32. return storage.get(DICT_KEY) || {}
  33. },
  34. /**
  35. * 获取指定编码的字典项列表
  36. * @param {string} code - 字典编码
  37. * @returns {Array} 字典项数组
  38. */
  39. getDictList: (code) => {
  40. const dicts = dictUtil.getDicts()
  41. return dicts[code] || []
  42. },
  43. /**
  44. * 根据字典编码和值获取显示名称
  45. * @param {string} code - 字典编码
  46. * @param {*} value - 字典值
  47. * @returns {string} 显示名称
  48. */
  49. getDictLabel: (code, value) => {
  50. if (value === null || value === undefined || value === '') {
  51. return value
  52. }
  53. const dicts = dictUtil.getDicts()
  54. const list = dicts[code]
  55. if (!list || list.length === 0) {
  56. return String(value)
  57. }
  58. const item = list.find(k => k.value == value)
  59. return item ? item.name : String(value)
  60. },
  61. /**
  62. * 根据字典编码和名称反查值
  63. * @param {string} code - 字典编码
  64. * @param {string} name - 显示名称
  65. * @returns {*} 字典值
  66. */
  67. getDictValue: (code, name) => {
  68. const dicts = dictUtil.getDicts()
  69. const list = dicts[code]
  70. if (!list || list.length === 0) {
  71. return null
  72. }
  73. const item = list.find(k => k.name === name)
  74. return item ? item.value : null
  75. },
  76. /**
  77. * 根据字典编码和值获取颜色
  78. * @param {string} code - 字典编码
  79. * @param {*} value - 字典值
  80. * @returns {string} 颜色值
  81. */
  82. getDictColor: (code, value) => {
  83. if (value === null || value === undefined) {
  84. return ''
  85. }
  86. const list = dictUtil.getDictList(code)
  87. const item = list.find(k => k.value == value)
  88. return item?.color || ''
  89. },
  90. /**
  91. * 获取选项列表(用于下拉框等)
  92. * @param {string} code - 字典编码
  93. * @returns {Array<{label: string, value: any}>}
  94. */
  95. getDictOptions: (code) => {
  96. const list = dictUtil.getDictList(code)
  97. return list.map(item => ({
  98. label: item.name,
  99. value: item.value
  100. }))
  101. },
  102. /**
  103. * 获取带"全部"选项的筛选器选项列表
  104. * @param {string} code - 字典编码
  105. * @returns {Array<{label: string, value: any}>}
  106. */
  107. getDictFilterOptions: (code) => {
  108. const opts = dictUtil.getDictOptions(code)
  109. return [{ label: '全部', value: '' }, ...opts]
  110. },
  111. /**
  112. * 格式化字典值(返回显示名称)
  113. */
  114. formatDict: (code, value) => {
  115. return dictUtil.getDictLabel(code, value)
  116. },
  117. /**
  118. * 清除缓存的字典数据
  119. */
  120. clearDicts: () => {
  121. storage.remove(DICT_KEY)
  122. },
  123. /**
  124. * 按 key 分组
  125. * @param {Array} elements - 原始数组
  126. * @param {string} key - 分组字段
  127. * @returns {Object} 分组后的对象
  128. */
  129. groupByKey: (elements, key) => {
  130. const map = {}
  131. if (!elements || elements.length === 0) {
  132. return map
  133. }
  134. for (let i = 0; i < elements.length; i++) {
  135. if (!elements[i][key] && elements[i][key] !== 0) {
  136. continue
  137. }
  138. const k = String(elements[i][key])
  139. const tmp = map[k] || []
  140. tmp.push(elements[i])
  141. map[k] = tmp
  142. }
  143. return map
  144. }
  145. }
  146. export default dictUtil
  147. // 便捷导出(兼容旧的调用方式)
  148. export const fmtDictName = (code, value) => dictUtil.getDictLabel(code, value)
  149. export const getDictColor = (code, value) => dictUtil.getDictColor(code, value)
  150. export const loadDicts = dictUtil.loadDicts