From d7e9863f76e575c623f570cd2fdf81dac2a64b07 Mon Sep 17 00:00:00 2001 From: Karlcarlo Date: Thu, 25 Jun 2026 18:51:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=B0=83=E8=AF=95=E6=B5=AE?= =?UTF-8?q?=E5=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/app/pages/LoginPage.tsx | 53 +++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/frontend/src/app/pages/LoginPage.tsx b/frontend/src/app/pages/LoginPage.tsx index 6f748d4..2ced7cc 100644 --- a/frontend/src/app/pages/LoginPage.tsx +++ b/frontend/src/app/pages/LoginPage.tsx @@ -10,18 +10,43 @@ export function LoginPage() { const navigate = useNavigate(); const { setTokenAndUser, fetchProfile } = useUser(); const [error, setError] = useState(""); + const [debugInfo, setDebugInfo] = useState([]); + const addDebug = (msg: string) => { + console.log(msg); + setDebugInfo(prev => [...prev.slice(-20), msg]); + }; useEffect(() => { clearDebugLogs(); - // 身份一致性检查:URL 中的 openid 与缓存 token 不一致时清除缓存 + // 收集调试信息(直接在页面显示,排查缓存问题) + const infos: string[] = []; const urlOpenid = new URLSearchParams(window.location.search).get('openid'); const cachedToken = getToken(); + const ts = new Date().toISOString().slice(11, 19); + infos.push(`[${ts}] URL: ${window.location.href.substring(0, 100)}`); + infos.push(`[${ts}] urlOpenid=${urlOpenid || '(空)'}`); + + let cacheOpenid = ''; + if (cachedToken) { + try { + const p = decodeToken(cachedToken); + cacheOpenid = p.openid; + infos.push(`[${ts}] cache openid=${p.openid}, uid=${p.uid}, exp=${p.exp ? new Date(p.exp * 1000).toLocaleString() : '?'}`); + } catch (e) { + infos.push(`[${ts}] 缓存 token 解码失败: ${e}`); + } + } else { + infos.push(`[${ts}] 无缓存 token`); + } + addDebug(infos.join('\n')); + + // 身份一致性检查 if (urlOpenid && cachedToken) { try { const payload = decodeToken(cachedToken); if (payload.openid !== urlOpenid) { - debugLog(`[auth] openid 不一致: URL=${urlOpenid}, cache=${payload.openid},清除缓存`); + addDebug(`[auth] openid 不一致: URL=${urlOpenid}, cache=${payload.openid},清除缓存`); clearToken(); } } catch (e) { @@ -35,28 +60,28 @@ export function LoginPage() { const openidParam = params.get('openid'); if (openidParam && openidParam.trim()) { - debugLog(`openid: ${openidParam}`); + addDebug(`开始登录: openid=${openidParam}`); fetchApi("/auth/openid-login", { method: "POST", body: JSON.stringify({ openid: openidParam }), }).then((res: any) => { - debugLog(`openid-login OK: id=${res.user_id}, phone=${res.mobile}`); + addDebug(`openid-login OK: id=${res.user_id}, phone=${res.mobile}`); setTokenAndUser(res.token, { id: res.user_id, openid: res.openid, phone: res.mobile }); fetchProfile().then((dto) => { - if (dto) debugLog(`fetchProfile: name=${dto.name}, phone=${dto.phone}, onboarded=${dto.is_onboarded}`); + if (dto) addDebug(`profile: name=${dto.name}, phone=${dto.phone}, onboarded=${dto.is_onboarded}`); navigate(dto?.is_onboarded ? "/home" : "/onboarding", { replace: true }); }).catch((e: any) => { debugLog(`fetchProfile error: ${e?.message}`); setError("登录已失效,请在小程序重新进入"); }); }).catch((e: any) => { - debugLog(`openid-login error: ${e?.message}`); + addDebug(`openid-login error: ${e?.message}`); setError("登录失败: " + (e?.message || "")); }); return; } - debugLog(`no openid param (val=${openidParam || '(empty)'}), fallback to dev-login`); + addDebug(`no openid, fallback to dev-login`); devLogin().then((res) => { debugLog(`dev-login: openid=${res.openid}, phone=${res.mobile}`); setTokenAndUser(res.token, { id: res.user_id, openid: res.openid, phone: res.mobile }); @@ -71,7 +96,19 @@ export function LoginPage() { }, []); // eslint-disable-line react-hooks/exhaustive-deps return ( -
+
+ {/* 调试浮层:点击可关闭 */} + {debugInfo.length > 0 && ( +
setDebugInfo([])} + > + {debugInfo.map((msg, i) => ( +
{msg}
+ ))} +
+ )}