dict.js 2.4 KB

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