instance.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import axios from 'axios';
  2. import type { AxiosResponse, AxiosError, AxiosInstance, AxiosRequestConfig } from 'axios';
  3. import { REFRESH_TOKEN_CODE } from '@/config';
  4. import {
  5. localStg,
  6. handleAxiosError,
  7. handleBackendError,
  8. handleResponseError,
  9. handleServiceResult,
  10. transformRequestData
  11. } from '@/utils';
  12. import { handleRefreshToken } from './helpers';
  13. /**
  14. * 封装axios请求类
  15. * @author Soybean<honghuangdc@gmail.com>
  16. */
  17. export default class CustomAxiosInstance {
  18. instance: AxiosInstance;
  19. backendConfig: Service.BackendResultConfig;
  20. /**
  21. *
  22. * @param axiosConfig - axios配置
  23. * @param backendConfig - 后端返回的数据配置
  24. */
  25. constructor(
  26. axiosConfig: AxiosRequestConfig,
  27. backendConfig: Service.BackendResultConfig = {
  28. codeKey: 'code',
  29. dataKey: 'data',
  30. msgKey: 'status',
  31. successCode: true
  32. }
  33. ) {
  34. this.backendConfig = backendConfig;
  35. this.instance = axios.create(axiosConfig);
  36. this.setInterceptor();
  37. }
  38. /** 设置请求拦截器 */
  39. setInterceptor() {
  40. this.instance.interceptors.request.use(
  41. async config => {
  42. const handleConfig = { ...config };
  43. if (handleConfig.headers) {
  44. // 数据转换
  45. const contentType = handleConfig.headers['Content-Type'] as UnionKey.ContentType;
  46. handleConfig.data = await transformRequestData(handleConfig.data, contentType);
  47. // 设置token
  48. handleConfig.headers.Authorization = localStg.get('token') || '';
  49. }
  50. return handleConfig;
  51. },
  52. (axiosError: AxiosError) => {
  53. const error = handleAxiosError(axiosError);
  54. return handleServiceResult(error, null);
  55. }
  56. );
  57. this.instance.interceptors.response.use(
  58. (async response => {
  59. const { status } = response;
  60. if (status === 200 || status < 300 || status === 304) {
  61. const backend = response.data;
  62. const { codeKey, dataKey, successCode } = this.backendConfig;
  63. // 请求成功
  64. if (backend[codeKey] === successCode) {
  65. return handleServiceResult(null, backend[dataKey]);
  66. }
  67. // token失效, 刷新token
  68. if (REFRESH_TOKEN_CODE.includes(backend[codeKey])) {
  69. const config = await handleRefreshToken(response.config);
  70. if (config) {
  71. return this.instance.request(config);
  72. }
  73. }
  74. const error = handleBackendError(backend, this.backendConfig);
  75. return handleServiceResult(error, null);
  76. }
  77. const error = handleResponseError(response);
  78. return handleServiceResult(error, null);
  79. }) as (response: AxiosResponse<any, any>) => Promise<AxiosResponse<any, any>>,
  80. (axiosError: AxiosError) => {
  81. const error = handleAxiosError(axiosError);
  82. return handleServiceResult(error, null);
  83. }
  84. );
  85. }
  86. }