mirror of
https://github.com/m-xlsea/ruoyi-plus-soybean.git
synced 2025-09-24 07:49:47 +08:00
build(projects): 依赖升级,规范目录
This commit is contained in:
@ -14,10 +14,7 @@ const ERROR_STATUS = {
|
||||
504: '504: 网关超时~',
|
||||
505: '505: http版本不支持该请求~'
|
||||
};
|
||||
type ErrorStatus = 400 | 401 | 403 | 404 | 405 | 408 | 500 | 501 | 502 | 503 | 504 | 505;
|
||||
|
||||
/** 错误信息显示时间 */
|
||||
export const errorDuration = 3000 / 1000;
|
||||
type ErrorStatus = keyof typeof ERROR_STATUS;
|
||||
|
||||
/**
|
||||
* 网络请求错误状态处理
|
||||
@ -37,7 +34,7 @@ export function errorHandler(error: any): void {
|
||||
ElMessage.error('网络不可用~');
|
||||
return;
|
||||
}
|
||||
ElMessage.error('未知错误~');
|
||||
ElMessage.error('请求错误~');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,10 +1,6 @@
|
||||
import { createRequest } from './request';
|
||||
import { REQUEST_TIMEOUT, ContentType } from './config';
|
||||
import { REQUEST_TIMEOUT } from './config';
|
||||
|
||||
export { handleResponse } from './request';
|
||||
export { ContentType };
|
||||
|
||||
// emoss-admin
|
||||
export const adminRequest = createRequest({
|
||||
baseURL: import.meta.env.VITE_HTTP_URL_EMOSS_ADMIN as string,
|
||||
timeout: REQUEST_TIMEOUT
|
||||
|
@ -1,9 +1,11 @@
|
||||
import axios from 'axios';
|
||||
import qs from 'qs';
|
||||
import { getStorageToken } from '@/utils';
|
||||
import { ElMessage } from 'element-plus';
|
||||
import type { AxiosRequestConfig, AxiosInstance } from 'axios';
|
||||
import { ContentType } from '@/enum';
|
||||
import { getStorageToken } from '@/utils';
|
||||
import { errorHandler } from './errorHandler';
|
||||
import { transformFile } from '../utils';
|
||||
|
||||
export interface StatusConfig {
|
||||
/** 表明请求状态的属性key */
|
||||
@ -37,15 +39,20 @@ export default class CustomAxiosInstance {
|
||||
/** 设置请求拦截器 */
|
||||
setInterceptor(statusConfig: StatusConfig): void {
|
||||
this.instance.interceptors.request.use(
|
||||
config => {
|
||||
async config => {
|
||||
const handleConfig = { ...config };
|
||||
// content-type为application/x-www-form-urlencoded类型的data参数需要序列化
|
||||
if (handleConfig.headers['Content-Type'] === 'application/x-www-form-urlencoded') {
|
||||
// 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 = getStorageToken();
|
||||
|
||||
return handleConfig;
|
||||
},
|
||||
error => {
|
||||
|
@ -49,18 +49,3 @@ export function createRequest(axiosConfig: AxiosRequestConfig, statusConfig?: St
|
||||
const request = new Request(customInstance.instance);
|
||||
return request;
|
||||
}
|
||||
|
||||
/**
|
||||
* 对请求的结果数据进行格式化的处理
|
||||
* @param handleFunc - 处理函数
|
||||
* @param errors - 接收多个请求的错误
|
||||
* @param datas - 接收多个请求的数据
|
||||
*/
|
||||
export function handleResponse<T>(handleFunc: Function, errors: any[], datas: any[]) {
|
||||
let handleData = null;
|
||||
if (errors.every(error => !error)) {
|
||||
handleData = handleFunc(...datas);
|
||||
}
|
||||
const resError = errors.find(error => Boolean(error));
|
||||
return [resError, handleData] as [any, T];
|
||||
}
|
||||
|
Reference in New Issue
Block a user