student.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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/student`,
  8. noClass: '/api/train/student/seek',
  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. async noClass({ commit }, payload) {
  34. const res = await this.$axios.$get(`${api.noClass}`, payload);
  35. return res;
  36. },
  37. async mergeRequest({ commit, dispatch }, { method, data }) {
  38. let toRequest = () => {
  39. let res = [];
  40. for (const i of data) {
  41. res.push(dispatch(method, i));
  42. }
  43. };
  44. let result = await axios.all(toRequest());
  45. let newFilter = data => {
  46. let res = data.map(i => {
  47. let type = _.isArray(i);
  48. if (!type) {
  49. //fetch的多个请求 是object 将errcode为0的data取出来
  50. return _.get(i, `data`, i);
  51. } else {
  52. //query的多个请求 array 将此数据再次走这个方法
  53. return newFilter(i);
  54. }
  55. });
  56. return res;
  57. };
  58. let returns = _.flattenDeep(newFilter(result));
  59. return returns;
  60. },
  61. };
  62. export default {
  63. namespaced: true,
  64. state,
  65. mutations,
  66. actions,
  67. };