getters.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * vuex getter
  3. */
  4. import dictEnum from '@/enum/dict';
  5. import store from './index';
  6. export default {
  7. user: (state) => state.user,
  8. theme: (state) => state.theme,
  9. dict: (state) => state.dict,
  10. taskObj: (state) => state.taskObj,
  11. // 根据字典enumName 和 dictCode 获取字典项
  12. getDict: (state) => (enumName, dictCode) =>
  13. (state.dict[dictEnum[enumName]] || []).find(
  14. (item) => item.dictCode === dictCode
  15. ) || {},
  16. // 根据字典enumName 和 dictCode 获取字典 值(名称
  17. getDictValue: (state) => (enumName, dictCode) => {
  18. const obj = (state.dict[dictEnum[enumName]] || []).find((item) => {
  19. return item.dictCode === dictCode + '';
  20. });
  21. if (!obj) {
  22. // 提交dict模块下的actions的requestDict
  23. store.dispatch('dict/requestDict', enumName);
  24. return '';
  25. }
  26. return obj && obj.dictValue;
  27. },
  28. // 根据字典enumName 获取字典列表 label value 格式
  29. getDictListByName: (sate) => (enumName) => {
  30. const dList = sate.dict[dictEnum[enumName]] || [];
  31. if (!dList.length) {
  32. // 提交dict模块下的actions的requestDict
  33. store.dispatch('dict/requestDict', enumName);
  34. return '';
  35. }
  36. return dList.map((i) => {
  37. return {
  38. ...i,
  39. label: i.dictValue,
  40. value: i.dictCode
  41. };
  42. });
  43. }
  44. };