WEB中对于AXIOS封装

一、文档结构

 二、具体代码

//index.t最外部
//service统一出口
import HYRequest from './request';
import { BASE_URL, TIME_OUT } from './request/config';

const hyRequest = new HYRequest({
  baseURL: BASE_URL,
  timeout: TIME_OUT
});

export default hyRequest;
//request中index.ts
import axios, { AxiosInstance } from 'axios';
import { HYRequestInterceptors, HYRequestConfig } from './type';
import { ElLoading } from 'element-plus';
/**
 * 三种拦截器:1、全局拦截器 2、实例拦截器 3、单个请求拦截器
 * 控制显示loading:
 *          默认为显示,
 *          可以在创建请求实例时,指定是否显示,
 *          也可以对单个请求进行指定是否显示
 */

const DEFAULT_LOADING = true;
class HYRequest {
  instance: AxiosInstance;
  interceptors?: HYRequestInterceptors;
  showLoading: boolean;
  loading?: any;
  constructor(config: HYRequestConfig) {
    // 创建axios实例
    this.instance = axios.create(config);
    // 保存基本信息
    this.interceptors = config.interceptors;
    this.showLoading = config.showLoading ?? DEFAULT_LOADING;
    //从config中取出的拦截器是对应实例具有的拦截器
    this.instance.interceptors.request.use(
      this.interceptors?.requestInterceptor as AxiosInstance,
      this.interceptors?.requestInterceptorCatch
    );
    this.instance.interceptors.response.use(
      this.interceptors?.responseInterceptor,
      this.interceptors?.responseInterceptorCatch
    );
    // 添加所有实例具有的拦截器
    this.instance.interceptors.request.use(
      (config) => {
        // 控制实例的loading是否显示,默认为显示
        if (this.showLoading) {
          this.loading = ElLoading.service({
            lock: true,
            text: '正在请求数据。。。。',
            background: 'rgb(0, 0, 0, 0.5)'
          });
        }
        return config;
      },
      (err) => {
        return err;
      }
    );
    this.instance.interceptors.response.use(
      (res) => {
        // 关闭loading
        this.loading?.close();
        return res.data;
      },
      (err) => {
        // 关闭loading
        this.loading?.close();
        return err;
      }
    );
  }

  request<T>(config: HYRequestConfig<T>): Promise<T> {
    return new Promise((resolve, reject) => {
      // 单个请求拦截器
      if (config.interceptors?.requestInterceptor) {
        config = config.interceptors.requestInterceptor(config);
      }
      // 处理单个请求是否显示loading
      if (config.showLoading === false) {
        this.showLoading = false;
      }
      this.instance
        .request<any, T>(config)
        .then((res) => {
          // 单个请求拦截器
          if (config.interceptors?.responseInterceptor) {
            res = config.interceptors.responseInterceptor(res);
          }
          //将showLoading设置为true,这样不会影响下一个请求
          this.showLoading = DEFAULT_LOADING;
          // 返回结果
          resolve(res);
        })
        .catch((err) => {
          //将showLoading设置为true,这样不会影响下一个请求
          this.showLoading = DEFAULT_LOADING;
          // 返回结果
          reject(err);
        });
    });
  }
  get<T>(config: HYRequestConfig<T>): Promise<T> {
    return this.request({ ...config, method: 'GET' });
  }

  post<T>(config: HYRequestConfig<T>): Promise<T> {
    return this.request({ ...config, method: 'POST' });
  }

  patch<T>(config: HYRequestConfig<T>): Promise<T> {
    return this.request({ ...config, method: 'PATCH' });
  }

  delete<T>(config: HYRequestConfig<T>): Promise<T> {
    return this.request({ ...config, method: 'DELETE' });
  }
}

export default HYRequest;
//type.ts
import { AxiosRequestConfig, AxiosResponse } from 'axios';

export interface HYRequestInterceptors<T = AxiosResponse> {
  requestInterceptor?: (config: AxiosRequestConfig) => AxiosRequestConfig;
  requestInterceptorCatch?: (err: any) => any;
  responseInterceptor?: (res: T) => T;
  responseInterceptorCatch?: (err: any) => any;
}

export interface HYRequestConfig<T = AxiosResponse> extends AxiosRequestConfig {
  interceptors?: HYRequestInterceptors<T>;
  showLoading?: boolean;
}
//config.ts
let BASE_URL = '';
const TIME_OUT = 100000;

if (process.env.NODE_ENV == 'development') {
  BASE_URL = '';
} else if (process.env.NODE_ENV == 'production') {
  BASE_URL = 'http://baidu.com/production';
} else {
  BASE_URL = 'http://baidu.com/test';
}

export { BASE_URL, TIME_OUT };

三、使用

import hyRequest from './service';
hyRequest
  .get({
    url: '/getUserList',
    showLoading: false
  })
  .then((res) => {
    console.log(res);
  });

四、解释

其中有三种拦截器,分别为实例拦截器,全局拦截器和单个请求拦截器

其中有loading加载,分为实例loading和单个请求loading,可自由配置

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

国服第三切图仔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值