1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- /* eslint-disable no-param-reassign */
- /* eslint-disable no-shadow */
- // import * as types from './.dict.js';
- import assert from 'assert';
- import _ from 'lodash';
- import { LOADED, PRESET } from './.dict';
- const api = {
- listItem: '/gaf/code/:codeType/items',
- listUnit: '/naf/unit/list',
- listXzqh: '/naf/code/xzqh/list',
- };
- // initial state
- export const state = () => ({
- codeTypes: [], // 字典分类
- items: {
- usage: [
- { code: '0', name: '正常' },
- { code: '1', name: '停用' },
- ],
- },
- codes: {
- usage: {
- 0: '正常',
- 1: '停用',
- },
- },
- });
- // actions
- export const actions = {
- async load({ state, commit }, payload) {
- assert(payload);
- if (state.items[payload]) {
- return state.items[payload];
- }
- // LOAD PRESET DICT
- if (PRESET[payload]) {
- commit(LOADED, { codeType: payload, items: PRESET[payload] });
- return PRESET[payload];
- }
- let res;
- if (payload === 'unit') {
- // LOAD UNIT DICT
- res = await this.$axios.$get(api.listUnit);
- } else if (payload === 'xzqh') {
- // LOAD XZQH DICT
- res = await this.$axios.$get(api.listXzqh, { level: 1 });
- if (res.errcode) return res;
- const rs1 = res.data || res;
- res = await this.$axios.$get(api.listXzqh, { level: 2 });
- if (res.errcode) return res;
- const rs2 = res.data || res;
- res = rs1.map(p => {
- const prefix = p.code.substr(0, 2);
- const children = rs2.filter(c => c.code !== p.code && c.code.startsWith(prefix));
- return { ...p, children };
- });
- } else if (payload === 'city') {
- // 吉林省内地市
- res = await this.$axios.$get(api.listXzqh, { level: 2, parent: '220000' });
- } else {
- // LOAD COMMONS DICT
- res = await this.$axios.$get(api.listItem, { codeType: payload });
- }
- if (!res.errcode) {
- commit(LOADED, { codeType: payload, items: res.data || res });
- return res.data || res;
- }
- return res;
- },
- };
- // mutations
- export const mutations = {
- [LOADED](state, { codeType, items }) {
- state.items[codeType] = items;
- state.codes[codeType] = items.reduce((acc, item) => {
- acc[item.code] = item.name;
- if (Array.isArray(item.children) && item.children.length > 0) {
- _.forEach(item.children, p => {
- acc[p.code] = p.name;
- });
- }
- return acc;
- }, {});
- },
- };
- export const namespaced = true;
|