dict.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { getDictionariesDetails } from '@/api/system'
  2. // 菜单类型字典
  3. export const MENU_TYPE = {
  4. DIRECTORY: 0,
  5. MENU: 1,
  6. BUTTON: 2,
  7. COMPONENT: 3
  8. }
  9. // 机构类型字典
  10. export const ORGANIZATION_CATEGORY = [
  11. { label: '支行', value: '支行' },
  12. { label: '网点', value: '网点' },
  13. { label: '部室', value: '部室' }
  14. ]
  15. // 审核状态字典
  16. export const APPROVAL_STATUS = [
  17. { text: '处理中', color: 'default' },
  18. { text: '审核通过', color: 'success' },
  19. { text: '审核拒绝', color: 'danger' },
  20. { text: '异常结束', color: 'warning' }
  21. ]
  22. export const BONUS_ALLOCATION_STATUS = {
  23. 0: {
  24. text: '未分配',
  25. color: 'warning'
  26. },
  27. 1: {
  28. text: '已分配',
  29. color: 'success'
  30. },
  31. 2: {
  32. text: '已确认',
  33. color: 'info'
  34. }
  35. }
  36. // const EMPLOYEE_STATUS = {}
  37. // 字典缓存对象
  38. const dictCache = {}
  39. // 字典配置
  40. const DICT_CONFIG = {
  41. EMPLOYEE_STATUS: {
  42. code: 'employeeStatus',
  43. data: null
  44. }
  45. }
  46. /**
  47. * 获取字典数据
  48. * @param {string} dictKey 字典键名
  49. * @returns {Promise} 返回字典数据
  50. */
  51. async function getDict (dictKey) {
  52. // 如果缓存中有数据,直接返回
  53. if (dictCache[dictKey]) {
  54. return dictCache[dictKey]
  55. }
  56. // 如果没有配置,抛出错误
  57. if (!DICT_CONFIG[dictKey]) {
  58. throw new Error(`字典 ${dictKey} 未配置`)
  59. }
  60. // 如果已有数据但未缓存,先缓存
  61. if (DICT_CONFIG[dictKey].data) {
  62. dictCache[dictKey] = DICT_CONFIG[dictKey].data
  63. return dictCache[dictKey]
  64. }
  65. // 从API获取数据
  66. try {
  67. const { data } = await getDictionariesDetails({ dictCode: DICT_CONFIG[dictKey].code })
  68. const _data = data.map(item => {
  69. return {
  70. label: item.dictTitle,
  71. value: item.dictValue,
  72. color: item.webContent?.dictColor
  73. }
  74. })
  75. DICT_CONFIG[dictKey].data = _data
  76. dictCache[dictKey] = _data
  77. return _data
  78. } catch (error) {
  79. console.error(`获取字典 ${dictKey} 失败:`, error)
  80. throw error
  81. }
  82. }
  83. // 默认导出字典获取方法
  84. export default {
  85. /**
  86. * 获取单个字典
  87. * @param {string} dictKey 字典键名
  88. * @returns {Promise} 返回字典数据
  89. */
  90. get: getDict,
  91. /**
  92. * 获取多个字典
  93. * @param {Array} dictKeys 字典键名数组
  94. * @returns {Promise} 返回字典对象 {dictKey1: data, dictKey2: data}
  95. */
  96. async getMultiple (dictKeys) {
  97. const result = {}
  98. await Promise.all(dictKeys.map(async key => {
  99. result[key] = await getDict(key)
  100. }))
  101. return result
  102. },
  103. /**
  104. * 预加载字典
  105. * @param {Array} dictKeys 需要预加载的字典键名数组
  106. */
  107. preload (dictKeys) {
  108. return this.getMultiple(dictKeys)
  109. }
  110. }