student.js 2.0 KB

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