feat: 请求层泛型化并收紧默认超时
This commit is contained in:
+42
-32
@@ -2,11 +2,21 @@ import axios from 'axios';
|
||||
import { ElMessage } from "element-plus";
|
||||
import router from "@/router";
|
||||
|
||||
const basic_url = import.meta.env.VITE_BASE_URL;
|
||||
/** 后端统一响应包装:code === 200 表示业务成功 */
|
||||
export interface ApiResponse<T = any> {
|
||||
code: number;
|
||||
message?: string;
|
||||
data: T;
|
||||
}
|
||||
|
||||
const baseURL = import.meta.env.VITE_BASE_URL;
|
||||
|
||||
// 默认 30s:普通 CRUD 足够;AI 聚合等长耗时接口需在 api 层显式覆写 timeout
|
||||
const DEFAULT_TIMEOUT = 30_000;
|
||||
|
||||
const axiosInstance = axios.create({
|
||||
baseURL: basic_url,
|
||||
timeout: 600000,
|
||||
baseURL,
|
||||
timeout: DEFAULT_TIMEOUT,
|
||||
withCredentials: true
|
||||
});
|
||||
|
||||
@@ -51,43 +61,43 @@ const validateResponse = (res: any) => {
|
||||
return result;
|
||||
};
|
||||
|
||||
function get(url: string, params: Record<string, any>): Promise<any>;
|
||||
function get(url: string): Promise<any>;
|
||||
|
||||
function get(url: string, params: Record<string, any> = {}) {
|
||||
return axiosInstance.get(url, { params })
|
||||
const get = <T = any>(
|
||||
url: string,
|
||||
params: Record<string, any> = {},
|
||||
config: Record<string, any> = {},
|
||||
): Promise<ApiResponse<T>> =>
|
||||
axiosInstance.get(url, { params, ...config })
|
||||
.then(validateResponse)
|
||||
.catch(handleError);
|
||||
}
|
||||
|
||||
const post = (url: string, data: any = null, config: any = {}) => {
|
||||
|
||||
if (config.params) {
|
||||
// 如果传入 config.params,则作为 URL 参数
|
||||
return axiosInstance.post(url, data, { params: config.params, ...config })
|
||||
.then(validateResponse)
|
||||
.catch(handleError);
|
||||
} else {
|
||||
// 默认 POST JSON
|
||||
return axiosInstance.post(url, data, config)
|
||||
.then(validateResponse)
|
||||
.catch(handleError);
|
||||
}
|
||||
};
|
||||
|
||||
const put = (url: string, data: any, config: any = {}) => {
|
||||
return axiosInstance.put(url, data, config)
|
||||
const post = <T = any>(
|
||||
url: string,
|
||||
data: any = null,
|
||||
config: Record<string, any> = {},
|
||||
): Promise<ApiResponse<T>> =>
|
||||
axiosInstance.post(url, data, config)
|
||||
.then(validateResponse)
|
||||
.catch(handleError);
|
||||
};
|
||||
|
||||
const del = (url: string, params: Record<string, any> = {}, config: any = {}) => {
|
||||
return axiosInstance.delete(url, { params, ...config })
|
||||
const put = <T = any>(
|
||||
url: string,
|
||||
data: any,
|
||||
config: Record<string, any> = {},
|
||||
): Promise<ApiResponse<T>> =>
|
||||
axiosInstance.put(url, data, config)
|
||||
.then(validateResponse)
|
||||
.catch(handleError);
|
||||
};
|
||||
|
||||
const requestNotImplemented = (method: string) => {
|
||||
const del = <T = any>(
|
||||
url: string,
|
||||
params: Record<string, any> = {},
|
||||
config: Record<string, any> = {},
|
||||
): Promise<ApiResponse<T>> =>
|
||||
axiosInstance.delete(url, { params, ...config })
|
||||
.then(validateResponse)
|
||||
.catch(handleError);
|
||||
|
||||
const requestNotImplemented = () => {
|
||||
ElMessage.error("该功能暂不可用,请刷新后重试");
|
||||
throw new Error("该功能暂不可用,请刷新后重试");
|
||||
};
|
||||
@@ -98,7 +108,7 @@ const request = (method: string, url: string, paramsOrData: any) => {
|
||||
case 'post': return post(url, paramsOrData);
|
||||
case 'put': return put(url, paramsOrData);
|
||||
case 'delete': return del(url, paramsOrData);
|
||||
default: return requestNotImplemented(method);
|
||||
default: return requestNotImplemented();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user