You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

requestConfig.ts 3.1 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * @Author: 赵伟
  3. * @Date: 2024-03-25 13:52:54
  4. * @Description: 网络请求配置,详情请参考 https://umijs.org/docs/max/request
  5. */
  6. import type {
  7. AxiosError,
  8. AxiosRequestConfig,
  9. AxiosResponse,
  10. RequestConfig,
  11. RequestError,
  12. RequestOptions,
  13. } from '@umijs/max';
  14. import { message } from 'antd';
  15. import { clearSessionToken, getAccessToken } from './access';
  16. import { setRemoteMenu } from './services/session';
  17. import Loading from './utils/loading';
  18. import { gotoLoginPage } from './utils/ui';
  19. // [antd: Notification] You are calling notice in render which will break in React 18 concurrent mode. Please trigger in effect instead.
  20. const popupError = (error: string, skipErrorHandler: boolean | undefined = false) => {
  21. if (!skipErrorHandler) {
  22. // 直接调用 message.error 有时候不弹出来
  23. setTimeout(() => {
  24. message.error(error);
  25. }, 100);
  26. }
  27. };
  28. /**
  29. * Umi Max 网络请求配置
  30. * @doc https://umijs.org/docs/max/request#配置
  31. */
  32. export const requestConfig: RequestConfig = {
  33. timeout: 120 * 1000,
  34. requestInterceptors: [
  35. (url: string, options: AxiosRequestConfig) => {
  36. const headers = options.headers ?? {};
  37. const authHeader = headers['Authorization'];
  38. const isToken = headers['isToken'];
  39. const skipLoading = (options as RequestOptions)?.skipLoading;
  40. if (!authHeader && isToken !== false) {
  41. const accessToken = getAccessToken();
  42. if (accessToken) {
  43. headers['Authorization'] = `Bearer ${accessToken}`;
  44. }
  45. }
  46. if (!skipLoading) {
  47. Loading.show();
  48. }
  49. return { url, options };
  50. },
  51. ],
  52. responseInterceptors: [
  53. [
  54. (response: AxiosResponse) => {
  55. const { status, data, config } = response || {};
  56. const options = config as RequestOptions;
  57. const skipErrorHandler = options?.skipErrorHandler;
  58. const skipLoading = options?.skipLoading;
  59. if (!skipLoading) {
  60. Loading.hide();
  61. }
  62. if (status >= 200 && status < 300) {
  63. if (data && (data instanceof Blob || data.code === 200)) {
  64. return response;
  65. } else if (data && data.code === 401) {
  66. clearSessionToken();
  67. setRemoteMenu(null);
  68. gotoLoginPage(false);
  69. popupError('请重新登录');
  70. return Promise.reject(response);
  71. } else {
  72. popupError(data?.msg ?? '请求失败', skipErrorHandler);
  73. return Promise.reject(response);
  74. }
  75. } else {
  76. popupError('请求失败', skipErrorHandler);
  77. return Promise.reject(response);
  78. }
  79. },
  80. (error: RequestError) => {
  81. const options = (error as AxiosError).config as RequestOptions;
  82. const skipErrorHandler = options?.skipErrorHandler;
  83. const skipLoading = options?.skipLoading;
  84. if (!skipLoading) {
  85. Loading.hide();
  86. }
  87. popupError(error.message ?? '请求失败', skipErrorHandler);
  88. return Promise.reject(error);
  89. },
  90. ],
  91. ],
  92. };