dict.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import dictEnum, {
  2. numberList
  3. } from '@/enum/dict';
  4. import Vue from 'vue'
  5. // import {
  6. // get,
  7. // postJ
  8. // } from "@/utils/api.js";
  9. import {
  10. get,
  11. put,
  12. putJ,
  13. postJ,
  14. deleteApi
  15. } from "@/utils/request";
  16. const getSubListByParentId = function(id = 0) {
  17. return get(Vue.prototype.apiUrl + `/classify/getSubListByParentId/${id}`)
  18. }
  19. const getDictListByMainCode = function(mainCode) {
  20. return get(Vue.prototype.apiUrl + `/system/dict/getByCode/${mainCode}`)
  21. }
  22. //非枚举定义
  23. const otherDictConfig = {
  24. [dictEnum.物品类型]: {
  25. request: getSubListByParentId,
  26. dictCode: 'type',
  27. dictValue: 'name',
  28. resKey: '' //为空选 data
  29. }
  30. }
  31. const state = {}
  32. const mutations = {
  33. //根据字典code 添加字典
  34. ADD_DICT: (state, {
  35. code,
  36. dict
  37. }) => {
  38. // console.log(dict, 'dict1')
  39. Vue.set(state, code, dict)
  40. }
  41. // // 根据字典enumName 和 dictCode 获取字典项
  42. // GET_DICT (state, { enumName, dictCode }) {
  43. // return (
  44. // (state[dictEnum[enumName]] || []).find(
  45. // item => item.dictCode === dictCode
  46. // ) || {}
  47. // )
  48. // },
  49. // // 根据字典enumName 和 dictCode 获取字典 值(名称
  50. // GET_DICT_VALUE (state, { enumName, dictCode }) {
  51. // const obj = (state[dictEnum[enumName]] || []).find(
  52. // item => item.dictCode === dictCode
  53. // )
  54. // return obj && obj.dictValue
  55. // }
  56. }
  57. const actions = {
  58. // 根据字典enumName请求字典 已获取的不做重复请求
  59. async requestDict({
  60. commit,
  61. state
  62. }, enumName) {
  63. const code = dictEnum[enumName]
  64. if (state[code]?.length) return state[code]
  65. let res
  66. if (otherDictConfig[dictEnum[enumName]]) {
  67. const config = otherDictConfig[dictEnum[enumName]]
  68. //非枚举定义
  69. res = await config.request()
  70. if (res?.success) {
  71. let list = config.resKey ? res.data[config.resKey] : res.data
  72. commit('ADD_DICT', {
  73. code,
  74. dict: list.map(item => ({
  75. ...item,
  76. dictCode: item[config.dictCode],
  77. dictValue: item[config.dictValue]
  78. }))
  79. })
  80. return res.list
  81. }
  82. } else {
  83. res = await getDictListByMainCode(code)
  84. const isNumber = numberList.includes(code);
  85. if (res?.code == 0) {
  86. commit('ADD_DICT', {
  87. code,
  88. dict: res.data.map((item) => {
  89. const arr = Object.entries(item)[0] || [];
  90. return {
  91. dictCode: isNumber ? Number(arr[0]) : arr[0],
  92. dictValue: arr[1]
  93. };
  94. })
  95. })
  96. return res.data
  97. }
  98. }
  99. return []
  100. },
  101. // 更新字典
  102. async reloadRequestDict({
  103. commit
  104. }, enumName) {
  105. const code = dictEnum[enumName]
  106. const res = await getDictListByMainCode(code)
  107. const isNumber = numberList.includes(code);
  108. if (res?.success) {
  109. commit('ADD_DICT', {
  110. code,
  111. dict: res.data.map((item) => {
  112. const arr = Object.entries(item)[0] || [];
  113. return {
  114. dictCode: isNumber ? Number(arr[0]) : arr[0],
  115. dictValue: arr[1]
  116. };
  117. })
  118. })
  119. return res.data
  120. }
  121. return []
  122. }
  123. }
  124. export default {
  125. namespaced: true,
  126. state,
  127. mutations,
  128. actions
  129. }