LoginPage.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. import { useCallback, useEffect, useState } from "react";
  2. import { useNavigate } from "react-router-dom";
  3. import { post, setToken } from "../api/http";
  4. import {
  5. ctxMissing,
  6. createCaptcha,
  7. loginWithPassword,
  8. verifyChallenge,
  9. type Captcha,
  10. type LoginScope,
  11. } from "../api/platform";
  12. import { firstRoute } from "../auth/nav";
  13. import { useSession } from "../auth/session";
  14. import { Btn, Field } from "../components/ui";
  15. /** 本机演示账号:`end_user` 档=IAM 项目空间业务账号(temp/uds_p5_probe.py 建号,
  16. * 口令见该台账);`platform` 档=平台身份服务账号,本机仅 root。
  17. * ⚠️ 两档账号不互通:拿 `end_user` 账号走 `platform` 档登录会 12002。 */
  18. const DEMO: Record<LoginScope, { user: string; label: string }[]> = {
  19. end_user: [
  20. { user: "klt_bos_admin", label: "总运营" },
  21. { user: "klt_bos_ops_main", label: "运维团队主账号" },
  22. { user: "klt_bos_ops_sub", label: "运维子账号" },
  23. { user: "klt_bos_team_lead", label: "运营团队主账号" },
  24. { user: "klt_bos_ops", label: "运营子账号" },
  25. { user: "klt_bos_ent", label: "客户主账号" },
  26. { user: "klt_bos_sub", label: "客户下属分账号" },
  27. { user: "klt_bos_partner", label: "合伙人" },
  28. { user: "klt_bos_operator", label: "运营商(只读)" },
  29. ],
  30. platform: [{ user: "root", label: "平台账号(对照用)" }],
  31. };
  32. const SCOPE_TABS: { key: LoginScope; label: string }[] = [
  33. { key: "end_user", label: "项目空间账号" },
  34. { key: "platform", label: "平台账号" },
  35. ];
  36. export function LoginPage() {
  37. const nav = useNavigate();
  38. const { ctx, reload, error: ctxError, signOut } = useSession();
  39. const [username, setUsername] = useState("");
  40. const [password, setPassword] = useState("");
  41. const [code, setCode] = useState("");
  42. const [captcha, setCaptcha] = useState<Captcha | null>(null);
  43. const [busy, setBusy] = useState(false);
  44. const [err, setErr] = useState<string | null>(null);
  45. const [step, setStep] = useState<"login" | "pwd">("login");
  46. const [scope, setScope] = useState<LoginScope>("end_user");
  47. const [np1, setNp1] = useState("");
  48. const [np2, setNp2] = useState("");
  49. const refreshCaptcha = useCallback(async () => {
  50. try {
  51. setCaptcha(await createCaptcha());
  52. setCode("");
  53. } catch (e) {
  54. setErr(`验证码获取失败:${(e as Error).message}`);
  55. setCaptcha(null);
  56. }
  57. }, []);
  58. useEffect(() => {
  59. void refreshCaptcha();
  60. }, [refreshCaptcha]);
  61. /** 拿到业务上下文后分流:强制改密优先于进入工作台(总则§16.5:未改密不得用其它功能)。 */
  62. useEffect(() => {
  63. if (!ctx) return;
  64. if (ctx.must_change_password) setStep("pwd");
  65. else nav(`/${firstRoute(ctx.role_code)}`, { replace: true });
  66. }, [ctx, nav]);
  67. async function onSubmit(e: React.FormEvent) {
  68. e.preventDefault();
  69. if (!captcha) {
  70. setErr("验证码尚未就绪,请重试");
  71. return;
  72. }
  73. if (ctxMissing(scope)) {
  74. setErr("缺少项目空间标识(tenant_id/project_id):本机联调需 temp/seoh_provision.json 在位,或用 VITE_SEOH_* 指定");
  75. return;
  76. }
  77. setBusy(true);
  78. setErr(null);
  79. try {
  80. const challengeToken = await verifyChallenge({
  81. username,
  82. captchaId: captcha.captcha_id,
  83. code,
  84. scope,
  85. });
  86. const r = await loginWithPassword({ username, password, challengeToken, scope });
  87. setToken(r.access_token);
  88. await reload();
  89. } catch (e2) {
  90. setErr((e2 as Error).message || "登录失败");
  91. void refreshCaptcha();
  92. setBusy(false);
  93. return;
  94. }
  95. setBusy(false);
  96. }
  97. async function onChangePassword(e: React.FormEvent) {
  98. e.preventDefault();
  99. if (np1 !== np2) {
  100. setErr("两次输入的新密码不一致");
  101. return;
  102. }
  103. setBusy(true);
  104. setErr(null);
  105. try {
  106. await post("/account/change_password@v1", { old_password: password, new_password: np1 });
  107. await reload();
  108. if (step === "pwd") nav(`/${firstRoute(ctx?.role_code)}`, { replace: true });
  109. } catch (e2) {
  110. setErr((e2 as Error).message || "修改密码失败");
  111. }
  112. setBusy(false);
  113. }
  114. const img = captcha?.image_base64 ?? captcha?.image ?? "";
  115. return (
  116. <div className="login">
  117. <aside className="login-side">
  118. <h1>运营支撑系统</h1>
  119. <p>
  120. 恪澜通宽带业务的运营支撑后台:组织与区域、运营商/合伙人、运营与运维团队、客户与分账号、批量导入与审计追溯。
  121. </p>
  122. <p className="note">
  123. 登录经 SEOH 平台统一身份服务下发令牌,本系统再据令牌换取业务角色上下文;身份、角色与可见范围全部由后端裁决,
  124. 前端⛔ 自行判权,界面上能看到⛔ 等于能操作。
  125. </p>
  126. </aside>
  127. <div className="login-main">
  128. <div className="login-form">
  129. <section className="panel">
  130. {step === "login" ? (
  131. <form onSubmit={onSubmit}>
  132. <h2>账号登录</h2>
  133. <div className="tabs" role="tablist" aria-label="登录档位">
  134. {SCOPE_TABS.map((t) => (
  135. <button
  136. key={t.key}
  137. type="button"
  138. role="tab"
  139. aria-selected={scope === t.key}
  140. className={`tab${scope === t.key ? " active" : ""}`}
  141. onClick={() => {
  142. setScope(t.key);
  143. setUsername("");
  144. setErr(null);
  145. }}
  146. >
  147. {t.label}
  148. </button>
  149. ))}
  150. </div>
  151. <Field label="登录名" required>
  152. <input
  153. value={username}
  154. onChange={(e) => setUsername(e.target.value)}
  155. autoComplete="username"
  156. placeholder={scope === "end_user" ? "项目空间账号登录名" : "平台登录名"}
  157. />
  158. </Field>
  159. <Field label="密码" required>
  160. <input
  161. type="password"
  162. value={password}
  163. onChange={(e) => setPassword(e.target.value)}
  164. autoComplete="current-password"
  165. />
  166. </Field>
  167. <Field label="图形验证码" required hint="点击图片可换一张;连错 5 次锁定 1 小时">
  168. <div className="captcha-row">
  169. <input
  170. value={code}
  171. onChange={(e) => setCode(e.target.value)}
  172. placeholder="图中字符"
  173. autoComplete="off"
  174. />
  175. {img ? (
  176. <img
  177. src={img.startsWith("data:") ? img : `data:image/png;base64,${img}`}
  178. alt="验证码"
  179. title="点击刷新"
  180. onClick={() => void refreshCaptcha()}
  181. />
  182. ) : (
  183. <Btn ghost onClick={() => void refreshCaptcha()}>
  184. 获取
  185. </Btn>
  186. )}
  187. </div>
  188. </Field>
  189. {err ? <div className="alert">{err}</div> : null}
  190. {/* 票据已拿到但业务上下文取不到:必须显式呈现,⛔ 停在登录页静默失败
  191. (2026-11-08 手测「点登录没反应」=上下文失败被吞掉,页面既不进系统也不报错)。 */}
  192. {!err && ctxError ? (
  193. <div className="alert">
  194. 登录已通过,但业务上下文获取失败:{ctxError}
  195. <br />
  196. <span className="note">令牌已保存在本会话,可「重新获取」再试;仍失败请清除登录态后换账号。</span>
  197. <div className="form-actions">
  198. <Btn ghost onClick={() => void reload()}>
  199. 重新获取
  200. </Btn>
  201. <Btn ghost onClick={signOut}>
  202. 清除登录态
  203. </Btn>
  204. </div>
  205. </div>
  206. ) : null}
  207. <div className="form-actions">
  208. <Btn type="submit" disabled={busy || !username || !password || !code}>
  209. {busy ? "登录中…" : "登录"}
  210. </Btn>
  211. <Btn ghost onClick={() => void refreshCaptcha()}>
  212. 换一张验证码
  213. </Btn>
  214. </div>
  215. </form>
  216. ) : (
  217. <form onSubmit={onChangePassword}>
  218. <h2>设置新密码</h2>
  219. <p className="note">首次登录或密码被重置,须先设置新密码才能继续使用其它功能。</p>
  220. <div className="form-grid">
  221. <Field label="新密码" required>
  222. <input type="password" value={np1} onChange={(e) => setNp1(e.target.value)} autoComplete="new-password" />
  223. </Field>
  224. <Field label="确认新密码" required error={np2 && np1 !== np2 ? "两次输入不一致" : undefined}>
  225. <input type="password" value={np2} onChange={(e) => setNp2(e.target.value)} autoComplete="new-password" />
  226. </Field>
  227. </div>
  228. {err ? <div className="alert">{err}</div> : null}
  229. <div className="form-actions">
  230. <Btn type="submit" disabled={busy || !np1 || !np2}>
  231. {busy ? "提交中…" : "保存并进入"}
  232. </Btn>
  233. </div>
  234. </form>
  235. )}
  236. </section>
  237. {step === "login" ? (
  238. <section className="panel">
  239. <h2>本机演示账号</h2>
  240. <p className="note">
  241. 点击卡片仅填入登录名,密码需手输(口令见 <code>temp/uds_p5_probe.py</code> 建号台账)。两档账号不互通:
  242. 项目空间账号走平台档登录会报 12002。
  243. </p>
  244. <div className="cards">
  245. {DEMO[scope].map((d) => (
  246. <button
  247. key={d.user}
  248. type="button"
  249. className="persona"
  250. onClick={() => {
  251. setUsername(d.user);
  252. setErr(null);
  253. }}
  254. >
  255. <strong>{d.label}</strong>
  256. <span>{d.user}</span>
  257. </button>
  258. ))}
  259. </div>
  260. </section>
  261. ) : null}
  262. </div>
  263. </div>
  264. </div>
  265. );
  266. }