dict.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { listSimpleDictDatas } from '@/api/system/dict/data'
  2. const state = {
  3. /**
  4. * 数据字典 MAP
  5. * key:数据字典大类枚举值 dictType
  6. * dictValue:数据字典小类数值 {dictValue: '', dictLabel: ''} 的数组
  7. */
  8. dictDatas: {}
  9. }
  10. const mutations = {
  11. SET_DICT_DATAS: (state, dictDatas) => {
  12. state.dictDatas = dictDatas
  13. }
  14. }
  15. const actions = {
  16. loadDictDatas({ commit }) {
  17. listSimpleDictDatas().then(response => {
  18. // 设置数据
  19. const dictDataMap = {}
  20. response.data.forEach(dictData => {
  21. // 获得 dictType 层级
  22. const enumValueObj = dictDataMap[dictData.dictType]
  23. if (!enumValueObj) {
  24. dictDataMap[dictData.dictType] = []
  25. }
  26. // 处理 dictValue 层级
  27. dictDataMap[dictData.dictType].push({
  28. value: dictData.value,
  29. label: dictData.label
  30. })
  31. })
  32. // 存储到 Store 中
  33. commit('SET_DICT_DATAS', dictDataMap)
  34. })
  35. }
  36. }
  37. export default {
  38. namespaced: true,
  39. state,
  40. mutations,
  41. actions
  42. }