axios.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import AxiosError from 'axios/lib/core/AxiosError';
  2. export default function ({ $axios, redirect }) {
  3. // $axios.defaults.dataKey = 'data';
  4. $axios.onRequest((config) => {
  5. config.headers.client = 7;
  6. return config;
  7. });
  8. $axios.onResponse((response) => {
  9. if (response.data.status !== 0) {
  10. // const err = new Error(
  11. // response.data.msg || response.data.data || '未知错误',
  12. // );
  13. // Object.assign(err, response);
  14. // return Promise.reject(err);
  15. // if (response.data.status === 502) {
  16. // }
  17. throw new AxiosError(
  18. response.data.msg || response.data.data || '未知错误',
  19. null,
  20. response.config,
  21. response.request,
  22. response,
  23. );
  24. }
  25. // else if (response.config.dataKey) {
  26. // response.data = response.data[response.config.dataKey];
  27. // return response;
  28. // }
  29. return response;
  30. });
  31. // $axios.onError((error) => {
  32. // const code = parseInt(error.response && error.response.status);
  33. // if (code === 400) {
  34. // redirect('/400');
  35. // }
  36. // });
  37. // globalThis.$axios = $axios;
  38. }