classes.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. updateHeadTeacher: `/api/train/class/uptea`,
  10. addStudent: id => `/api/train/class/upstuclass/${id} `, //id:班级id, ids:学生ID列表
  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. //分班
  36. async divide({ commit }, payload) {
  37. const res = await this.$axios.$post(`${api.divide}`, payload);
  38. return res;
  39. },
  40. //手动分班
  41. async addStudent({ commit }, { id, ids }) {
  42. const res = await this.$axios.$post(`${api.addStudent(id)}`, ids);
  43. return res;
  44. },
  45. async upHeadTea({ commit }, payload) {
  46. const res = await this.$axios.$post(`${api.updateHeadTeacher}`, payload);
  47. return res;
  48. },
  49. async mergeRequest({ commit, dispatch }, { method, data }) {
  50. let toRequest = () => {
  51. let res = [];
  52. for (const i of data) {
  53. res.push(dispatch(method, i));
  54. }
  55. return res;
  56. };
  57. let result = await axios.all(toRequest());
  58. let newFilter = data => {
  59. let res = data.map(i => {
  60. let type = _.isArray(i);
  61. if (!type) {
  62. //fetch的多个请求 是object 将errcode为0的data取出来
  63. return _.get(i, `data`, i);
  64. } else {
  65. //query的多个请求 array 将此数据再次走这个方法
  66. return newFilter(i);
  67. }
  68. });
  69. return res;
  70. };
  71. let returns = _.flattenDeep(newFilter(result));
  72. return returns;
  73. },
  74. };
  75. export default {
  76. namespaced: true,
  77. state,
  78. mutations,
  79. actions,
  80. };