axios.js 1.1 KB

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