From 325c2e3a870a9fa10e3a91e2faaeeb7dc982d94a Mon Sep 17 00:00:00 2001 From: tone <3341154833@qq.com> Date: Thu, 19 Jun 2025 15:24:44 +0800 Subject: [PATCH] =?UTF-8?q?userMe=E5=A4=84=E7=90=86=E6=97=A0token=E6=83=85?= =?UTF-8?q?=E5=86=B5=EF=BC=8C=E8=8A=82=E7=9C=81=E8=B5=84=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tone-page-web/hooks/user/use-user-me.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tone-page-web/hooks/user/use-user-me.ts b/tone-page-web/hooks/user/use-user-me.ts index 4969fa6..b622e0a 100644 --- a/tone-page-web/hooks/user/use-user-me.ts +++ b/tone-page-web/hooks/user/use-user-me.ts @@ -6,7 +6,12 @@ export function useUserMe({ onError }: { onError?: (e: any) => void } = {}) { const { data: user, isLoading, error } = useSWR( '/api/user/me', - async () => UserApi.me(), + async () => { + if (isClientSide && !localStorage.getItem('token')) { + throw Object.assign(new Error('未登录'), { statusCode: -1 }); + } + return await UserApi.me(); + }, { onError: (error) => { if (error.statusCode === 401) { @@ -20,7 +25,7 @@ export function useUserMe({ onError }: { onError?: (e: any) => void } = {}) { revalidateIfStale: false, revalidateOnFocus: false, shouldRetryOnError: (err) => { - if (err.statusCode === 401) { + if ([-1, 401].includes(err.statusCode)) { return false; } return true;