student.js 2.2 KB

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