CustomerSelfServicePages.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * 客户自助侧页面(第 3 册 6.13 / 6.14)—— 真源=klt-bos。
  3. *
  4. * 分账号读写:`POST /sub_account/get_list@v1` + `POST /sub_account/update@v1`;
  5. * [实] `/sub_account/*` ⛔ 新增与删除路由 ⇒ 只保留「修改真实姓名」;
  6. * 客户身份展示走 `/customer/get@v1`(**自读**,主体号取自会话),⛔ 下发项标「后端未下发」。
  7. *
  8. * ⚠️ 本页此前取的是 `/customer/get_list@v1`——那是**运营侧**名录读口(权限键 `bos.customer.list`),
  9. * 客户角色本就不该拿到它(2026-11-14「我的套餐/分账号」整页空态的根因)。自读口只要求
  10. * `bos.customer.view`,且行集由后端按主体收窄,客户看不到别家档案。
  11. */
  12. import { useMemo, useState } from "react";
  13. import { PageIntro } from "../components/PageIntro";
  14. import { useNavigate, useParams } from "react-router";
  15. import { FetchError } from "../components/fetchError";
  16. import { PencilLine } from "lucide-react";
  17. import { Button } from "../components/ui/button";
  18. import { Input } from "../components/ui/input";
  19. import { ListPager } from "../components/ListPager";
  20. import { OpsNotice, OpsStatus, OpsTableEmpty } from "../components/OperationsKit";
  21. import { Tri } from "../components/triState";
  22. import { slicePage, useLocalPage } from "../lib/list-paging";
  23. import { useDemoIdentity } from "../lib/demo-session";
  24. import { useProfileOverlay } from "../components/layout/ProfileOverlay";
  25. import { useLoad } from "../lib/useLoad";
  26. import { ApiError } from "../lib/api/http";
  27. import { customerGet, portalPlanBoard, subAccountGetList, subAccountUpdate } from "../lib/api/bos";
  28. import { ACCT_STATUS, PLAN_FAMILY, label } from "../lib/api/labels";
  29. import type { PortalPlanBoardRow, SubAccountRow } from "../lib/api/types";
  30. import { isSeatPlaceholder, seatDisplayName } from "../lib/customer-subaccounts";
  31. import "../styles/operations.css";
  32. const MISSING = "后端未下发";
  33. /** 金额分 → 「¥1,234.56」;`null` 不可用 ⇒ 显「—」(§4.3 列 3 不许拿订阅价顶替)。 */
  34. function money(cents: number | null): string {
  35. if (cents === null || !Number.isFinite(cents)) return "—";
  36. return `¥${(cents / 100).toLocaleString("zh-CN", { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;
  37. }
  38. /** 线路出口列:数组逐项一行,空集 ⇒「—」。 */
  39. function exits(exitsValue: string[] | null) {
  40. if (!exitsValue || exitsValue.length === 0) return <span className="opacity-60">—</span>;
  41. return <>{exitsValue.map((e) => <div key={e}>{e}</div>)}</>;
  42. }
  43. /**
  44. * 门户 4「我的套餐」(第 9 册 §4)——真源=`/portal/plan_board@v1`。
  45. *
  46. * 两表一次取数、由**服务端**切分与排序(C79):⛔ 前端分两次查询再自行归类,
  47. * 那会让两次查询之间的状态翻转把同一行摆进两张表。
  48. * 主体号一律由会话派生(`/portal/plan_board@v1` 硬拒载荷里的 `customer_id`),
  49. * 所以本页⛔ 传主体号;`useParams().customerNo` 只用来做会话回退比对。
  50. */
  51. export function CustomerPlanPage() {
  52. const { customerNo: customerNoParam = "" } = useParams();
  53. const identity = useDemoIdentity();
  54. const customerNo = customerNoParam || identity?.customerNo || "";
  55. const board = useLoad(() => portalPlanBoard({ view: "board" }), []);
  56. const data = board.data;
  57. if (board.error) {
  58. return <section className="ops-page"><header className="ops-heading"><div><p className="ops-eyebrow"><span /> 企业门户 · 我的套餐</p><h1>我的套餐</h1></div></header>
  59. <FetchError message={board.error.message} onRetry={board.reload} />
  60. </section>;
  61. }
  62. if (board.loading && !data) {
  63. return <section className="ops-page"><header className="ops-heading"><div><p className="ops-eyebrow"><span /> 企业门户 · 我的套餐</p><h1>我的套餐</h1></div></header>
  64. <div className="ops-card ops-plan-card"><div><h2>套餐加载中…</h2><p>正在按会话主体号读取当前套餐与往期套餐。</p></div></div>
  65. </section>;
  66. }
  67. if (!data) {
  68. return <section className="ops-page"><header className="ops-heading"><div><p className="ops-eyebrow"><span /> 企业门户 · 我的套餐</p><h1>我的套餐</h1></div></header>
  69. <div className="ops-card ops-plan-card"><div><h2>套餐暂不可读</h2><p>{customerNo ? `会话主体号 ${customerNo}` : "当前会话未带客户主体号"} 未取到套餐数据,请稍后重试或联系运维核实账号权限。</p></div></div>
  70. </section>;
  71. }
  72. const current = data.current_rows;
  73. const history = data.history_rows;
  74. const table = (title: string, rows: PortalPlanBoardRow[], empty: string) =>
  75. <div className="ops-list-shell list-shell-fill">
  76. <div className="ops-card-title-row"><h2>{title}</h2><span className="ops-muted">共 {rows.length} 条</span></div>
  77. <div className="ops-table-wrap measured-table-scroll"><table className="ops-table">
  78. <thead><tr><th>套餐 / 合同</th><th>运营商</th><th>总金额</th><th>方式</th><th>线路出口</th><th>期间</th><th>状态</th></tr></thead>
  79. <tbody>
  80. {rows.map((row) => <tr key={row.subscription_id}>
  81. <td>
  82. <strong>{row.plan_name}</strong>
  83. <div className="ops-muted">{row.contract_no ?? "—"}</div>
  84. {/* 交付现状摘要(第 9 册 §4.3 行内摘要):台账口径,只读;⛔ 不占列、不开详情口 */}
  85. {row.ip_summary && <div className="ops-muted">在册 {row.ip_summary.live_ip_count} 个 IP · {row.ip_summary.live_account_count} 个在用账号{row.ip_summary.released_ip_count > 0 ? `(已回收 ${row.ip_summary.released_ip_count})` : ""}</div>}
  86. </td>
  87. <td>{data.party.operator_name ?? <Tri tone="missing" text={MISSING} title="客户档案未挂运营商:合同签订方一列无值,非取数失败" />}</td>
  88. <td>{money(row.contract_amount_cents)}</td>
  89. <td>{row.plan_family ? label(PLAN_FAMILY, row.plan_family) : "—"}</td>
  90. <td>{exits(row.line_exits)}</td>
  91. <td>{row.start_date ?? "—"} 至 {row.end_date ?? "—"}</td>
  92. <td><OpsStatus tone={row.status.key === "active" ? "success" : row.status.key === "pending" ? "warning" : "neutral"}>{row.status.label}</OpsStatus></td>
  93. </tr>)}
  94. {!rows.length && <OpsTableEmpty colSpan={7}>{empty}</OpsTableEmpty>}
  95. </tbody>
  96. </table></div>
  97. <p className="ops-muted" role="note">{data.amount_note}</p>
  98. </div>;
  99. return <section className="ops-page"><header className="ops-heading"><div><p className="ops-eyebrow"><span /> 企业门户 · 我的套餐</p><h1>{data.party.customer_name || customerNo}</h1><p>所属区域:{data.party.region_path ?? <Tri tone="missing" text={MISSING} />} · 运营商:{data.party.operator_name ?? <Tri tone="missing" text={MISSING} />}</p></div></header>
  100. {table("当前套餐", current, "当前没有待生效或生效中的套餐。")}
  101. {table("往期套餐", history, "没有已到期或已终止的套餐。")}
  102. {data.notes.length > 0 && <ul className="ops-muted" role="note">{data.notes.map((n) => <li key={n}>{n}</li>)}</ul>}
  103. </section>;
  104. }
  105. export function CustomerAccountsPage() {
  106. const { customerNo: customerNoParam = "" } = useParams();
  107. const identity = useDemoIdentity();
  108. const navigate = useNavigate();
  109. const { openProfile } = useProfileOverlay();
  110. const customerNo = customerNoParam || identity?.customerNo || "";
  111. // 额度与主体名走自读口 `/customer/get@v1`(`sub_account_count`/`customer_name`),⛔ 运营侧名录。
  112. const detail = useLoad(
  113. () => (customerNo ? customerGet({ customer_id: customerNo }) : Promise.resolve(null)),
  114. [customerNo],
  115. );
  116. const customer = detail.data;
  117. const state = useLoad(() => subAccountGetList({ customer_id: customerNo, page: 0, page_size: 100 }), [customerNo]);
  118. const rows: SubAccountRow[] = state.data?.items ?? [];
  119. const paging = useLocalPage(rows.length);
  120. const pageRows = slicePage(rows, paging.safePage);
  121. const [editing, setEditing] = useState<SubAccountRow | null>(null);
  122. const [name, setName] = useState("");
  123. const [notice, setNotice] = useState("");
  124. const [error, setError] = useState("");
  125. const [busy, setBusy] = useState(false);
  126. const quota = customer?.sub_account_count ?? null;
  127. const used = state.data?.total ?? null;
  128. const remaining = quota !== null && used !== null ? Math.max(0, quota - used) : null;
  129. const ownerName = useMemo(() => customer?.customer.customer_name ?? customerNo, [customer, customerNo]);
  130. async function save() {
  131. if (!editing) return;
  132. setBusy(true);
  133. setError("");
  134. try {
  135. await subAccountUpdate({ sub_account_id: editing.sub_account_id, display_name: name });
  136. setEditing(null);
  137. setNotice("分账号真实姓名已更新(后端回执为准)。");
  138. state.reload();
  139. } catch (e) {
  140. setError((e as ApiError).message || "取数失败");
  141. } finally {
  142. setBusy(false);
  143. }
  144. }
  145. return <section className="ops-page page-fill"><header className="ops-heading"><div><p className="ops-eyebrow"><span /> 企业门户 · 分账号管理</p><h1>{ownerName}</h1><PageIntro>看看企业的分账号额度还剩多少,并为已开通的账号更新使用人姓名。</PageIntro></div><div><Button variant="outline" onClick={() => navigate("/workspace/plan")}>查看我的套餐</Button><Button onClick={openProfile}>修改登录密码</Button></div></header>
  146. <div className="ops-quota-meter ops-card"><span><small>分账号额度</small><strong>{quota ?? <Tri tone="missing" text={MISSING} title={`${MISSING}:/customer/get@v1 未回 sub_account_count`} />} · 已用 {used ?? <Tri tone="missing" text={MISSING} />} · 剩余 {remaining ?? <Tri tone="missing" text={MISSING} />}</strong></span><p>后端不提供分账号新增/删除路由,本页仅支持修改真实姓名。</p></div>
  147. <div className="ops-list-shell list-shell-fill">
  148. {state.error && <FetchError message={state.error.message} onRetry={state.reload} />}
  149. <div className="ops-table-wrap measured-table-scroll"><table className="ops-table"><thead><tr><th>登录名</th><th>真实姓名</th><th>状态</th><th>操作</th></tr></thead><tbody>{pageRows.map((row) => <tr key={row.sub_account_id}>
  150. {/* 三态(巡检 P1-D2):未开通/已停用的席位没有登录名属正常态,⛔ 标「后端未下发」。 */}
  151. <td><strong>{row.username?.trim()
  152. ? row.username
  153. : row.status === "not_opened"
  154. ? <span className="ops-muted" title="该席位尚未开通,登录名在开通后登记">未开通</span>
  155. : row.status === "stopped"
  156. ? <span className="ops-muted" title="该席位已停用,登录名随停用不再展示">已停用</span>
  157. : <Tri tone="missing" text={MISSING} title="sub_account.username 已下发、值为空" />}</strong></td>
  158. <td>{seatDisplayName(row)
  159. ? <>{seatDisplayName(row)}{isSeatPlaceholder(row) && <small className="ml-1 text-xs opacity-70">(席位占位·姓名待填)</small>}</>
  160. : row.status === "not_opened"
  161. ? <span className="ops-muted" title="该席位尚未开通,真实姓名开通后由客户填写">未开通(开通后填写)</span>
  162. : <Tri tone="missing" text={MISSING} title="sub_account.display_name 已下发、值为空" />}</td>
  163. <td><OpsStatus tone={row.status === "active" || row.status === "normal" ? "success" : row.status === "stopped" || row.status === "disabled" ? "neutral" : "warning"}>{label(ACCT_STATUS, row.status) || MISSING}</OpsStatus></td>
  164. <td><Button variant="ghost" size="sm" onClick={() => { setEditing(row); setName(row.display_name ?? ""); setError(""); }}><PencilLine size={14} />修改姓名</Button></td>
  165. </tr>)}{!rows.length && !state.loading && <OpsTableEmpty colSpan={4}>{state.error ? "分账号取数失败。" : "暂无分账号。"}</OpsTableEmpty>}{!rows.length && state.loading && <OpsTableEmpty colSpan={4}>分账号加载中…</OpsTableEmpty>}</tbody></table></div>
  166. <ListPager total={rows.length} page={paging.safePage} pageSize={paging.pageSize} onPageChange={paging.setPage} unit="个" />
  167. </div>
  168. {editing && <div className="ops-confirm-backdrop" onMouseDown={() => setEditing(null)}><div className="ops-confirm-card" onMouseDown={(event) => event.stopPropagation()} role="dialog" aria-modal="true"><p className="ops-confirm-kicker">修改真实姓名</p><h2>{editing.username ?? editing.sub_account_id}</h2><label className="ops-field">真实姓名<span><Input value={name} onChange={(e) => setName(e.target.value)} /></span></label>{error && <p className="ops-form-error" role="alert">{error}</p>}<div className="ops-confirm-actions"><Button variant="outline" onClick={() => setEditing(null)}>取消</Button><Button disabled={busy || !name.trim()} onClick={() => void save()}>{busy ? "保存中…" : "保存"}</Button></div></div></div>}
  169. {notice && <OpsNotice message={notice} onClose={() => setNotice("")} />}
  170. </section>;
  171. }