http.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { MessagePlugin } from 'tdesign-vue-next';
  2. import axios from 'axios';
  3. import { getCookie } from '@/services/cookie';
  4. import router from './router';
  5. import i18n from './i18n';
  6. const $t = i18n.global.t;
  7. import { axiosRequest, axiosResponse} from '@le5le/auth-token';
  8. // axios 配置
  9. axios.defaults.timeout = 60000;
  10. axios.defaults.withCredentials = false;
  11. axios.defaults.baseURL = `${import.meta.env.VITE_BASE_API}/api-dashboard`
  12. // axiosRequest(axios);
  13. // http request 拦截器
  14. // axios.interceptors.request.use(
  15. // (config: any) => {
  16. // config.headers.Authorization =
  17. // 'Bearer ' + ( getCookie('token') || new URLSearchParams(location.search).get('token') || '');
  18. // return config;
  19. // },
  20. // (err: any) => Promise.reject(err)
  21. // );
  22. // http response 拦截器
  23. // axiosResponse(axios);
  24. // axios.interceptors.response.use(
  25. // (response: any) => {
  26. // if (response && response.data && response.data.error) {
  27. // MessagePlugin.error(response.data.error);
  28. // return;
  29. // }
  30. // if (response) {
  31. // if (response.status == undefined) {
  32. // return response;
  33. // }
  34. // return response.data;
  35. // }
  36. // return;
  37. // },
  38. // (error: any) => {
  39. // if (error && !error.response) {
  40. // return;
  41. // }
  42. // if (error && error.response) {
  43. // if (
  44. // error.response.config.url === '/api/account/profile' ||
  45. // error.response.data.error === $t('此为付费数据,请购买后访问')
  46. // ) {
  47. // return;
  48. // }
  49. // if (
  50. // error.response.status !== 401 &&
  51. // error.response &&
  52. // error.response.data &&
  53. // error.response.data.error
  54. // ) {
  55. // MessagePlugin.error(error.response.data.error);
  56. // return;
  57. // }
  58. // switch (error.response.status) {
  59. // case 401:
  60. // if (error.response.config.url==='/api/account/statistics') {
  61. // break;
  62. // }
  63. // sessionStorage.setItem('cb', encodeURIComponent(location.href));
  64. // MessagePlugin.error($t('请先登录!'));
  65. // // router.replace({ path: '/login' });
  66. // break;
  67. // case 403:
  68. // MessagePlugin.error($t('请求错误,不合法的请求!'));
  69. // break;
  70. // case 404:
  71. // if (error.response.config.url.indexOf('/data/') !== 0) {
  72. // MessagePlugin.error($t('访问数据不存在,请检查后重试!'));
  73. // }
  74. // break;
  75. // case 500:
  76. // MessagePlugin.error($t('请求服务错误,请稍后重试!'));
  77. // break;
  78. // case 504:
  79. // MessagePlugin.error($t('网络超时,请检测你的网络!'));
  80. // break;
  81. // default:
  82. // MessagePlugin.error($t('未知网络错误!'));
  83. // break;
  84. // }
  85. // }
  86. // // return error.response ? error.response.data : error;
  87. // }
  88. // );
  89. axios.interceptors.request.use(
  90. (config: any) => {
  91. config.headers.Authorization = 'Bearer ' + import.meta.env.VITE_TEMP_TOKEN;
  92. return config;
  93. },
  94. (err: any) => Promise.reject(err)
  95. );
  96. axios.interceptors.response.use(
  97. (response: any) => {
  98. if (!response || !response.data) {
  99. console.error('Empty Response')
  100. return;
  101. }
  102. if (!response.headers['content-type'].startsWith('application/json')) {
  103. return response.data
  104. }
  105. const { data, code, msg } = response.data;
  106. if (code !== 0) {
  107. MessagePlugin.error(msg);
  108. return;
  109. }
  110. if (data === null || data === undefined) {
  111. MessagePlugin.error($t("请求数据为空"));
  112. return;
  113. }
  114. return data;
  115. },
  116. (error: any) => {
  117. if (!error?.response) {
  118. return;
  119. }
  120. const { status, data } = error.response
  121. if (status === 401) {
  122. MessagePlugin.error($t('请先登录!'));
  123. } else {
  124. const errMsg: string = data.msg || data.error || '';
  125. MessagePlugin.error(`HTTP Error: ${status} ${errMsg}`);
  126. }
  127. }
  128. );
  129. export default axios;