axios.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. // config.headers["lang"] = 111;
  50. }
  51. if (config && config?.toast) {
  52. Toast.show({
  53. icon: "loading",
  54. content: "请求中...",
  55. duration: 0,
  56. maskClickable: false,
  57. });
  58. }
  59. }
  60. return config;
  61. },
  62. (error) => {
  63. if (requestInterceptorCatch) {
  64. requestInterceptorCatch(error);
  65. }
  66. }
  67. );
  68. /**
  69. * @description 全局响应拦截
  70. */
  71. axiosInstance.interceptors.response.use(
  72. (res) => {
  73. Toast.clear();
  74. if (responseInterceptor) {
  75. res = responseInterceptor(res);
  76. }
  77. return res;
  78. },
  79. (error) => {
  80. if (responseInterceptorCatch) {
  81. responseInterceptorCatch(error);
  82. }
  83. Toast.clear();
  84. }
  85. );
  86. }
  87. /**
  88. * @description 单个实列请求
  89. */
  90. request<T = unknown>(config: AxiosOptions): Promise<Result<T>> {
  91. return new Promise((resolve, reject) => {
  92. this.axiosInstance
  93. .request<any, AxiosResponse<Result>>(config)
  94. .then((res) => {
  95. const { transform } = config;
  96. if (transform && transform.responseInterceptor) {
  97. res = transform.responseInterceptor(res);
  98. }
  99. if (res && res.data && res.data.code === 200) {
  100. resolve(res?.data);
  101. } else {
  102. reject(res);
  103. }
  104. })
  105. .catch((error) => {
  106. reject(error);
  107. });
  108. });
  109. }
  110. get<T>(config: AxiosOptions): Promise<Result<T>> {
  111. return this.request({ ...config, method: "get" });
  112. }
  113. post<T>(config: AxiosOptions): Promise<Result<T>> {
  114. return this.request({ ...config, method: "post" });
  115. }
  116. put<T>(config: AxiosOptions): Promise<Result<T>> {
  117. return this.request({ ...config, method: "put" });
  118. }
  119. delete<T>(config: AxiosOptions): Promise<Result<T>> {
  120. return this.request({ ...config, method: "delete" });
  121. }
  122. }