axios.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import AxiosError from 'axios/lib/core/AxiosError';
  2. export default function ({ $axios, $auth, redirect }) {
  3. // $axios.defaults.dataKey = 'data';
  4. $axios.onRequest((config) => {
  5. config.headers.client = 7;
  6. return config;
  7. });
  8. $axios.onResponse(async (response) => {
  9. // console.log("🚀 ~ file: axios.js ~ line 11 ~ $axios.onResponse ~ response", response)
  10. if (Object.prototype.toString.call(response.data) === '[object Object]') {
  11. if ([6013, 6014].includes(response.data.status)) {
  12. // await $auth.logout();
  13. await $auth.reset();
  14. // await $auth.setUserToken(null);
  15. }
  16. if (response.data.status !== 0) {
  17. // const err = new Error(
  18. // response.data.msg || response.data.data || '未知错误',
  19. // );
  20. // Object.assign(err, response);
  21. // return Promise.reject(err);
  22. // if (response.data.status === 502) {
  23. // }
  24. throw new AxiosError(
  25. response.data.msg || response.data.data || '未知错误',
  26. null,
  27. response.config,
  28. response.request,
  29. response,
  30. );
  31. }
  32. }
  33. // else if (response.config.dataKey) {
  34. // response.data = response.data[response.config.dataKey];
  35. // return response;
  36. // }
  37. return response;
  38. });
  39. // $axios.onError((error) => {
  40. // const code = parseInt(error.response && error.response.status);
  41. // if (code === 400) {
  42. // redirect('/400');
  43. // }
  44. // });
  45. // globalThis.$axios = $axios;
  46. }