dict.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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: '/naf/code/:catg/list',
  9. listUnit: '/naf/unit/list',
  10. listXzqh: '/naf/code/xzqh/list',
  11. };
  12. // initial state
  13. export const state = () => ({
  14. categories: [], // 字典分类
  15. items: {
  16. // 分项映射表
  17. status: [{ code: '0', name: '正常' }, { code: '1', name: '挂起' }, { code: '2', name: '注销' }],
  18. usage: [{ code: '0', name: '正常' }, { code: '1', name: '停用' }],
  19. },
  20. codes: {
  21. // 代码映射表
  22. status: {
  23. 0: '正常',
  24. 1: '挂起',
  25. 2: '注销',
  26. },
  27. usage: {
  28. 0: '正常',
  29. 1: '停用',
  30. },
  31. },
  32. });
  33. // actions
  34. export const actions = {
  35. async load({ state, commit }, payload) {
  36. assert(payload);
  37. if (state.items[payload]) {
  38. return state.items[payload];
  39. }
  40. // LOAD PRESET DICT
  41. if (PRESET[payload]) {
  42. commit(LOADED, { category: payload, items: PRESET[payload] });
  43. return PRESET[payload];
  44. }
  45. let res;
  46. if (payload === 'unit') {
  47. // LOAD UNIT DICT
  48. res = await this.$axios.$get(api.listUnit);
  49. } else if (payload === 'xzqh') {
  50. // LOAD XZQH DICT
  51. res = await this.$axios.$get(api.listXzqh, { level: 1 });
  52. if (res.errcode) return res;
  53. const rs1 = res.data || res;
  54. res = await this.$axios.$get(api.listXzqh, { level: 2 });
  55. if (res.errcode) return res;
  56. const rs2 = res.data || res;
  57. res = rs1.map(p => {
  58. const prefix = p.code.substr(0, 2);
  59. const children = rs2.filter(c => c.code !== p.code && c.code.startsWith(prefix));
  60. return { ...p, children };
  61. });
  62. } else if (payload === 'city') {
  63. // 吉林省内地市
  64. res = await this.$axios.$get(api.listXzqh, { level: 2, parent: '220000' });
  65. } else {
  66. // LOAD COMMONS DICT
  67. res = await this.$axios.$get(api.listItem, { catg: payload });
  68. }
  69. if (!res.errcode) {
  70. commit(LOADED, { category: payload, items: res.data || res });
  71. return res.data || res;
  72. }
  73. return res;
  74. },
  75. };
  76. // mutations
  77. export const mutations = {
  78. [LOADED](state, { category, items }) {
  79. state.items[category] = items;
  80. state.codes[category] = items.reduce((acc, item) => {
  81. acc[item.code] = item.name;
  82. if (_.isArray(item.children) && item.children.length > 0) {
  83. _.forEach(item.children, p => {
  84. acc[p.code] = p.name;
  85. });
  86. }
  87. return acc;
  88. }, {});
  89. },
  90. };
  91. export const namespaced = true;