classes.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import Vue from 'vue';
  2. import Vuex from 'vuex';
  3. import _ from 'lodash';
  4. import axios from 'axios';
  5. Vue.use(Vuex);
  6. const api = {
  7. interface: `/api/train/class`,
  8. divide: `/api/train/class/divide`,
  9. };
  10. const state = () => ({});
  11. const mutations = {};
  12. const actions = {
  13. async query({ commit }, { skip = 0, limit, ...info } = {}) {
  14. const res = await this.$axios.$get(`${api.interface}`, { skip, limit, ...info });
  15. return res;
  16. },
  17. async create({ commit }, payload) {
  18. const res = await this.$axios.$post(`${api.interface}`, payload);
  19. return res;
  20. },
  21. async fetch({ commit }, payload) {
  22. const res = await this.$axios.$get(`${api.interface}/${payload}`);
  23. return res;
  24. },
  25. async update({ commit }, { id, ...data }) {
  26. const res = await this.$axios.$post(`${api.interface}/update/${id}`, data);
  27. return res;
  28. },
  29. async delete({ commit }, payload) {
  30. const res = await this.$axios.$delete(`${api.interface}/${payload}`);
  31. return res;
  32. },
  33. //分班
  34. async divide({ commit }, payload) {
  35. const res = await this.$axios.$post(`${api.divide}`, payload);
  36. return res;
  37. },
  38. async mergeRequest({ commit, dispatch }, { method, data }) {
  39. let toRequest = () => {
  40. let res = [];
  41. for (const i of data) {
  42. res.push(dispatch(method, i));
  43. }
  44. return res;
  45. };
  46. let result = await axios.all(toRequest());
  47. let newFilter = data => {
  48. let res = data.map(i => {
  49. let type = _.isArray(i);
  50. if (!type) {
  51. //fetch的多个请求 是object 将errcode为0的data取出来
  52. return _.get(i, `data`, i);
  53. } else {
  54. //query的多个请求 array 将此数据再次走这个方法
  55. return newFilter(i);
  56. }
  57. });
  58. return res;
  59. };
  60. let returns = _.flattenDeep(newFilter(result));
  61. return returns;
  62. },
  63. };
  64. export default {
  65. namespaced: true,
  66. state,
  67. mutations,
  68. actions,
  69. };