nation.js 1.7 KB

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