import dictEnum, { numberList } from '@/enum/dict'; import Vue from 'vue'; import { getByCode } from '@/api/system/dictionary-data'; //非枚举定义 // const otherDictConfig = { // // [dictEnum.物品类型]: { // // request: getSubListByParentId, // // dictCode: 'type', // // dictValue: 'name', // // resKey: '' //为空选 data // // } // }; const state = { // 加载中的字典项 dictLoading: [] }; const mutations = { //根据字典code 添加字典 ADD_DICT: (state, { code, dict }) => { Vue.set(state, code, dict); }, dictLoadingAdd: (state, code) => { state.dictLoading.push(code); }, dictLoadingRemove: (state, code) => { state.dictLoading = state.dictLoading.filter((item) => item !== code); } }; const actions = { // 根据字典enumName请求字典 已获取的不做重复请求 async requestDict({ commit, state }, enumName) { const code = dictEnum[enumName]; // 如果字典正在加载中,则不重复请求 if (state.dictLoading.includes(code)) return; commit('dictLoadingAdd', code); if (state[code]?.length) return state[code]; try { let res = await getByCode(code); if (res?.code == 0) { const isNumber = numberList.includes(code); commit('ADD_DICT', { code, dict: res.data.map((item) => { const arr = Object.entries(item)[0] || []; return { dictCode: isNumber ? Number(arr[0]) : arr[0], dictValue: arr[1] }; }) }); commit('dictLoadingRemove', code); return res.data; } commit('dictLoadingRemove', code); return []; } catch (error) { commit('dictLoadingRemove', code); return []; } }, // 更新字典 async reloadRequestDict({ commit }, enumName) { const code = dictEnum[enumName]; const res = await getByCode(code); const isNumber = numberList.includes(code); if (res?.code == 0) { commit('ADD_DICT', { code, dict: res.data.map((item) => { const arr = Object.entries(item); return { dictCode: isNumber ? Number(arr[0]) : arr[0], dictValue: arr[1] }; }) }); return res.data; } return []; } }; export default { namespaced: true, state, mutations, actions };