From 4143018faafd034e2dc2dea4a738c79cc01ddb1d Mon Sep 17 00:00:00 2001 From: Audit Bot Date: Mon, 29 Jun 2026 09:35:08 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=85=A8=E9=9D=A2=E8=A1=A5=E5=85=85?= =?UTF-8?q?=E5=89=8D=E7=AB=AF=E4=BA=A4=E4=BA=92=E7=8A=B6=E6=80=81=E5=8F=8D?= =?UTF-8?q?=E9=A6=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 状态,区分加载失败与记录不存在 --- apps/web/src/api/client.ts | 34 ++++++++++++++++-- apps/web/src/pages/assets/AssetLibrary.tsx | 1 + apps/web/src/pages/dashboard/Dashboard.tsx | 11 +++++- .../pages/duplication/DuplicationDetail.tsx | 14 +++++++- .../pages/duplication/DuplicationResults.tsx | 2 ++ apps/web/src/pages/generate/GeneratePage.tsx | 35 ++++++++++++++++--- .../web/src/pages/products/ProductLibrary.tsx | 1 + .../src/pages/templates/TemplateLibrary.tsx | 4 ++- apps/web/src/pages/titles/TitleLibrary.tsx | 1 + apps/web/src/pages/voices/VoiceLibrary.tsx | 1 + 10 files changed, 94 insertions(+), 10 deletions(-) diff --git a/apps/web/src/api/client.ts b/apps/web/src/api/client.ts index 771b76256..042e54a64 100644 --- a/apps/web/src/api/client.ts +++ b/apps/web/src/api/client.ts @@ -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); } ); diff --git a/apps/web/src/pages/assets/AssetLibrary.tsx b/apps/web/src/pages/assets/AssetLibrary.tsx index 16fee17f3..dd53be6ad 100644 --- a/apps/web/src/pages/assets/AssetLibrary.tsx +++ b/apps/web/src/pages/assets/AssetLibrary.tsx @@ -120,6 +120,7 @@ const AssetLibrary: React.FC = () => { queryClient.invalidateQueries({ queryKey: ['assets', activeLibrary] }); queryClient.invalidateQueries({ queryKey: ['asset-libraries'] }); }, + onError: (err: any) => { if (!err?.__msgShown) message.error('删除失败') }, }); /** 处理上传 */ diff --git a/apps/web/src/pages/dashboard/Dashboard.tsx b/apps/web/src/pages/dashboard/Dashboard.tsx index 6d2b674ae..891ea5fc5 100644 --- a/apps/web/src/pages/dashboard/Dashboard.tsx +++ b/apps/web/src/pages/dashboard/Dashboard.tsx @@ -16,6 +16,7 @@ import { Button, Progress, Space, + Alert, } from 'antd'; import { FileOutlined, @@ -60,7 +61,7 @@ const StatusTag: React.FC<{ status: string }> = ({ status }) => { const Dashboard: React.FC = () => { const navigate = useNavigate(); - const { data, isLoading } = useQuery({ + const { data, isLoading, isError } = useQuery({ queryKey: ['dashboard-overview'], queryFn: getDashboardOverview, }); @@ -111,6 +112,14 @@ const Dashboard: React.FC = () => { ); } + if (isError) { + return ( +
+ +
+ ); + } + return (
diff --git a/apps/web/src/pages/duplication/DuplicationDetail.tsx b/apps/web/src/pages/duplication/DuplicationDetail.tsx index 888ee7e9e..5bf90291b 100644 --- a/apps/web/src/pages/duplication/DuplicationDetail.tsx +++ b/apps/web/src/pages/duplication/DuplicationDetail.tsx @@ -228,7 +228,7 @@ const DuplicationDetail: React.FC = () => { const { id } = useParams<{ id: string }>(); const navigate = useNavigate(); - const { data: detail, isLoading } = useQuery({ + const { data: detail, isLoading, isError } = useQuery({ queryKey: ['duplication-detail', id], queryFn: () => getDuplicationDetail(id!), enabled: !!id, @@ -242,6 +242,18 @@ const DuplicationDetail: React.FC = () => { ); } + if (isError) { + return ( + <div style={{ padding: 24 }}> + <Empty description="加载查重记录失败"> + <Button onClick={() => navigate('/duplication/results')}> + 返回列表 + </Button> + </Empty> + </div> + ); + } + if (!detail) { return ( <div style={{ padding: 24 }}> diff --git a/apps/web/src/pages/duplication/DuplicationResults.tsx b/apps/web/src/pages/duplication/DuplicationResults.tsx index d0aac3af0..0137182df 100644 --- a/apps/web/src/pages/duplication/DuplicationResults.tsx +++ b/apps/web/src/pages/duplication/DuplicationResults.tsx @@ -105,6 +105,7 @@ const DuplicationResults: React.FC = () => { message.success('已删除'); queryClient.invalidateQueries({ queryKey: ['duplication-records'] }); }, + onError: (err: any) => { if (!err?.__msgShown) message.error('删除失败') }, }); // 重新查重 @@ -114,6 +115,7 @@ const DuplicationResults: React.FC = () => { message.success('已重新提交查重'); queryClient.invalidateQueries({ queryKey: ['duplication-records'] }); }, + onError: (err: any) => { if (!err?.__msgShown) message.error('重新查重失败') }, }); /** 批量删除 */ diff --git a/apps/web/src/pages/generate/GeneratePage.tsx b/apps/web/src/pages/generate/GeneratePage.tsx index beb92de75..0f6ed73f6 100644 --- a/apps/web/src/pages/generate/GeneratePage.tsx +++ b/apps/web/src/pages/generate/GeneratePage.tsx @@ -46,31 +46,31 @@ const GeneratePage: React.FC = () => { const [generated, setGenerated] = useState(false); // 获取模板列表 - const { data: templates = [] } = useQuery({ + const { data: templates = [], isLoading: tplLoading, isError: tplError } = useQuery({ queryKey: ['templates'], queryFn: getTemplates, }); // 获取素材库和素材 - const { data: libraries = [] } = useQuery({ + const { data: libraries = [], isLoading: libLoading, isError: libError } = useQuery({ queryKey: ['asset-libraries'], queryFn: getAssetLibraries, }); // 获取标题 - const { data: titles = [] } = useQuery({ + const { data: titles = [], isLoading: titleLoading, isError: titleError } = useQuery({ queryKey: ['titles'], queryFn: getTitles, }); // 获取配音 - const { data: voices = [] } = useQuery({ + const { data: voices = [], isLoading: voiceLoading, isError: voiceError } = useQuery({ queryKey: ['voices'], queryFn: getVoices, }); // 获取所有素材(跨库) - const { data: allAssets = [] } = useQuery({ + const { data: allAssets = [], isLoading: assetsLoading, isError: assetsError } = useQuery({ queryKey: ['all-assets'], queryFn: async () => { const all: AssetItem[] = []; @@ -83,6 +83,9 @@ const GeneratePage: React.FC = () => { enabled: libraries.length > 0, }); + const pageLoading = tplLoading || libLoading || titleLoading || voiceLoading || assetsLoading; + const pageError = tplError || libError || titleError || voiceError || assetsError; + // 创建生成任务 const generateMutation = useMutation({ mutationFn: createGenerationTask, @@ -275,6 +278,28 @@ const GeneratePage: React.FC = () => { }, ]; + if (pageLoading) { + return ( + <div style={{ textAlign: 'center', padding: 80 }}> + <Spin size="large" /> + </div> + ); + } + + if (pageError) { + return ( + <div style={{ padding: '24px', maxWidth: 1200, margin: '0 auto' }}> + <Alert + type="error" + message="加载数据失败" + description="部分数据获取失败,请刷新页面重试。" + showIcon + action={<Button onClick={() => window.location.reload()}>刷新页面</Button>} + /> + </div> + ); + } + return ( <div style={{ padding: '24px', maxWidth: 1200, margin: '0 auto' }}> <Title level={3} style={{ marginBottom: 24 }}> diff --git a/apps/web/src/pages/products/ProductLibrary.tsx b/apps/web/src/pages/products/ProductLibrary.tsx index cc21f4075..fdc309134 100644 --- a/apps/web/src/pages/products/ProductLibrary.tsx +++ b/apps/web/src/pages/products/ProductLibrary.tsx @@ -64,6 +64,7 @@ const ProductLibrary: React.FC = () => { message.success('已删除'); queryClient.invalidateQueries({ queryKey: ['products'] }); }, + onError: (err: any) => { if (!err?.__msgShown) message.error('删除失败') }, }); // 下载 diff --git a/apps/web/src/pages/templates/TemplateLibrary.tsx b/apps/web/src/pages/templates/TemplateLibrary.tsx index cedae704b..fc4899cd5 100644 --- a/apps/web/src/pages/templates/TemplateLibrary.tsx +++ b/apps/web/src/pages/templates/TemplateLibrary.tsx @@ -50,9 +50,11 @@ const TemplateLibrary: React.FC = () => { // 收藏/取消收藏 const favMutation = useMutation({ mutationFn: toggleFavoriteTemplate, - onSuccess: () => { + onSuccess: (data) => { + message.success(data.is_favorite ? '已收藏' : '已取消收藏'); queryClient.invalidateQueries({ queryKey: ['templates'] }); }, + onError: (err: any) => { if (!err?.__msgShown) message.error('操作失败') }, }); /** 提取所有分类 */ diff --git a/apps/web/src/pages/titles/TitleLibrary.tsx b/apps/web/src/pages/titles/TitleLibrary.tsx index fcf982295..5d637639e 100644 --- a/apps/web/src/pages/titles/TitleLibrary.tsx +++ b/apps/web/src/pages/titles/TitleLibrary.tsx @@ -85,6 +85,7 @@ const TitleLibrary: React.FC = () => { message.success('已删除'); queryClient.invalidateQueries({ queryKey: ['titles'] }); }, + onError: (err: any) => { if (!err?.__msgShown) message.error('删除失败') }, }); // 批量导入 diff --git a/apps/web/src/pages/voices/VoiceLibrary.tsx b/apps/web/src/pages/voices/VoiceLibrary.tsx index ca72148b4..bcf31b37c 100644 --- a/apps/web/src/pages/voices/VoiceLibrary.tsx +++ b/apps/web/src/pages/voices/VoiceLibrary.tsx @@ -103,6 +103,7 @@ const VoiceLibrary: React.FC = () => { message.success('已删除'); queryClient.invalidateQueries({ queryKey: ['voices'] }); }, + onError: (err: any) => { if (!err?.__msgShown) message.error('删除失败') }, }); // AI 生成配音