From 970f10a274ef98b39c68e15077dc982e1c12e63f Mon Sep 17 00:00:00 2001 From: hhs <386998068@qq.com> Date: Sat, 20 Jun 2026 15:07:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=89=8D=E7=AB=AF=20l?= =?UTF-8?q?int=20=E9=94=99=E8=AF=AF=20=E2=80=94=20=E7=A7=BB=E9=99=A4=20ref?= =?UTF-8?q?=20=E6=A8=A1=E5=BC=8F=EF=BC=8C=E7=94=A8=20useEffect=20=E4=BE=9D?= =?UTF-8?q?=E8=B5=96=E6=B3=A8=E5=85=A5=E5=9B=9E=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - api.ts: 移除 catch 中未使用的 err 变量 - auth.tsx: 去掉 useRef 模式,直接在 useEffect 中注册回调并声明依赖 --- frontend/src/lib/api.ts | 2 +- frontend/src/lib/auth.tsx | 12 +++--------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts index 21e6918..960b076 100644 --- a/frontend/src/lib/api.ts +++ b/frontend/src/lib/api.ts @@ -116,7 +116,7 @@ async function request( } return { data: body as T, status }; - } catch (err) { + } catch { return { error: { code: "NETWORK_ERROR", message: "网络连接失败,请检查网络" }, status: 0, diff --git a/frontend/src/lib/auth.tsx b/frontend/src/lib/auth.tsx index 6c8f51c..733175c 100644 --- a/frontend/src/lib/auth.tsx +++ b/frontend/src/lib/auth.tsx @@ -109,20 +109,14 @@ export function AuthProvider({ children }: { children: ReactNode }) { [clearRefreshTimer, persistAuth] ); - // 用 ref 保存最新回调,供 API 层 401 拦截器使用(避免闭包陈旧) - const persistAuthRef = useRef(persistAuth); - const scheduleRefreshRef = useRef(scheduleRefresh); - persistAuthRef.current = persistAuth; - scheduleRefreshRef.current = scheduleRefresh; - // 注册 API 层认证回调(用于 401 拦截器) useEffect(() => { setAuthCallbacks({ getAccessToken: () => loadAccessToken(), getRefreshToken: () => loadRefreshToken(), onRefreshSuccess: (u, at, rt) => { - persistAuthRef.current(u, at, rt); - scheduleRefreshRef.current(at); + persistAuth(u, at, rt); + scheduleRefresh(at); }, onRefreshFailed: () => { clearAuth(); @@ -130,7 +124,7 @@ export function AuthProvider({ children }: { children: ReactNode }) { setAccessToken(null); }, }); - }, []); // eslint-disable-line react-hooks/exhaustive-deps + }, [persistAuth, scheduleRefresh]); // 初始化:检查已有 token 并尝试刷新 useEffect(() => {