import Vue from 'vue'; import Vuex from 'vuex'; import _ from 'lodash'; import axios from 'axios'; Vue.use(Vuex); const api = { lookInfo: `/api/train/notice/look `, noticeInfo: `/api/train/notice`, classInfo: id => `/api/train/class/classinfo/${id}`, }; const state = () => ({}); const mutations = {}; const actions = { async query({ commit }, { skip = 0, limit, ...info } = {}) { const res = await this.$axios.$get(`${api.noticeInfo}`, { skip, limit, ...info }); return res; }, async create({ commit }, payload) { const res = await this.$axios.$post(`${api.noticeInfo}`, payload); return res; }, async fetch({ commit }, payload) { const res = await this.$axios.$get(`${api.noticeInfo}/${payload}`); return res; }, async lookFetch({ commit }, payload) { const res = await this.$axios.$post(`${api.lookInfo}`, payload); return res; }, async classList({ commit }, payload) { const res = await this.$axios.$get(`${api.classInfo(payload)}`); return res; }, async resend({ commit }, payload) { const res = await this.$axios.$post(`${api.noticeInfo}/resend`, payload); return res; }, async mergeRequest({ commit, dispatch }, { method, data }) { let toRequest = () => { let res = []; for (const i of data) { res.push(dispatch(method, i)); } return res; }; let result = await axios.all(toRequest()); let newFilter = data => { let res = data.map(i => { let type = _.isArray(i); if (!type) { //fetch的多个请求 是object 将errcode为0的data取出来 return _.get(i, `data`, i); } else { //query的多个请求 array 将此数据再次走这个方法 return newFilter(i); } }); return res; }; let returns = _.flattenDeep(newFilter(result)); return returns; }, }; export default { namespaced: true, state, mutations, actions, };