import { useCallback, useEffect, useState } from "react"; import { useNavigate } from "react-router-dom"; import { post, setToken } from "../api/http"; import { ctxMissing, createCaptcha, loginWithPassword, verifyChallenge, type Captcha, type LoginScope, } from "../api/platform"; import { firstRoute } from "../auth/nav"; import { useSession } from "../auth/session"; import { Btn, Field } from "../components/ui"; /** 本机演示账号:`end_user` 档=IAM 项目空间业务账号(temp/uds_p5_probe.py 建号, * 口令见该台账);`platform` 档=平台身份服务账号,本机仅 root。 * ⚠️ 两档账号不互通:拿 `end_user` 账号走 `platform` 档登录会 12002。 */ const DEMO: Record = { end_user: [ { user: "klt_bos_admin", label: "总运营" }, { user: "klt_bos_ops_main", label: "运维团队主账号" }, { user: "klt_bos_ops_sub", label: "运维子账号" }, { user: "klt_bos_team_lead", label: "运营团队主账号" }, { user: "klt_bos_ops", label: "运营子账号" }, { user: "klt_bos_ent", label: "客户主账号" }, { user: "klt_bos_sub", label: "客户下属分账号" }, { user: "klt_bos_partner", label: "合伙人" }, { user: "klt_bos_operator", label: "运营商(只读)" }, ], platform: [{ user: "root", label: "平台账号(对照用)" }], }; const SCOPE_TABS: { key: LoginScope; label: string }[] = [ { key: "end_user", label: "项目空间账号" }, { key: "platform", label: "平台账号" }, ]; export function LoginPage() { const nav = useNavigate(); const { ctx, reload, error: ctxError, signOut } = useSession(); const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [code, setCode] = useState(""); const [captcha, setCaptcha] = useState(null); const [busy, setBusy] = useState(false); const [err, setErr] = useState(null); const [step, setStep] = useState<"login" | "pwd">("login"); const [scope, setScope] = useState("end_user"); const [np1, setNp1] = useState(""); const [np2, setNp2] = useState(""); const refreshCaptcha = useCallback(async () => { try { setCaptcha(await createCaptcha()); setCode(""); } catch (e) { setErr(`验证码获取失败:${(e as Error).message}`); setCaptcha(null); } }, []); useEffect(() => { void refreshCaptcha(); }, [refreshCaptcha]); /** 拿到业务上下文后分流:强制改密优先于进入工作台(总则§16.5:未改密不得用其它功能)。 */ useEffect(() => { if (!ctx) return; if (ctx.must_change_password) setStep("pwd"); else nav(`/${firstRoute(ctx.role_code)}`, { replace: true }); }, [ctx, nav]); async function onSubmit(e: React.FormEvent) { e.preventDefault(); if (!captcha) { setErr("验证码尚未就绪,请重试"); return; } if (ctxMissing(scope)) { setErr("缺少项目空间标识(tenant_id/project_id):本机联调需 temp/seoh_provision.json 在位,或用 VITE_SEOH_* 指定"); return; } setBusy(true); setErr(null); try { const challengeToken = await verifyChallenge({ username, captchaId: captcha.captcha_id, code, scope, }); const r = await loginWithPassword({ username, password, challengeToken, scope }); setToken(r.access_token); await reload(); } catch (e2) { setErr((e2 as Error).message || "登录失败"); void refreshCaptcha(); setBusy(false); return; } setBusy(false); } async function onChangePassword(e: React.FormEvent) { e.preventDefault(); if (np1 !== np2) { setErr("两次输入的新密码不一致"); return; } setBusy(true); setErr(null); try { await post("/account/change_password@v1", { old_password: password, new_password: np1 }); await reload(); if (step === "pwd") nav(`/${firstRoute(ctx?.role_code)}`, { replace: true }); } catch (e2) { setErr((e2 as Error).message || "修改密码失败"); } setBusy(false); } const img = captcha?.image_base64 ?? captcha?.image ?? ""; return (
{step === "login" ? (

账号登录

{SCOPE_TABS.map((t) => ( ))}
setUsername(e.target.value)} autoComplete="username" placeholder={scope === "end_user" ? "项目空间账号登录名" : "平台登录名"} /> setPassword(e.target.value)} autoComplete="current-password" />
setCode(e.target.value)} placeholder="图中字符" autoComplete="off" /> {img ? ( 验证码 void refreshCaptcha()} /> ) : ( void refreshCaptcha()}> 获取 )}
{err ?
{err}
: null} {/* 票据已拿到但业务上下文取不到:必须显式呈现,⛔ 停在登录页静默失败 (2026-11-08 手测「点登录没反应」=上下文失败被吞掉,页面既不进系统也不报错)。 */} {!err && ctxError ? (
登录已通过,但业务上下文获取失败:{ctxError}
令牌已保存在本会话,可「重新获取」再试;仍失败请清除登录态后换账号。
void reload()}> 重新获取 清除登录态
) : null}
{busy ? "登录中…" : "登录"} void refreshCaptcha()}> 换一张验证码
) : (

设置新密码

首次登录或密码被重置,须先设置新密码才能继续使用其它功能。

setNp1(e.target.value)} autoComplete="new-password" /> setNp2(e.target.value)} autoComplete="new-password" />
{err ?
{err}
: null}
{busy ? "提交中…" : "保存并进入"}
)}
{step === "login" ? (

本机演示账号

点击卡片仅填入登录名,密码需手输(口令见 temp/uds_p5_probe.py 建号台账)。两档账号不互通: 项目空间账号走平台档登录会报 12002。

{DEMO[scope].map((d) => ( ))}
) : null}
); }