fix: 全面补充前端交互状态反馈
- client.ts: 添加全局错误拦截器,自动提取后端 detail/message/msg 字段展示 toast - 处理网络异常、超时、5xx 等场景 - 通过 __msgShown 标记避免与组件 onError 重复弹提示 - 6 个 delete mutation 补充 onError 回退提示(AssetLibrary、TitleLibrary、 VoiceLibrary、ProductLibrary、DuplicationResults×2) - GeneratePage: 5 个 query 增加 loading + error 状态展示 - TemplateLibrary: favMutation 增加收藏/取消收藏成功提示及失败回退 - Dashboard: 增加 isError 错误状态展示,避免静默显示全零数据 - DuplicationDetail: 增加 isError 状态,区分加载失败与记录不存在
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
* 封装 Axios 实例,配置拦截器和 Token 管理
|
||||
*/
|
||||
import axios, { AxiosError, InternalAxiosRequestConfig } from 'axios';
|
||||
import { message } from 'antd';
|
||||
import { useAuthStore } from '@/store/authStore';
|
||||
|
||||
// 创建 Axios 实例
|
||||
@@ -28,14 +29,43 @@ apiClient.interceptors.request.use(
|
||||
}
|
||||
);
|
||||
|
||||
// 响应拦截器:处理未授权状态
|
||||
// 响应拦截器:统一错误提示 + 处理未授权状态
|
||||
apiClient.interceptors.response.use(
|
||||
(response) => response,
|
||||
async (error: AxiosError) => {
|
||||
async (error: AxiosError<{ detail?: string; message?: string; msg?: string }>) => {
|
||||
// 401 → 清除登录态
|
||||
if (error.response?.status === 401) {
|
||||
useAuthStore.getState().clearAuth();
|
||||
}
|
||||
|
||||
// 提取后端返回的错误信息(detail / message / msg)
|
||||
const data = error.response?.data;
|
||||
const serverMsg = data?.detail || data?.message || data?.msg;
|
||||
let handled = false;
|
||||
|
||||
if (error.code === 'ECONNABORTED' || error.message?.includes('timeout')) {
|
||||
message.error('请求超时,请检查网络后重试');
|
||||
handled = true;
|
||||
} else if (!error.response) {
|
||||
message.error('网络连接异常,请检查网络设置');
|
||||
handled = true;
|
||||
} else if (serverMsg) {
|
||||
message.error(serverMsg);
|
||||
handled = true;
|
||||
} else {
|
||||
const status = error.response?.status;
|
||||
if (status && status >= 500) {
|
||||
message.error('服务器繁忙,请稍后再试');
|
||||
handled = true;
|
||||
}
|
||||
// 4xx 且无具体信息时不弹通用提示,由各组件自行处理
|
||||
}
|
||||
|
||||
// 标记已展示过提示,组件 onError 可据此跳过重复 toast
|
||||
if (handled) {
|
||||
(error as any).__msgShown = true;
|
||||
}
|
||||
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user