From 85f68a54621dcd597f81011edf95ea5b7be04a5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B5=B7=E9=BE=99?= Date: Sun, 31 Aug 2025 03:47:45 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A6=BB=E8=B0=B1=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E8=A7=A3=E5=86=B3=E6=94=BE=E8=82=86=EF=BC=8C=E4=B8=8A=E7=BA=A7?= =?UTF-8?q?=E8=A6=81=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/request.ts | 1 + components/auth/auth-guard.tsx | 14 ++++++++++---- lib/auth.ts | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/api/request.ts b/api/request.ts index fc16b46..7a59dbf 100644 --- a/api/request.ts +++ b/api/request.ts @@ -50,6 +50,7 @@ request.interceptors.response.use( (response: AxiosResponse) => { // 检查业务状态码 if (response.data?.code !== 0 && response.data?.code !== 202) { + console.log('response', response) // 处理业务层面的错误 const businessCode = response.data?.code; const errorMessage = response.data?.message; diff --git a/components/auth/auth-guard.tsx b/components/auth/auth-guard.tsx index cc8c573..3ced090 100644 --- a/components/auth/auth-guard.tsx +++ b/components/auth/auth-guard.tsx @@ -2,8 +2,10 @@ import { useEffect, useState } from 'react'; import { useRouter, usePathname } from 'next/navigation'; -import { checkAuth, getUserProfile, isAuthenticated } from '@/lib/auth'; +import { checkAuth, clearAuthData, getUserProfile, isAuthenticated } from '@/lib/auth'; import GlobalLoad from '../common/GlobalLoad'; +import { message } from 'antd'; +import { errorHandle } from '@/api/errorHandle'; interface AuthGuardProps { children: React.ReactNode; @@ -42,9 +44,13 @@ export default function AuthGuard({ children }: AuthGuardProps) { } else { router.push('/login'); } - } catch (error) { - console.error('Auth verification failed:', error); - router.push('/login'); + } catch (errorCode:any) { + // 强制的领导要求,401和502都跳转到登录页 其他的不管 + if(errorCode.message == 401||errorCode.message == 502){ + router.push('/login'); + clearAuthData(); + } + errorHandle(errorCode.message) } finally { setIsLoading(false); } diff --git a/lib/auth.ts b/lib/auth.ts index f86bb5d..3faeef9 100644 --- a/lib/auth.ts +++ b/lib/auth.ts @@ -277,7 +277,7 @@ export const getUserProfile = async (): Promise => { }); if (!response.ok) { - throw new Error(`HTTP error! status: ${response.status}`); + throw new Error(response.status.toString()); } const data = await response.json();