import { fetchOperatorList, fetchAccessOperatorList } from '@/api/base/index'; import typeMapping from './typeMapping'; const FETCH_OPERATOR_LIST = 'FETCH_OPERATOR_LIST'; const FETCH_STATE = 'FETCH_STATE'; const FETCH_END = 'FETCH_END'; const state = () => { let operatorState = {}; typeMapping.map(type => { operatorState[`list${type.value}`] = []; operatorState[`fetching${type.value}`] = false; }); return operatorState; }; const initGetters = () => { let getters = {}; typeMapping.map(type => { getters[`operatorOptionList${type.value}`] = state => state[`list${type.value}`]; }); return getters; }; const getters = initGetters(); const initAction = () => { let actions = {}; typeMapping.map(type => { actions[`fetchOperatorList${type.value}`] = ({ state, commit }) => { if (state[`fetching${type.value}`]) return; commit(FETCH_STATE, type.value); let fetchHandle = null; if (type.value == 0) { fetchHandle = fetchOperatorList; } else { fetchHandle = fetchAccessOperatorList; } return fetchHandle().then(res => { const list = res.data; commit(FETCH_OPERATOR_LIST, { type: type.value, list }); commit(FETCH_END, type.value); }); }; }); return actions; }; const actions = initAction(); const mutations = { [FETCH_OPERATOR_LIST](state, { type, list }) { state[`list${type}`] = list; }, [FETCH_STATE](state, type) { state[`fetching${type}`] = true; }, [FETCH_END](state, type) { state[`fetching${type}`] = false; }, }; export default { namespaced: true, state, getters, actions, mutations, };