新项目, antd6, react19
This commit is contained in:
79
src/utils/useRequest2.ts
Normal file
79
src/utils/useRequest2.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
// import { OSSBaseUrl } from '@/config/config';
|
||||
import type { NotificationInstance } from 'antd/es/notification/interface';
|
||||
import { dingRequest } from './common';
|
||||
import { post } from './http';
|
||||
|
||||
type IConfig = {
|
||||
notification?: NotificationInstance;
|
||||
};
|
||||
|
||||
/**
|
||||
* 轻量级请求
|
||||
* @param url 请求地址
|
||||
* @param data 请求参数
|
||||
* @param config 请求配置项 + 提示组件 {}
|
||||
* @returns Promise<any>
|
||||
* */
|
||||
export const requestLite = async (url: string, data?: any, config?: IConfig) => {
|
||||
const { notification, ...option } = config || {};
|
||||
try {
|
||||
const res = await post(url, data, option);
|
||||
if (res?.err_code != 0) {
|
||||
if (res?.err_code == 110000) {
|
||||
location.href = '#/login';
|
||||
} else {
|
||||
if (notification) {
|
||||
notification.error({
|
||||
title: `${'错误码'}:${res.err_code}`,
|
||||
description: res.err_msg,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
} catch (error: any) {
|
||||
console.log(error);
|
||||
if (notification) {
|
||||
notification.error({
|
||||
title: `${'服务错误'}`,
|
||||
description: `${error?.message}, ${error?.response?.data?.message || ''}`,
|
||||
});
|
||||
}
|
||||
const msg = `接口地址:${url}\n响应信息:${error?.message}\n错误信息:${JSON.stringify(
|
||||
error?.response?.data || '',
|
||||
).replace(/"/g, '')}\n请求参数:${JSON.stringify(data || '').replace(/"/g, '')}`;
|
||||
dingRequest(msg || '错误');
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 请求获取文件
|
||||
* @param url 文件地址
|
||||
* @param options 参数
|
||||
* @param options.oss 判断是不是 oss文件, 默认 true
|
||||
* @returns Promise<File>
|
||||
*/
|
||||
// export const requestFile = async (
|
||||
// url: string,
|
||||
// options: { oss?: boolean } = { oss: true },
|
||||
// ) => {
|
||||
// const u = url.startsWith("/") ? url.substring(1) : url;
|
||||
// return new Promise<Blob>((resolve, reject) => {
|
||||
// try {
|
||||
// fetch(options.oss ? `${OSSBaseUrl}${u}` : url, { mode: "cors" })
|
||||
// .then((response) => {
|
||||
// if (!response.ok) {
|
||||
// throw new Error(`HTTP 错误!状态:${response.status}`);
|
||||
// }
|
||||
// return response.blob();
|
||||
// })
|
||||
// .then((res) => {
|
||||
// // const fileName = url.split('/').pop() || '';
|
||||
// // resolve(blobToFile([res], fileName));
|
||||
// resolve(res);
|
||||
// });
|
||||
// } catch (error) {
|
||||
// reject(error);
|
||||
// }
|
||||
// });
|
||||
// };
|
||||
Reference in New Issue
Block a user