From 5073090bcbdc9d630163276ca3a8c7a4b58d4e13 Mon Sep 17 00:00:00 2001 From: Wenyan Date: Wed, 26 Nov 2025 12:13:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20403=20=E6=97=A0?= =?UTF-8?q?=E6=9D=83=E9=99=90=E5=BC=B9=E7=AA=97=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 修改内容 在 axios 响应拦截器中添加 403 Forbidden 错误处理: - 检测到 403 状态码时,显示 toast 警告提示 - 提示信息:"无权限访问该资源" - 只在客户端环境显示,服务端不显示 toast ## 代码位置 - app/api/axios-client.ts:177-185 ## 实现 ```typescript // 🔒 403 无权限错误处理 if (isAxiosError(error) && error.response?.status === 403) { console.warn('⚠️ [403 Forbidden] 无权限访问:', error.config?.url); // 只在客户端显示 toast 提示 if (typeof window !== 'undefined') { toastService.warning('无权限访问该资源'); } } ``` ## 用户体验 - 用户访问无权限资源时,右上角显示黄色警告 toast - toast 自动消失,不阻塞用户操作 - 控制台同时输出警告日志便于调试 --- app/api/axios-client.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/app/api/axios-client.ts b/app/api/axios-client.ts index c28c391..8f1ebc4 100644 --- a/app/api/axios-client.ts +++ b/app/api/axios-client.ts @@ -1,6 +1,7 @@ import axios, { AxiosRequestConfig, AxiosResponse, isAxiosError } from 'axios'; import { mockData, type MockApiResponse } from './mock'; import { API_BASE_URL, DOCUMENT_URL } from '../config/api-config'; +import { toastService } from '../components/ui/Toast'; /** * API响应类型 @@ -173,6 +174,16 @@ axiosInstance.interceptors.response.use( } } + // 🔒 403 无权限错误处理 + if (isAxiosError(error) && error.response?.status === 403) { + console.warn('⚠️ [403 Forbidden] 无权限访问:', error.config?.url); + + // 只在客户端显示 toast 提示 + if (typeof window !== 'undefined') { + toastService.warning('无权限访问该资源'); + } + } + return Promise.reject(error); } );