| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- 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<LoginScope, { user: string; label: string }[]> = {
- 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<Captcha | null>(null);
- const [busy, setBusy] = useState(false);
- const [err, setErr] = useState<string | null>(null);
- const [step, setStep] = useState<"login" | "pwd">("login");
- const [scope, setScope] = useState<LoginScope>("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 (
- <div className="login">
- <aside className="login-side">
- <h1>运营支撑系统</h1>
- <p>
- 恪澜通宽带业务的运营支撑后台:组织与区域、运营商/合伙人、运营与运维团队、客户与分账号、批量导入与审计追溯。
- </p>
- <p className="note">
- 登录经 SEOH 平台统一身份服务下发令牌,本系统再据令牌换取业务角色上下文;身份、角色与可见范围全部由后端裁决,
- 前端⛔ 自行判权,界面上能看到⛔ 等于能操作。
- </p>
- </aside>
- <div className="login-main">
- <div className="login-form">
- <section className="panel">
- {step === "login" ? (
- <form onSubmit={onSubmit}>
- <h2>账号登录</h2>
- <div className="tabs" role="tablist" aria-label="登录档位">
- {SCOPE_TABS.map((t) => (
- <button
- key={t.key}
- type="button"
- role="tab"
- aria-selected={scope === t.key}
- className={`tab${scope === t.key ? " active" : ""}`}
- onClick={() => {
- setScope(t.key);
- setUsername("");
- setErr(null);
- }}
- >
- {t.label}
- </button>
- ))}
- </div>
- <Field label="登录名" required>
- <input
- value={username}
- onChange={(e) => setUsername(e.target.value)}
- autoComplete="username"
- placeholder={scope === "end_user" ? "项目空间账号登录名" : "平台登录名"}
- />
- </Field>
- <Field label="密码" required>
- <input
- type="password"
- value={password}
- onChange={(e) => setPassword(e.target.value)}
- autoComplete="current-password"
- />
- </Field>
- <Field label="图形验证码" required hint="点击图片可换一张;连错 5 次锁定 1 小时">
- <div className="captcha-row">
- <input
- value={code}
- onChange={(e) => setCode(e.target.value)}
- placeholder="图中字符"
- autoComplete="off"
- />
- {img ? (
- <img
- src={img.startsWith("data:") ? img : `data:image/png;base64,${img}`}
- alt="验证码"
- title="点击刷新"
- onClick={() => void refreshCaptcha()}
- />
- ) : (
- <Btn ghost onClick={() => void refreshCaptcha()}>
- 获取
- </Btn>
- )}
- </div>
- </Field>
- {err ? <div className="alert">{err}</div> : null}
- {/* 票据已拿到但业务上下文取不到:必须显式呈现,⛔ 停在登录页静默失败
- (2026-11-08 手测「点登录没反应」=上下文失败被吞掉,页面既不进系统也不报错)。 */}
- {!err && ctxError ? (
- <div className="alert">
- 登录已通过,但业务上下文获取失败:{ctxError}
- <br />
- <span className="note">令牌已保存在本会话,可「重新获取」再试;仍失败请清除登录态后换账号。</span>
- <div className="form-actions">
- <Btn ghost onClick={() => void reload()}>
- 重新获取
- </Btn>
- <Btn ghost onClick={signOut}>
- 清除登录态
- </Btn>
- </div>
- </div>
- ) : null}
- <div className="form-actions">
- <Btn type="submit" disabled={busy || !username || !password || !code}>
- {busy ? "登录中…" : "登录"}
- </Btn>
- <Btn ghost onClick={() => void refreshCaptcha()}>
- 换一张验证码
- </Btn>
- </div>
- </form>
- ) : (
- <form onSubmit={onChangePassword}>
- <h2>设置新密码</h2>
- <p className="note">首次登录或密码被重置,须先设置新密码才能继续使用其它功能。</p>
- <div className="form-grid">
- <Field label="新密码" required>
- <input type="password" value={np1} onChange={(e) => setNp1(e.target.value)} autoComplete="new-password" />
- </Field>
- <Field label="确认新密码" required error={np2 && np1 !== np2 ? "两次输入不一致" : undefined}>
- <input type="password" value={np2} onChange={(e) => setNp2(e.target.value)} autoComplete="new-password" />
- </Field>
- </div>
- {err ? <div className="alert">{err}</div> : null}
- <div className="form-actions">
- <Btn type="submit" disabled={busy || !np1 || !np2}>
- {busy ? "提交中…" : "保存并进入"}
- </Btn>
- </div>
- </form>
- )}
- </section>
- {step === "login" ? (
- <section className="panel">
- <h2>本机演示账号</h2>
- <p className="note">
- 点击卡片仅填入登录名,密码需手输(口令见 <code>temp/uds_p5_probe.py</code> 建号台账)。两档账号不互通:
- 项目空间账号走平台档登录会报 12002。
- </p>
- <div className="cards">
- {DEMO[scope].map((d) => (
- <button
- key={d.user}
- type="button"
- className="persona"
- onClick={() => {
- setUsername(d.user);
- setErr(null);
- }}
- >
- <strong>{d.label}</strong>
- <span>{d.user}</span>
- </button>
- ))}
- </div>
- </section>
- ) : null}
- </div>
- </div>
- </div>
- );
- }
|