lesson.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/lesson`,
  8. model: `/api/train/lessonmode`,
  9. arrange: id => `/api/train/lesson/autolesson/${id}`,
  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 modelQuery({ commit }, { skip = 0, limit, ...info } = {}) {
  35. const res = await this.$axios.$get(`${api.model}`, { skip, limit, ...info });
  36. return res;
  37. },
  38. async modelCreate({ commit }, payload) {
  39. const res = await this.$axios.$post(`${api.model}`, payload);
  40. return res;
  41. },
  42. async modelFetch({ commit }, payload) {
  43. const res = await this.$axios.$get(`${api.model}/${payload}`);
  44. return res;
  45. },
  46. async modelUpdate({ commit }, { id, ...data }) {
  47. const res = await this.$axios.$post(`${api.model}/update/${id}`, data);
  48. return res;
  49. },
  50. async modelDelete({ commit }, payload) {
  51. const res = await this.$axios.$delete(`${api.model}/${payload}`);
  52. return res;
  53. },
  54. async arrange({ commit }, payload) {
  55. const res = await this.$axios.$post(api.arrange(payload));
  56. return res;
  57. },
  58. async mergeRequest({ commit, dispatch }, { method, data }) {
  59. let toRequest = () => {
  60. let res = [];
  61. for (const i of data) {
  62. res.push(dispatch(method, i));
  63. }
  64. return res;
  65. };
  66. let result = await axios.all(toRequest());
  67. let newFilter = data => {
  68. let res = data.map(i => {
  69. let type = _.isArray(i);
  70. if (!type) {
  71. //fetch的多个请求 是object 将errcode为0的data取出来
  72. return _.get(i, `data`, i);
  73. } else {
  74. //query的多个请求 array 将此数据再次走这个方法
  75. return newFilter(i);
  76. }
  77. });
  78. return res;
  79. };
  80. let returns = _.flattenDeep(newFilter(result));
  81. return returns;
  82. },
  83. };
  84. export default {
  85. namespaced: true,
  86. state,
  87. mutations,
  88. actions,
  89. };