axios.js 1.4 KB

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