import axios, { AxiosRequestConfig } from 'axios'; import { message } from 'ant-design-vue'; import { ERROR_CODE_MESSAGE } from './config'; type AxiosRequestConfigExtend = AxiosRequestConfig & { X_DISABLE_ERROR?: boolean; X_DISABLE_UNWRAP?: boolean; }; type AxiosRequestConfigExtendWithResponse = AxiosRequestConfig & { X_DISABLE_ERROR?: boolean; X_DISABLE_UNWRAP: true; }; type AxiosRequestConfigExtendWithoutResponse = AxiosRequestConfig & { X_DISABLE_ERROR?: boolean; X_DISABLE_UNWRAP?: false; }; interface Response { errCode: number; errMsg: string; success: boolean; data: T; } const instance = axios.create({ timeout: 50000, withCredentials: false, }); // 添加请求拦截器 instance.interceptors.request.use( function (config) { // 在发送请求之前做些什么 const token = localStorage.getItem('token') || ''; if (token) { config.headers = config.headers || {}; config.headers['token'] = token; } return config; }, function (error) { // 对请求错误做些什么 if (!(error && error.config && error.config.X_DISABLE_ERROR)) { message.error('请求出错了,请稍后刷新页面重试!'); } return Promise.reject(error); } ); // 添加响应拦截器 instance.interceptors.response.use( function (response) { // 对响应数据做点什么 const responseData = response.data; const responseConfig = response.config; const { X_DISABLE_ERROR, X_DISABLE_UNWRAP } = responseConfig as AxiosRequestConfigExtend; const { errCode, errMsg, errcode, errmsg } = responseData; const errCode2 = errCode === undefined ? errcode : errCode; const errMsg2 = errMsg === undefined ? errmsg : errMsg; if (errCode2 === 0) { // 这里有点trick // 这里看起来返回的是response和responseData // 但是在各个请求方法的then函数中存在这个逻辑`res => res.data` // 所以最终业务中使用的数据是response.data和responseData.data return X_DISABLE_UNWRAP ? response : responseData; } else { if (!X_DISABLE_ERROR) { message.error( ERROR_CODE_MESSAGE[errCode2] || errMsg2 || '请求出错了,请稍后刷新页面重试!' ); } return Promise.reject(responseData); } }, function (error) { // 对响应错误做点什么 if (!(error && error.config && error.config.X_DISABLE_ERROR)) { message.error('请求出错了,请稍后刷新页面重试!'); } return Promise.reject(error); } ); function request( config: AxiosRequestConfigExtendWithResponse ): Promise>; function request( config: AxiosRequestConfigExtendWithoutResponse ): Promise; function request(config: AxiosRequestConfigExtend) { return instance.request>(config).then(res => res.data); } function get( url: string, config: AxiosRequestConfigExtendWithResponse ): Promise>; function get( url: string, config?: AxiosRequestConfigExtendWithoutResponse ): Promise; function get(url: string, config?: AxiosRequestConfigExtend) { return instance.get>(url, config).then(res => res.data); } function myDelete( url: string, config: AxiosRequestConfigExtendWithResponse ): Promise>; function myDelete( url: string, config?: AxiosRequestConfigExtendWithoutResponse ): Promise; function myDelete(url: string, config?: AxiosRequestConfigExtend) { return instance.delete>(url, config).then(res => res.data); } function head( url: string, config: AxiosRequestConfigExtendWithResponse ): Promise>; function head( url: string, config?: AxiosRequestConfigExtendWithoutResponse ): Promise; function head(url: string, config?: AxiosRequestConfigExtend) { return instance.head>(url, config).then(res => res.data); } function options( url: string, config: AxiosRequestConfigExtendWithResponse ): Promise>; function options( url: string, config?: AxiosRequestConfigExtendWithoutResponse ): Promise; function options(url: string, config?: AxiosRequestConfigExtend) { return instance.options>(url, config).then(res => res.data); } function post( url: string, data: SafeAny, config: AxiosRequestConfigExtendWithResponse ): Promise>; function post( url: string, data?: SafeAny, config?: AxiosRequestConfigExtendWithoutResponse ): Promise; function post( url: string, data?: SafeAny, config?: AxiosRequestConfigExtend ) { return instance.post>(url, data, config).then(res => res.data); } function put( url: string, data: SafeAny, config: AxiosRequestConfigExtendWithResponse ): Promise>; function put( url: string, data?: SafeAny, config?: AxiosRequestConfigExtendWithoutResponse ): Promise; function put( url: string, data?: SafeAny, config?: AxiosRequestConfigExtend ) { return instance.put>(url, data, config).then(res => res.data); } function patch( url: string, data: SafeAny, config: AxiosRequestConfigExtendWithResponse ): Promise>; function patch( url: string, data?: SafeAny, config?: AxiosRequestConfigExtendWithoutResponse ): Promise; function patch( url: string, data?: SafeAny, config?: AxiosRequestConfigExtend ) { return instance.patch>(url, data, config).then(res => res.data); } export default { request, get, delete: myDelete, head, options, post, put, patch, };