4e1c71e4d5
- axios 全局超时 5s → 180s(3 分钟),避免 AI 响应未到即断开 - 回忆对比按钮等待时显示旋转动画 + 已等待秒数 + 30s 后安抚文案 - 结束会话报告草稿生成时显示等待提示 + 秒数统计
104 lines
3.5 KiB
TypeScript
104 lines
3.5 KiB
TypeScript
import axios from 'axios';
|
|
import { ElMessage } from "element-plus";
|
|
import router from "@/router";
|
|
|
|
const basic_url = import.meta.env.VITE_BASE_URL;
|
|
|
|
const axiosInstance = axios.create({
|
|
baseURL: basic_url,
|
|
timeout: 180000,
|
|
withCredentials: true
|
|
});
|
|
|
|
const handleError = (error: any) => {
|
|
console.log(error);
|
|
|
|
// 401 未授权:token 过期或未登录,直接跳转登录页
|
|
if (error?.response?.status === 401) {
|
|
localStorage.removeItem("isLoggedIn");
|
|
router.push("/login");
|
|
return Promise.reject(error);
|
|
}
|
|
|
|
// 业务异常由 validateResponse 负责提示,这里透传,避免被网络错误文案覆盖。
|
|
if (error && typeof error === "object" && "code" in error && !error.response) {
|
|
return Promise.reject(error);
|
|
}
|
|
|
|
if (error.response) {
|
|
const backendMessage = error.response?.data?.message || JSON.stringify(error.response?.data);
|
|
ElMessage.error(`请求失败,服务器返回: ${backendMessage}`);
|
|
} else if (error.request) {
|
|
ElMessage.error("请求未收到响应,请检查接口运行状态");
|
|
} else {
|
|
ElMessage.error(error.message || "请求失败");
|
|
}
|
|
throw new Error("网络不通畅,请检查您的网络连接或服务器状态");
|
|
};
|
|
|
|
const validateResponse = (res: any) => {
|
|
const result = res.data;
|
|
if (result.code !== 200) {
|
|
ElMessage.error(result.message || '请求失败');
|
|
return Promise.reject(result);
|
|
}
|
|
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> = {}) {
|
|
console.log("开始GET请求", basic_url + url, "参数:", params);
|
|
return axiosInstance.get(url, { params })
|
|
.then(validateResponse)
|
|
.catch(handleError);
|
|
}
|
|
|
|
const post = (url: string, data: any = null, config: any = {}) => {
|
|
console.log("开始POST请求", url, "参数:", data, "配置:", config);
|
|
|
|
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 = {}) => {
|
|
console.log("开始PUT请求", basic_url + url, "参数:", data);
|
|
return axiosInstance.put(url, data, config)
|
|
.then(validateResponse)
|
|
.catch(handleError);
|
|
};
|
|
|
|
const del = (url: string, params: Record<string, any> = {}, config: any = {}) => {
|
|
console.log("开始DELETE请求", basic_url + url, "参数:", params);
|
|
return axiosInstance.delete(url, { params, ...config })
|
|
.then(validateResponse)
|
|
.catch(handleError);
|
|
};
|
|
|
|
const requestNotImplemented = (method: string) => {
|
|
ElMessage.error(`请求方法 ${method} 未实现`);
|
|
throw new Error(`请求方法 ${method} 未实现`);
|
|
};
|
|
|
|
const request = (method: string, url: string, paramsOrData: any) => {
|
|
switch (method.toLowerCase()) {
|
|
case 'get': return get(url, paramsOrData);
|
|
case 'post': return post(url, paramsOrData);
|
|
case 'put': return put(url, paramsOrData);
|
|
case 'delete': return del(url, paramsOrData);
|
|
default: return requestNotImplemented(method);
|
|
}
|
|
};
|
|
|
|
export default { get, post, put, del, request };
|