interceptor.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import axios from 'axios';
  2. import store from '../states/store';
  3. import { getApiBaseUrl } from './config';
  4. import { SystemMode } from '../states/systemModeSlice';
  5. import emitter from '../utils/eventEmitter';
  6. import { throttleCount } from '@/utils/throttle';
  7. const axiosInstance = axios.create({
  8. baseURL: getApiBaseUrl(),
  9. timeout: 30000, // 30秒超时,防止请求无限等待
  10. });
  11. function expensiveLog(mode) {
  12. console.log(`当前系统模式:${mode}`);
  13. }
  14. const log = throttleCount(expensiveLog, 60);
  15. axiosInstance.interceptors.request.use(
  16. (config) => {
  17. // ✅ 动态设置 baseURL,支持运行时修改
  18. config.baseURL = getApiBaseUrl();
  19. const state = store.getState();
  20. log(state.systemMode.mode);
  21. const token =
  22. state.systemMode.mode === SystemMode.Emergency
  23. ? state.product.guest
  24. : state.userInfo.token;
  25. const { productName, language, source } = state.product;
  26. config.headers.Authorization = `Bearer ${token}`;
  27. config.headers.Language = language;
  28. config.headers.Product = productName;
  29. config.headers.Source = source;
  30. return config;
  31. },
  32. (error) => {
  33. return Promise.reject(error);
  34. }
  35. );
  36. // 请求拦截器
  37. axiosInstance.interceptors.request.use(
  38. (config) => {
  39. if (config.url?.includes('/pub/quota')) {
  40. // 不打印配额查询请求日志
  41. return config;
  42. }
  43. console.log('【网络】🚀 请求:', {
  44. url: config.url,
  45. method: config.method,
  46. headers: config.headers,
  47. data: config.data,
  48. params: config.params,
  49. });
  50. return config;
  51. },
  52. (error) => {
  53. console.error('【网络】❌ 请求错误:', error);
  54. return Promise.reject(error);
  55. }
  56. );
  57. // 响应拦截器
  58. axiosInstance.interceptors.response.use(
  59. (response) => {
  60. if (response.data.code !== '0x000000') {
  61. console.error('【网络】❌ 响应错误,非"0x000000":', {
  62. url: response.config.url,
  63. status: response.status,
  64. data: response.data,
  65. });
  66. } else {
  67. if (!response.config.url?.includes('/pub/quota')) {
  68. // 非配额查询响应需要纪录日志
  69. console.log('【网络】✅ 响应:', {
  70. url: response.config.url,
  71. status: response.status,
  72. data: response.data,
  73. });
  74. }
  75. }
  76. return response;
  77. },
  78. (error) => {
  79. console.error('【网络】❌ 响应错误:', {
  80. url: error.config?.url,
  81. status: error.response?.status,
  82. message: error.message,
  83. data: error.response?.data,
  84. });
  85. return Promise.reject(error);
  86. }
  87. );
  88. axiosInstance.interceptors.response.use(
  89. (response) => {
  90. return response;
  91. },
  92. (error) => {
  93. if (error.response && error.response.data.code === 0x000101) {
  94. emitter.emit('tokenExpired');
  95. // eslint-disable-next-line
  96. return new Promise(() => { }); // Suspend the current execution
  97. }
  98. return Promise.reject(error);
  99. }
  100. );
  101. export default axiosInstance;