classes.js 2.1 KB

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