dict.js 2.7 KB

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