Initial commit
This commit is contained in:
102
src/utils/request.ts
Normal file
102
src/utils/request.ts
Normal file
@ -0,0 +1,102 @@
|
||||
/**
|
||||
* request 网络请求工具
|
||||
* 更详细的 api 文档: https://github.com/umijs/umi-request
|
||||
*/
|
||||
import { extend } from 'umi-request';
|
||||
import { message } from 'antd';
|
||||
import { history } from 'umi';
|
||||
|
||||
const codeMessage = {
|
||||
200: '服务器成功返回请求的数据。',
|
||||
201: '新建或修改数据成功。',
|
||||
202: '一个请求已经进入后台排队(异步任务)。',
|
||||
204: '删除数据成功。',
|
||||
400: '发出的请求有错误,服务器没有进行新建或修改数据的操作。',
|
||||
401: '用户没有权限(令牌、用户名、密码错误)。',
|
||||
403: '用户得到授权,但是访问是被禁止的。',
|
||||
404: '发出的请求针对的是不存在的记录,服务器没有进行操作。',
|
||||
406: '请求的格式不可得。',
|
||||
410: '请求的资源被永久删除,且不会再得到的。',
|
||||
422: '当创建一个对象时,发生一个验证错误。',
|
||||
500: '服务器发生错误,请检查服务器。',
|
||||
502: '网关错误。',
|
||||
503: '服务不可用,服务器暂时过载或维护。',
|
||||
504: '网关超时。',
|
||||
};
|
||||
|
||||
/**
|
||||
* 异常处理程序
|
||||
*/
|
||||
// const errorHandler = (error: { response: Response }): Response => {
|
||||
// const errorHandler = (error: any): Response => {
|
||||
// const { response } = error;
|
||||
// if (response && response.status) {
|
||||
// const errorText = codeMessage[response.status] || response.statusText;
|
||||
// const { status, url } = response;
|
||||
// let mes = error.data?.message == '' ? '请求数据出错,请联系客服人员!' : error.data?.message;
|
||||
// // notification.error({
|
||||
// // // message: `请求错误 ${status}: ${url}`,
|
||||
// // message: `请求错误 ${status}:`,
|
||||
// // // description: errorText,
|
||||
// // description: mes,
|
||||
// // duration: 5,
|
||||
// // });
|
||||
// message.error(mes);
|
||||
// } else if (!response) {
|
||||
// message.error('您的网络发生异常,无法连接服务器');
|
||||
// }
|
||||
// return response;
|
||||
// };
|
||||
|
||||
/**
|
||||
* 配置request请求时的默认参数
|
||||
*/
|
||||
const request = extend({
|
||||
// errorHandler, // 默认错误处理
|
||||
credentials: 'include' // 默认请求是否带上cookie
|
||||
});
|
||||
// request拦截器, 改变url 或 options.
|
||||
request.interceptors.request.use(async (url, options) => {
|
||||
if (
|
||||
options.method === 'post' ||
|
||||
options.method === 'put' ||
|
||||
options.method === 'delete' ||
|
||||
options.method === 'get'
|
||||
) {
|
||||
//如果是获取token的url,则不加token
|
||||
// headers = {
|
||||
// Authorization: getUserToken() == null ? null : getUserToken(),
|
||||
// JwtToken: getSessionUserData() == null ? null : getSessionUserData().userId,
|
||||
// ...options.headers
|
||||
// }
|
||||
// options.headers = headers;
|
||||
}
|
||||
return {
|
||||
url,
|
||||
options: { ...options },
|
||||
};
|
||||
});
|
||||
//response拦截
|
||||
request.interceptors.response.use(async (response) => {
|
||||
const data = await response.clone().json();
|
||||
|
||||
//返回请求头状态码为401
|
||||
if (
|
||||
(response.status === 401 && data?.code == '90401') ||
|
||||
(response.status === 500 && data?.code == '90500')
|
||||
) {
|
||||
history.replace({
|
||||
pathname: '/401',
|
||||
query: {
|
||||
code: '90401',
|
||||
},
|
||||
});
|
||||
} else {
|
||||
//2021.9.7 zhoujianlong 新增风险防控专用错误码4004
|
||||
if (data.code != undefined && data.code != '200' && data.code != '4004' && data.code !== '1') {
|
||||
message.error(data.message, 3);
|
||||
}
|
||||
}
|
||||
return response;
|
||||
});
|
||||
export default request;
|
35
src/utils/utils.ts
Normal file
35
src/utils/utils.ts
Normal file
@ -0,0 +1,35 @@
|
||||
import { message,Upload } from 'antd';
|
||||
|
||||
// 验证上传文件大小是否符合要求
|
||||
// 参数说明:
|
||||
// file: 上传的文件
|
||||
// maxSize: 最大文件大小
|
||||
// type: 文件类型,支持*表示所有类型
|
||||
const FileTypeMap = {
|
||||
'application/pdf': 'pdf',
|
||||
'image/jpeg': 'jpg',
|
||||
'image/png': 'png',
|
||||
'application/msword': 'doc',
|
||||
'application/zip': 'zip',
|
||||
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': 'docx',
|
||||
'application/vnd.ms-excel': 'xls',
|
||||
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 'xlsx',
|
||||
'application/vnd.ms-powerpoint': 'ppt',
|
||||
'application/vnd.openxmlformats-officedocument.presentationml.presentation': 'pptx',
|
||||
|
||||
}
|
||||
export const validateFileSize = (file: File, maxSize: number,type: string[]) => {
|
||||
console.log(file)
|
||||
const { LIST_IGNORE } = Upload;
|
||||
const isLtMaxSize = file.size / 1024 / 1024 < maxSize;
|
||||
if (!isLtMaxSize) {
|
||||
message.error(`文件大小不能超过${maxSize}MB!`);
|
||||
return LIST_IGNORE;
|
||||
}
|
||||
const isValidFormat = type.includes('*') || type.includes(FileTypeMap[file.type as keyof typeof FileTypeMap]);
|
||||
if (!isValidFormat) {
|
||||
message.error(`只能上传${type.join(',')}格式文件!`);
|
||||
return LIST_IGNORE;
|
||||
}
|
||||
return isValidFormat && isLtMaxSize;
|
||||
};
|
Reference in New Issue
Block a user