refactor(projects): 请求函数重构初步

This commit is contained in:
Soybean
2021-11-22 00:14:12 +08:00
parent e44f5d72a2
commit 9f64321d73
15 changed files with 185 additions and 165 deletions

View File

@@ -1,85 +0,0 @@
import axios from 'axios';
import qs from 'qs';
import type { AxiosRequestConfig, AxiosInstance } from 'axios';
import { ContentType } from '@/enum';
import { getToken } from '@/utils';
import { transformFile, errorHandler } from '../utils';
export interface StatusConfig {
/** 表明请求状态的属性key */
statusKey: string;
/** 请求信息的属性key */
msgKey: string;
/** 成功状态的状态码 */
successCode: string | number;
}
/**
* 封装axios请求类
* @author Soybean(曹理斌) 2021-03-13
* @class CustomAxiosInstance
*/
export default class CustomAxiosInstance {
instance: AxiosInstance;
constructor(
axiosConfig: AxiosRequestConfig,
statusConfig: StatusConfig = {
statusKey: 'code',
msgKey: 'message',
successCode: 200
}
) {
this.instance = axios.create(axiosConfig);
this.setInterceptor(statusConfig);
}
/** 设置请求拦截器 */
setInterceptor(statusConfig: StatusConfig): void {
this.instance.interceptors.request.use(
async config => {
const handleConfig = { ...config };
if (handleConfig.headers) {
// form类型转换
if (handleConfig.headers['Content-Type'] === ContentType.formUrlencoded) {
handleConfig.data = qs.stringify(handleConfig.data);
}
// 文件类型转换
if (handleConfig?.headers['Content-Type'] === ContentType.formData) {
const key = Object.keys(handleConfig.data)[0];
const file = handleConfig.data[key];
handleConfig.data = await transformFile(file, key);
}
// 设置token
handleConfig.headers.Authorization = getToken();
}
return handleConfig;
},
error => {
errorHandler(error);
return Promise.reject(error);
}
);
this.instance.interceptors.response.use(
response => {
const { status, data } = response;
const { statusKey, msgKey, successCode } = statusConfig;
if (status === 200 || status < 300 || status === 304) {
const responseData = data as any;
if (responseData[statusKey] === successCode) {
return Promise.resolve(responseData.data);
}
window.$message?.error(responseData[msgKey]);
return Promise.reject(responseData[msgKey]);
}
const error = { response };
errorHandler(error);
return Promise.reject(error);
},
error => {
errorHandler(error);
return Promise.reject(error);
}
);
}
}