mirror of
https://github.com/m-xlsea/ruoyi-plus-soybean.git
synced 2025-09-24 07:49:47 +08:00
63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
import type { AxiosRequestConfig, AxiosInstance, AxiosResponse } from 'axios';
|
|
import type { RequestServiceError, CustomSuccessRequestResult, CustomFailRequestResult } from '@/interface';
|
|
|
|
/**
|
|
* 封装各个请求方法及结果处理的类
|
|
* @author Soybean<honghuangdc@gmail.com>
|
|
*/
|
|
export default class Request {
|
|
instance: AxiosInstance;
|
|
|
|
constructor(instance: AxiosInstance) {
|
|
this.instance = instance;
|
|
}
|
|
|
|
static successHandler<ResponseData>(response: AxiosResponse) {
|
|
const successResult: CustomSuccessRequestResult<ResponseData> = {
|
|
data: response as unknown as ResponseData,
|
|
error: null,
|
|
networkStatus: window.navigator.onLine
|
|
};
|
|
|
|
return successResult;
|
|
}
|
|
|
|
static failHandler(error: RequestServiceError) {
|
|
const failResult: CustomFailRequestResult = {
|
|
data: null,
|
|
error,
|
|
networkStatus: window.navigator.onLine
|
|
};
|
|
|
|
return failResult;
|
|
}
|
|
|
|
get<ResponseData>(url: string, config?: AxiosRequestConfig) {
|
|
return this.instance
|
|
.get(url, config)
|
|
.then(res => Request.successHandler<ResponseData>(res))
|
|
.catch(Request.failHandler);
|
|
}
|
|
|
|
post<ResponseData>(url: string, data?: any, config?: AxiosRequestConfig) {
|
|
return this.instance
|
|
.post(url, data, config)
|
|
.then(res => Request.successHandler<ResponseData>(res))
|
|
.catch(Request.failHandler);
|
|
}
|
|
|
|
put<ResponseData>(url: string, data?: any, config?: AxiosRequestConfig) {
|
|
return this.instance
|
|
.put(url, data, config)
|
|
.then(res => Request.successHandler<ResponseData>(res))
|
|
.catch(Request.failHandler);
|
|
}
|
|
|
|
delete<ResponseData>(url: string, config?: AxiosRequestConfig) {
|
|
return this.instance
|
|
.delete(url, config)
|
|
.then(res => Request.successHandler<ResponseData>(res))
|
|
.catch(Request.failHandler);
|
|
}
|
|
}
|