import Vue from 'vue' import Vuex from 'vuex' import * as api from '@/api' Vue.use(Vuex) const store = new Vuex.Store({ state: { // 存放状态 configList: [], departmentList: [], }, mutations: { // 分类列表 setCategoryList(state, data) { state.categoryList = data }, setConfigList(state, data) { state.configList = data }, setDepartmentList(state, data) { state.departmentList = data }, }, actions: { // 查询配置列表 async getConfig({ commit }) { const res = await api.config.queryConfigList() // 要求变成键值对的样子 const config = res.result.records.reduce((acc, item) => { if (!item.paramCode) { console.log('paramCode为空', item); return acc } acc[item.paramCode] = item return acc }, {}) commit('setConfigList', config) }, // 查询部门列表 async getDepartment({ commit }) { const res = await api.config.queryDepartmentList() commit('setDepartmentList', res.result.records) }, // 获取分类列表 async getCategory({ commit }) { const res = await api.config.queryCategoryList() commit('setCategoryList', res.result.records) }, // 初始化数据 async initData({ dispatch, state }) { // 检查是否已初始化 if (state.configList.length > 0 && state.departmentList.length > 0 && state.categoryList.length > 0) { console.log('配置数据已初始化,无需重复初始化') return } try { await Promise.all([ dispatch('getConfig'), dispatch('getDepartment'), // dispatch('getCategory'), ]) console.log('所有配置数据初始化完成') } catch (error) { console.error('配置数据初始化失败:', error) } }, } }) export default store