"use client"; import { useEffect, useState } from "react"; import { useSearchParams } from "next/navigation"; import { AppLayout } from "@/components/layout/app-layout"; import { QuoteRouteHub } from "@/components/embed/quote-route-hub"; import { EmbedDemoLogin } from "@/components/embed/embed-demo-login"; import { Card } from "@/components/ui/card"; import { SecondaryButton } from "@/components/ui/primary-button"; import { Skeleton } from "@/components/ui/skeleton"; import { ErrorBanner } from "@/components/ui/error-banner"; import { isEmbedHostedShell, parseEmbedSsoParams, stripEmbedSsoQueryFromUrl, } from "@/lib/embed/sso-params"; import { embedDemoLogin, embedDemoLoginWithKey, embedDemoLogout, embedDemoMe, type EmbedDemoSession, } from "@/lib/frontend/api-client"; export function EmbedDemoClient() { const searchParams = useSearchParams(); const [booting, setBooting] = useState(true); const [session, setSession] = useState(null); const [bootError, setBootError] = useState(null); const [hostedShell, setHostedShell] = useState(false); useEffect(() => { let cancelled = false; void (async () => { const params = new URLSearchParams(searchParams.toString()); const sso = parseEmbedSsoParams(params); const hosted = isEmbedHostedShell(params); if (!cancelled) setHostedShell(hosted); // 宿主直传凭证:自动登录,跳过登录页(勿先 logout) if (sso.mode === "api_key") { const res = await embedDemoLoginWithKey("", sso.apiKey); if (cancelled) return; if (res.code === 0) { stripEmbedSsoQueryFromUrl(); setSession(res.data); setBootError(null); setBooting(false); return; } setBootError(res.message || "API Key 无效,无法自动登录"); setBooting(false); return; } if (sso.mode === "password") { const res = await embedDemoLogin("", sso.customerId, sso.password); if (cancelled) return; if (res.code === 0) { stripEmbedSsoQueryFromUrl(); setSession(res.data); setBootError(null); setBooting(false); return; } setBootError(res.message || "客户编号或密码错误,无法自动登录"); setBooting(false); return; } // 无 SSO:尝试恢复已有 cookie 会话(刷新不掉登录) const me = await embedDemoMe(""); if (cancelled) return; if (me.code === 0) { setSession(me.data); setBootError(null); setBooting(false); return; } setSession(null); setBooting(false); })(); return () => { cancelled = true; }; }, [searchParams]); const isKeyLogin = session?.login_type === "api_key"; const handleLogout = async () => { await embedDemoLogout(""); setSession(null); setBootError(null); setHostedShell(false); }; if (booting) { return ( ); } if (!session) { return ( {bootError ? (
{bootError}
) : null} { setSession(data); setBootError(null); }} />
); } return (

当前登录客户 · {isKeyLogin ? "API Key 登录" : "账号密码登录"} {hostedShell ? " · 宿主直连" : ""}

{session.customer_id}

请选择 MotherShip 或 Flock Freight 模块开始查价

{!hostedShell ? ( void handleLogout()}> 退出登录 ) : null}
); }