axios.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { Toast } from "antd-mobile";
  2. import axios, { AxiosInstance, AxiosResponse, InternalAxiosRequestConfig } from "axios";
  3. import type { AxiosOptions } from "./type";
  4. type Result<T = any> = {
  5. code: number;
  6. msg: string;
  7. data: T;
  8. };
  9. interface CustomRequestConfig {
  10. toast?: boolean;
  11. }
  12. export default class Request {
  13. private axiosInstance: AxiosInstance;
  14. private readonly options: AxiosOptions;
  15. constructor(options: AxiosOptions) {
  16. this.axiosInstance = axios.create(options);
  17. this.options = options;
  18. this.setupInterceptors();
  19. }
  20. setupInterceptors() {
  21. // 定义拦截器
  22. const {
  23. options: { transform },
  24. axiosInstance,
  25. } = this;
  26. // 如果没有自定义拦截器则返回
  27. if (!transform) {
  28. return;
  29. }
  30. // 解包拦截器
  31. const {
  32. requestInterceptor,
  33. requestInterceptorCatch,
  34. responseInterceptor,
  35. responseInterceptorCatch,
  36. } = transform;
  37. /**
  38. * @description 全局请求拦截
  39. */
  40. axiosInstance.interceptors.request.use(
  41. async (config: InternalAxiosRequestConfig & CustomRequestConfig) => {
  42. if (requestInterceptor) {
  43. config = requestInterceptor(config);
  44. // header请求头添加token
  45. let globalStore = window.localStorage.getItem("globalStore") || "";
  46. if (globalStore) {
  47. let userInfo = JSON.parse(globalStore);
  48. config.headers["Token"] = userInfo?.state?.token;
  49. }
  50. if (config && config?.toast) {
  51. Toast.show({
  52. icon: "loading",
  53. content: "请求中...",
  54. duration: 0,
  55. maskClickable: false,
  56. });
  57. }
  58. }
  59. return config;
  60. },
  61. (error) => {
  62. if (requestInterceptorCatch) {
  63. requestInterceptorCatch(error);
  64. }
  65. }
  66. );
  67. /**
  68. * @description 全局响应拦截
  69. */
  70. axiosInstance.interceptors.response.use(
  71. (res) => {
  72. Toast.clear();
  73. if (responseInterceptor) {
  74. res = responseInterceptor(res);
  75. }
  76. return res;
  77. },
  78. (error) => {
  79. if (responseInterceptorCatch) {
  80. responseInterceptorCatch(error);
  81. }
  82. Toast.clear();
  83. }
  84. );
  85. }
  86. /**
  87. * @description 单个实列请求
  88. */
  89. request<T = unknown>(config: AxiosOptions): Promise<Result<T>> {
  90. return new Promise((resolve, reject) => {
  91. this.axiosInstance
  92. .request<any, AxiosResponse<Result>>(config)
  93. .then((res) => {
  94. const { transform } = config;
  95. if (transform && transform.responseInterceptor) {
  96. res = transform.responseInterceptor(res);
  97. }
  98. resolve(res.data);
  99. })
  100. .catch((error) => {
  101. reject(error);
  102. });
  103. });
  104. }
  105. get<T>(config: AxiosOptions): Promise<Result<T>> {
  106. return this.request({ ...config, method: "get" });
  107. }
  108. post<T>(config: AxiosOptions): Promise<Result<T>> {
  109. return this.request({ ...config, method: "post" });
  110. }
  111. put<T>(config: AxiosOptions): Promise<Result<T>> {
  112. return this.request({ ...config, method: "put" });
  113. }
  114. delete<T>(config: AxiosOptions): Promise<Result<T>> {
  115. return this.request({ ...config, method: "delete" });
  116. }
  117. }