mirror of
https://github.com/soybeanjs/soybean-admin.git
synced 2025-10-22 17:43:42 +08:00
refactor(projects): 请求函数重构初步
This commit is contained in:
@@ -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);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user