增加调试浮层
This commit is contained in:
@@ -10,18 +10,43 @@ export function LoginPage() {
|
||||
const navigate = useNavigate();
|
||||
const { setTokenAndUser, fetchProfile } = useUser();
|
||||
const [error, setError] = useState("");
|
||||
const [debugInfo, setDebugInfo] = useState<string[]>([]);
|
||||
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 (
|
||||
<div className="min-h-dvh bg-morandi-50 flex items-center justify-center p-4">
|
||||
<div className="min-h-dvh bg-morandi-50 flex items-center justify-center p-4 relative">
|
||||
{/* 调试浮层:点击可关闭 */}
|
||||
{debugInfo.length > 0 && (
|
||||
<div
|
||||
className="absolute top-0 left-0 right-0 z-50 bg-black/80 text-green-400 text-xs p-3 overflow-auto max-h-[50%] cursor-pointer font-mono"
|
||||
style={{ whiteSpace: 'pre-wrap', wordBreak: 'break-all', lineHeight: 1.5 }}
|
||||
onClick={() => setDebugInfo([])}
|
||||
>
|
||||
{debugInfo.map((msg, i) => (
|
||||
<div key={i}>{msg}</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
<div className="text-center">
|
||||
<motion.div
|
||||
animate={{ rotate: 360 }}
|
||||
|
||||
Reference in New Issue
Block a user