dict.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* eslint-disable no-param-reassign */
  2. /* eslint-disable no-shadow */
  3. // import * as types from './.dict.js';
  4. import assert from 'assert';
  5. import _ from 'lodash';
  6. import { LOADED, PRESET } from './.dict';
  7. const api = {
  8. listItem: '/gaf/code/:codeType/items',
  9. listUnit: '/naf/unit/list',
  10. listXzqh: '/naf/code/xzqh/list',
  11. };
  12. // initial state
  13. export const state = () => ({
  14. codeTypes: [], // 字典分类
  15. items: {
  16. usage: [
  17. { code: '0', name: '正常' },
  18. { code: '1', name: '停用' },
  19. ],
  20. },
  21. codes: {
  22. usage: {
  23. 0: '正常',
  24. 1: '停用',
  25. },
  26. },
  27. });
  28. // actions
  29. export const actions = {
  30. async load({ state, commit }, payload) {
  31. assert(payload);
  32. if (state.items[payload]) {
  33. return state.items[payload];
  34. }
  35. // LOAD PRESET DICT
  36. if (PRESET[payload]) {
  37. commit(LOADED, { codeType: payload, items: PRESET[payload] });
  38. return PRESET[payload];
  39. }
  40. let res;
  41. if (payload === 'unit') {
  42. // LOAD UNIT DICT
  43. res = await this.$axios.$get(api.listUnit);
  44. } else if (payload === 'xzqh') {
  45. // LOAD XZQH DICT
  46. res = await this.$axios.$get(api.listXzqh, { level: 1 });
  47. if (res.errcode) return res;
  48. const rs1 = res.data || res;
  49. res = await this.$axios.$get(api.listXzqh, { level: 2 });
  50. if (res.errcode) return res;
  51. const rs2 = res.data || res;
  52. res = rs1.map(p => {
  53. const prefix = p.code.substr(0, 2);
  54. const children = rs2.filter(c => c.code !== p.code && c.code.startsWith(prefix));
  55. return { ...p, children };
  56. });
  57. } else if (payload === 'city') {
  58. // 吉林省内地市
  59. res = await this.$axios.$get(api.listXzqh, { level: 2, parent: '220000' });
  60. } else {
  61. // LOAD COMMONS DICT
  62. res = await this.$axios.$get(api.listItem, { codeType: payload });
  63. }
  64. if (!res.errcode) {
  65. commit(LOADED, { codeType: payload, items: res.data || res });
  66. return res.data || res;
  67. }
  68. return res;
  69. },
  70. };
  71. // mutations
  72. export const mutations = {
  73. [LOADED](state, { codeType, items }) {
  74. state.items[codeType] = items;
  75. state.codes[codeType] = items.reduce((acc, item) => {
  76. acc[item.code] = item.name;
  77. if (Array.isArray(item.children) && item.children.length > 0) {
  78. _.forEach(item.children, p => {
  79. acc[p.code] = p.name;
  80. });
  81. }
  82. return acc;
  83. }, {});
  84. },
  85. };
  86. export const namespaced = true;