Files
live-hub-py/web/frontend/src/api/client.ts
T
yml2213 27e9107e38 fix(accounts): 修复鱼翅筛选参数类型为浮点数并优化错误提示
1. 后端将 min_gold 与 max_gold 参数及 Schema 类型从 int 改为 float | None,支持小数鱼翅(如 >0 快捷筛选的 0.01);
2. 修复前端 axios 拦截器在遇到 FastAPI 422 验证错误时未展开 detail 导致弹出 [object Object] 的问题;
3. 单元测试补充验证浮点数鱼翅过滤场景。
2026-10-05 11:36:46 +08:00

33 lines
1.1 KiB
TypeScript

import axios from 'axios';
const api = axios.create({
baseURL: '/api',
timeout: 30000,
withCredentials: true, // 携带 httpOnly cookie
});
// 响应拦截:统一错误处理
api.interceptors.response.use(
(response) => response.data,
(error) => {
// 仅在“已登录后 token 失效”时清除登录态并跳转登录页。
// 登录接口本身返回 401(用户名/密码错误)时不应跳转,否则会吞掉错误提示。
if (error.response?.status === 401 && error.config?.url !== '/auth/login') {
localStorage.removeItem('user');
window.location.href = '/login';
}
const detail = error.response?.data?.detail;
let msg = error.message || '请求失败';
if (typeof detail === 'string') {
msg = detail;
} else if (Array.isArray(detail) && detail.length > 0) {
msg = detail.map((d: any) => d?.msg || (typeof d === 'string' ? d : JSON.stringify(d))).join('; ');
} else if (detail && typeof detail === 'object') {
msg = JSON.stringify(detail);
}
return Promise.reject(new Error(msg));
}
);
export default api;