baseDataStore.js 1.31 KB
import { fetchSelectList } from '@/api/base/index';
import { selectTypeMapping } from '@/config';

const GET_OPTIONS_LIST = 'GET_OPTIONS_LIST';

const state = () => {
  let baseState = {};
  selectTypeMapping.map(typeItem => {
    baseState['list' + typeItem.value] = [];
  });
  return baseState;
};
const initGetters = () => {
  let getters = {};
  selectTypeMapping.map(typeItem => {
    getters['optionList' + typeItem.value] = state =>
      state['list' + typeItem.value];
    return {
      ['optionList' + typeItem.value]: state => state['list' + typeItem.value],
    };
  });
  return getters;
};
const initActions = () => {
  let actions = {};
  selectTypeMapping.map(typeItem => {
    actions['fetchOptionList' + typeItem.value] = ({ commit }) => {
      return fetchSelectList({
        params: {
          type: typeItem.value,
        },
      }).then(res => {
        const list = res.data;
        if (list) {
          commit(GET_OPTIONS_LIST, {
            key: 'list' + typeItem.value,
            list,
          });
        }
        return list;
      });
    };
  });
  return actions;
};

const getters = initGetters();

const actions = initActions();

const mutations = {
  [GET_OPTIONS_LIST](state, { key, list }) {
    state[key] = list;
  },
};

export default {
  state,
  getters,
  actions,
  mutations,
};