lesson.js 2.4 KB

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