|
|
|
@ -6,14 +6,13 @@ import { isFunction } from '/@/utils/is'; |
|
|
|
import { cloneDeep } from 'lodash-es'; |
|
|
|
|
|
|
|
import type { RequestOptions, CreateAxiosOptions, Result, UploadFileParams } from './types'; |
|
|
|
// import { ContentTypeEnum } from '/@/enums/httpEnum';
|
|
|
|
import { errorResult } from './const'; |
|
|
|
import { ContentTypeEnum } from '/@/enums/httpEnum'; |
|
|
|
|
|
|
|
export * from './axiosTransform'; |
|
|
|
|
|
|
|
/** |
|
|
|
* @description: axios模块 |
|
|
|
* @description: axios module |
|
|
|
*/ |
|
|
|
export class VAxios { |
|
|
|
private axiosInstance: AxiosInstance; |
|
|
|
@ -26,7 +25,7 @@ export class VAxios { |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @description: 创建axios实例 |
|
|
|
* @description: Create axios instance |
|
|
|
*/ |
|
|
|
private createAxios(config: CreateAxiosOptions): void { |
|
|
|
this.axiosInstance = axios.create(config); |
|
|
|
@ -42,7 +41,7 @@ export class VAxios { |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @description: 重新配置axios |
|
|
|
* @description: Reconfigure axios |
|
|
|
*/ |
|
|
|
configAxios(config: CreateAxiosOptions) { |
|
|
|
if (!this.axiosInstance) { |
|
|
|
@ -52,7 +51,7 @@ export class VAxios { |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @description: 设置通用header |
|
|
|
* @description: Set general header |
|
|
|
*/ |
|
|
|
setHeader(headers: any): void { |
|
|
|
if (!this.axiosInstance) { |
|
|
|
@ -62,7 +61,7 @@ export class VAxios { |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @description: 拦截器配置 |
|
|
|
* @description: Interceptor configuration |
|
|
|
*/ |
|
|
|
private setupInterceptors() { |
|
|
|
const transform = this.getTransform(); |
|
|
|
@ -78,7 +77,7 @@ export class VAxios { |
|
|
|
|
|
|
|
const axiosCanceler = new AxiosCanceler(); |
|
|
|
|
|
|
|
// 请求拦截器配置处理
|
|
|
|
// Request interceptor configuration processing
|
|
|
|
this.axiosInstance.interceptors.request.use((config: AxiosRequestConfig) => { |
|
|
|
// If cancel repeat request is turned on, then cancel repeat request is prohibited
|
|
|
|
const { |
|
|
|
@ -91,12 +90,12 @@ export class VAxios { |
|
|
|
return config; |
|
|
|
}, undefined); |
|
|
|
|
|
|
|
// 请求拦截器错误捕获
|
|
|
|
// Request interceptor error capture
|
|
|
|
requestInterceptorsCatch && |
|
|
|
isFunction(requestInterceptorsCatch) && |
|
|
|
this.axiosInstance.interceptors.request.use(undefined, requestInterceptorsCatch); |
|
|
|
|
|
|
|
// 响应结果拦截器处理
|
|
|
|
// Response result interceptor processing
|
|
|
|
this.axiosInstance.interceptors.response.use((res: AxiosResponse<any>) => { |
|
|
|
res && axiosCanceler.removePending(res.config); |
|
|
|
if (responseInterceptors && isFunction(responseInterceptors)) { |
|
|
|
@ -105,14 +104,14 @@ export class VAxios { |
|
|
|
return res; |
|
|
|
}, undefined); |
|
|
|
|
|
|
|
// 响应结果拦截器错误捕获
|
|
|
|
// Response result interceptor error capture
|
|
|
|
responseInterceptorsCatch && |
|
|
|
isFunction(responseInterceptorsCatch) && |
|
|
|
this.axiosInstance.interceptors.response.use(undefined, responseInterceptorsCatch); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @description: 文件上传 |
|
|
|
* @description: File Upload |
|
|
|
*/ |
|
|
|
uploadFile<T = any>(config: AxiosRequestConfig, params: UploadFileParams) { |
|
|
|
const formData = new window.FormData(); |
|
|
|
@ -145,9 +144,6 @@ export class VAxios { |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @description: 请求方法 |
|
|
|
*/ |
|
|
|
request<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> { |
|
|
|
let conf: AxiosRequestConfig = cloneDeep(config); |
|
|
|
const transform = this.getTransform(); |
|
|
|
|