axios.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. if (Object.prototype.toString.call(response.data) === '[object Object]') {
  10. if ([6013, 6014].includes(response.data.status)) {
  11. // await $auth.logout();
  12. await $auth.reset();
  13. // await $auth.setUserToken(null);
  14. }
  15. if (response.data.status !== 0) {
  16. // const err = new Error(
  17. // response.data.msg || response.data.data || '未知错误',
  18. // );
  19. // Object.assign(err, response);
  20. // return Promise.reject(err);
  21. // if (response.data.status === 502) {
  22. // }
  23. throw new AxiosError(
  24. response.data.msg || response.data.data || '未知错误',
  25. null,
  26. response.config,
  27. response.request,
  28. response,
  29. );
  30. }
  31. }
  32. // else if (response.config.dataKey) {
  33. // response.data = response.data[response.config.dataKey];
  34. // return response;
  35. // }
  36. return response;
  37. });
  38. // $axios.onError((error) => {
  39. // const code = parseInt(error.response && error.response.status);
  40. // if (code === 400) {
  41. // redirect('/400');
  42. // }
  43. // });
  44. // globalThis.$axios = $axios;
  45. }