build(projects): 依赖升级,规范目录

This commit is contained in:
Soybean
2021-08-13 14:22:35 +08:00
parent d680e7d931
commit a0ec84588a
29 changed files with 857 additions and 561 deletions

View File

@ -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('请求错误~');
}
/**

View File

@ -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

View File

@ -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 => {

View File

@ -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];
}

View File

@ -0,0 +1,45 @@
import FormData from 'form-data';
import { isArray } from '@/utils';
type HandleFunc<T> = (...arg: any) => T;
type RequestError = any;
type RequestData = any;
type RequestResult = [RequestError, RequestData];
/**
* 对请求的结果数据进行格式化的处理
* @param handleFunc - 处理函数
* @param requests - 请求结果
*/
export function handleResponse<T>(handleFunc: HandleFunc<T>, ...requests: RequestResult[]) {
let handleData: any = null;
let error: any = null;
const hasError = requests.some(item => {
const isError = Boolean(item[0]);
if (isError) {
[error] = item;
}
return isError;
});
if (!hasError) {
handleData = handleFunc(...requests.map(item => item[1]));
}
return [error, handleData] as [any, T];
}
/**
* 接口为上传文件的类型时数据转换
*/
export async function transformFile(file: File[] | File, key: string) {
const formData = new FormData();
if (isArray(file)) {
await Promise.all(
(file as File[]).map(item => {
formData.append(key, item);
return true;
})
);
} else {
await formData.append(key, file);
}
return formData;
}