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();