instance.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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: 'status',
  29. dataKey: 'data',
  30. msgKey: 'code',
  31. successCode: true,
  32. total:'total'
  33. }
  34. ) {
  35. this.backendConfig = backendConfig;
  36. this.instance = axios.create(axiosConfig);
  37. this.setInterceptor();
  38. }
  39. /** 设置请求拦截器 */
  40. setInterceptor() {
  41. this.instance.interceptors.request.use(
  42. async config => {
  43. const handleConfig = { ...config };
  44. console.log(handleConfig.headers);
  45. if (handleConfig.headers) {
  46. // 数据转换
  47. const contentType = handleConfig.headers['Content-Type'] as UnionKey.ContentType;
  48. handleConfig.data = await transformRequestData(handleConfig.data, contentType);
  49. // 设置token
  50. handleConfig.headers["Authorization"]= localStorage.getItem('token') ;
  51. }
  52. return handleConfig;
  53. },
  54. (axiosError: AxiosError) => {
  55. const error = handleAxiosError(axiosError);
  56. return handleServiceResult(error, null,null);
  57. }
  58. );
  59. this.instance.interceptors.response.use(
  60. (async response => {
  61. const { status } = response;
  62. if (status === 200 || status < 300 || status === 304 ) {
  63. const backend = response.data;
  64. const { codeKey, dataKey, successCode,total} = this.backendConfig;
  65. // 请求成功
  66. if (backend[codeKey] === successCode ) {
  67. return handleServiceResult(null, backend[dataKey],backend[total]);
  68. }
  69. // token失效, 刷新token
  70. if (REFRESH_TOKEN_CODE.includes(backend[codeKey])) {
  71. const config = await handleRefreshToken(response.config);
  72. if (config) {
  73. return this.instance.request(config);
  74. }
  75. }
  76. const error = handleBackendError(backend, this.backendConfig);
  77. return handleServiceResult(error, null,null);
  78. }
  79. const error = handleResponseError(response);
  80. return handleServiceResult(error, null,null);
  81. }) as (response: AxiosResponse<any, any>) => Promise<AxiosResponse<any, any>>,
  82. (axiosError: AxiosError) => {
  83. const error = handleAxiosError(axiosError);
  84. return handleServiceResult(error, null,null);
  85. }
  86. );
  87. }
  88. }