group.js 2.2 KB

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