| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656 |
- import { useCallback, useEffect, useRef, useState, type FormEvent, type ReactNode } from "react";
- import { BellRing, KeyRound, Mail, Pencil, Smartphone, UserRound, X } from "lucide-react";
- import { Button } from "../components/ui/button";
- import { Input } from "../components/ui/input";
- import { Tri } from "../components/triState";
- import { useDemoAccount, useDemoIdentity } from "../lib/demo-session";
- import { useSession } from "../auth/session";
- import {
- accountChangePassword,
- accountNotifyEmail,
- accountNotifyPref,
- accountNotifyPrefSave,
- accountProfileUpdate,
- } from "../lib/api/bos";
- import type { AccountNotifyPrefResult } from "../lib/api/types";
- import { ACCT_STATUS, label } from "../lib/api/labels";
- import { ApiError } from "../lib/api/http";
- import { WORD_FETCH_FAILED, WORD_UNCONFIGURED } from "../lib/api/lockedCopy";
- /** 三态:未登录=未配置,登录后后端没下发该键=取数失败,⛔ 用「—」冒充。 */
- function field(value: string | null | undefined, missing = WORD_UNCONFIGURED) {
- if (value === undefined) return <Tri tone="fail" text={WORD_FETCH_FAILED} />;
- if (value === null || value === "") return <Tri tone="missing" text={missing} />;
- return value;
- }
- function apiMessage(e: unknown, fallback: string) {
- return e instanceof ApiError ? e.message : fallback;
- }
- /**
- * 「个人资料」页(AC-05)——本人账号信息 + 自助改密 + 通知投递通道。
- *
- * 呈现形态:业主 2026-11-14 定案为**叠在当前页面上的浮层**(见 `components/layout/ProfileOverlay.tsx`),
- * 由顶栏用户菜单就地打开,关闭按钮/Esc/点遮罩即可退出;⛔ 再作为独立页面把用户带走。
- *
- * 账号信息取自 `GET /auth/context@v1`(后端已下发姓名/主体/手机号摘要/账号状态);
- * 姓名可点输入框右侧悬停出现的编辑图标就地改(`POST /account/profile/update@v1`,只写账号级
- * 自填姓名,⛔ 动主体档案);改密走 `POST /account/change_password@v1`(平台自助口,只改本人这一行);
- * 通知投递通道与投递邮箱(AC-06)同页承载(需求 §3.4、总则 §16.2 的 `/bos/me`);
- * 换绑手机号仍由运营侧代办,本页⛔ 提供自助换绑表单。
- * 校验口径:原密码必填、新密码 8–32 位、两次一致、新旧不同(`KLT-T223` 前置拦截)。
- */
- export function ProfilePage({ titleId }: { titleId?: string } = {}) {
- const account = useDemoAccount();
- const identity = useDemoIdentity();
- const { ctx, refresh } = useSession();
- // 改密界面由第一张卡片右侧的图标按钮唤起(业主 2026-11-14:⛔ 再占一张卡片)。
- const [pwdOpen, setPwdOpen] = useState(false);
- const [pwdNotice, setPwdNotice] = useState("");
- // 姓名就地编辑(姓名是账号持有人本人的姓名,⛔ 主体档案名称)
- const [editingName, setEditingName] = useState(false);
- const [nameDraft, setNameDraft] = useState("");
- const [namePending, setNamePending] = useState(false);
- const [nameError, setNameError] = useState("");
- const [nameNotice, setNameNotice] = useState("");
- function startEditName() {
- setNameDraft(ctx?.display_name ?? "");
- setNameError("");
- setNameNotice("");
- setEditingName(true);
- }
- function cancelEditName() {
- if (namePending) return;
- setEditingName(false);
- setNameError("");
- }
- async function saveName() {
- if (namePending) return;
- const value = nameDraft.trim();
- if (!value) {
- setNameError("请输入姓名");
- return;
- }
- setNamePending(true);
- setNameError("");
- try {
- await accountProfileUpdate({ display_name: value });
- // 顶栏称呼与资料页同源 `auth/context@v1`,保存后回读一次,⛔ 让两处显示打架。
- await refresh();
- setEditingName(false);
- setNameNotice("姓名已保存。");
- } catch (e) {
- setNameError(apiMessage(e, "保存姓名失败,请稍后重试"));
- } finally {
- setNamePending(false);
- }
- }
- const displayName = ctx?.display_name ?? ctx?.username ?? identity?.login ?? account;
- const acctStatus = ctx?.acct_status ? label(ACCT_STATUS, ctx.acct_status) : undefined;
- return (
- // ⚠️ 宽度跟随容器:资料页现在只作为浮层内容渲染,⛔ 再套 `max-w-3xl`
- // (那会让卡片比面板窄、右侧空出一条,实测面板 920 时左 25/右 127)。
- <section className="w-full">
- {/* 浮层内右上角有固定关闭按钮:标题区留出右侧安全区,⛔ 让副标题压到按钮下面。 */}
- <header className={titleId ? "pr-12" : undefined}>
- <h1 className="text-2xl font-semibold tracking-tight" id={titleId}>个人资料</h1>
- <p className="mt-2 text-sm text-muted-foreground">本人账号信息、密码维护与通知投递通道配置;换绑手机号请联系运营代办。</p>
- </header>
- <div className="mt-6 flex items-center gap-4 rounded-lg border border-border bg-card p-6 shadow-[var(--shadow-panel)]">
- <span className="grid size-12 shrink-0 place-items-center rounded-full bg-primary-soft text-primary"><UserRound aria-hidden="true" size={22} /></span>
- <div className="min-w-0 flex-1">
- <p className="font-semibold">{field(displayName)}</p>
- <p className="mt-1 text-sm text-muted-foreground">{identity?.title ?? "尚未接入认证"}</p>
- {identity && <p className="mt-1 text-sm text-muted-foreground">登录账号:{identity.login}</p>}
- </div>
- {/* 改密入口挂在名片右侧:点它唤起改密浮层,⛔ 再占一张卡片(业主 2026-11-14)。
- 用描边图标按钮而非纯 ghost:这是唯一的功能入口,需要一眼可见(⛔ 悬停才出现)。 */}
- <Button
- aria-haspopup="dialog"
- aria-label="修改密码"
- className="shrink-0 text-muted-foreground hover:text-foreground"
- onClick={() => { setPwdNotice(""); setPwdOpen(true); }}
- size="icon"
- title="修改密码"
- type="button"
- variant="outline"
- >
- <KeyRound aria-hidden="true" size={18} />
- </Button>
- </div>
- {pwdNotice && <p className="mt-2 text-sm text-primary" role="status">{pwdNotice}</p>}
- <dl className="mt-4 grid grid-cols-2 gap-px overflow-hidden rounded-lg border border-border bg-border text-sm sm:grid-cols-3">
- <div className="bg-card px-4 py-3">
- <dt className="text-xs text-muted-foreground">姓名</dt>
- <dd className="mt-1">
- {editingName ? (
- <div className="flex flex-col gap-1.5">
- <div className="flex flex-wrap items-center gap-1.5">
- <Input
- aria-label="姓名"
- autoFocus
- className="h-8 min-w-0 flex-1 basis-full sm:basis-0"
- disabled={namePending}
- maxLength={64}
- onChange={(e) => { setNameDraft(e.target.value); setNameError(""); }}
- onKeyDown={(e) => {
- if (e.key === "Enter") { e.preventDefault(); void saveName(); }
- // ⚠️ 必须拦住冒泡:资料浮层在 document 上监听 Esc 关闭,⛔ 让「取消改名」把整层关掉。
- if (e.key === "Escape") { e.preventDefault(); e.stopPropagation(); cancelEditName(); }
- }}
- value={nameDraft}
- />
- <Button disabled={namePending} onClick={() => void saveName()} size="sm" type="button">
- {namePending ? "保存中…" : "保存"}
- </Button>
- <Button disabled={namePending} onClick={cancelEditName} size="sm" type="button" variant="ghost">
- 取消
- </Button>
- </div>
- {nameError && <p className="text-xs text-destructive" role="alert">{nameError}</p>}
- </div>
- ) : (
- <span className="group/name flex items-start gap-1">
- <span className="break-words font-medium">{field(ctx?.display_name)}</span>
- <button
- aria-label="修改姓名"
- className="cursor-pointer rounded p-1 text-muted-foreground opacity-0 transition-opacity hover:bg-accent hover:text-foreground focus-visible:opacity-100 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring group-hover/name:opacity-100"
- onClick={startEditName}
- title="修改姓名"
- type="button"
- >
- <Pencil aria-hidden="true" size={13} />
- </button>
- </span>
- )}
- </dd>
- </div>
- <div className="bg-card px-4 py-3"><dt className="text-xs text-muted-foreground">账号用途</dt><dd className="mt-1 font-medium">{field(identity?.title ?? ctx?.role_code)}</dd></div>
- <div className="bg-card px-4 py-3"><dt className="text-xs text-muted-foreground">所属主体</dt><dd className="mt-1 font-medium">{field(ctx?.subject_name)}</dd></div>
- <div className="bg-card px-4 py-3"><dt className="text-xs text-muted-foreground">登录名</dt><dd className="mt-1 font-medium">{field(ctx?.username ?? identity?.login)}</dd></div>
- <div className="bg-card px-4 py-3"><dt className="text-xs text-muted-foreground">账号状态</dt><dd className="mt-1 font-medium">{field(acctStatus)}</dd></div>
- <div className="bg-card px-4 py-3"><dt className="text-xs text-muted-foreground">绑定手机号</dt><dd className="mt-1 font-medium">{field(ctx?.mobile_hint, "未绑定手机号")}</dd></div>
- </dl>
- {nameNotice && <p className="mt-2 text-sm text-primary" role="status">{nameNotice}</p>}
- <NotifyPrefPanel />
- {pwdOpen && (
- <PasswordDialog
- onClose={() => setPwdOpen(false)}
- onSaved={() => {
- setPwdOpen(false);
- setPwdNotice("密码已修改,下次登录请使用新密码。");
- }}
- />
- )}
- </section>
- );
- }
- /**
- * 改密浮层(AC-05)——由名片右侧的钥匙图标唤起,⛔ 再占一张「修改密码」卡片。
- *
- * 层级(`z-[70]`)高于个人资料浮层(`z-[60]`):看起来是资料浮层上的第二层。
- * ⚠️ Esc 必须**拦在本层**(`stopPropagation`),否则会继续冒泡到资料浮层的
- * document 监听器、把整个资料浮层一起关掉。
- *
- * 校验口径:原密码必填、新密码 8–32 位、两次一致、新旧不同(`KLT-T223` 前置拦截);
- * 真正的口令校验与落库在平台侧(`POST /account/change_password@v1`),本层只做入口拦截。
- */
- function PasswordDialog({ onClose, onSaved }: { onClose: () => void; onSaved: () => void }) {
- const [oldPassword, setOldPassword] = useState("");
- const [newPassword, setNewPassword] = useState("");
- const [confirmPassword, setConfirmPassword] = useState("");
- const [error, setError] = useState("");
- const [pending, setPending] = useState(false);
- const oldRef = useRef<HTMLInputElement>(null);
- useEffect(() => {
- oldRef.current?.focus();
- }, []);
- async function submit(event: FormEvent<HTMLFormElement>) {
- event.preventDefault();
- if (pending) return;
- setError("");
- if (!oldPassword) return setError("请输入原密码");
- if (newPassword.length < 8 || newPassword.length > 32) return setError("新密码需 8 至 32 位");
- if (newPassword !== confirmPassword) return setError("两次输入的新密码不一致");
- if (newPassword === oldPassword) return setError("新密码不能与原密码相同");
- setPending(true);
- try {
- await accountChangePassword({ old_password: oldPassword, new_password: newPassword });
- onSaved();
- } catch (e) {
- setError(apiMessage(e, "修改密码失败,请稍后重试"));
- } finally {
- setPending(false);
- }
- }
- return (
- <div
- className="fixed inset-0 z-[70] grid place-items-center bg-black/50 p-4"
- // 点遮罩空白处关闭本层(⛔ 穿透到资料浮层的遮罩)。
- onMouseDown={(event) => {
- if (event.target === event.currentTarget && !pending) onClose();
- }}
- // 两层浮层叠着:Esc 只关最上面这层(⛔ 冒泡到资料浮层的监听器)。
- onKeyDown={(event) => {
- if (event.key !== "Escape") return;
- event.preventDefault();
- event.stopPropagation();
- if (!pending) onClose();
- }}
- >
- <form
- aria-labelledby="password-dialog-title"
- aria-modal="true"
- className="w-[min(440px,100%)] rounded-xl border border-border bg-card p-6 shadow-[var(--shadow-panel)]"
- noValidate
- onSubmit={submit}
- role="dialog"
- >
- <div className="flex items-start justify-between gap-4">
- <div>
- <h2 className="flex items-center gap-2 text-base font-semibold" id="password-dialog-title">
- <KeyRound aria-hidden="true" size={17} />修改密码
- </h2>
- <p className="mt-1 text-sm text-muted-foreground">只改本人这一行的登录密码;改完请用新密码登录。</p>
- </div>
- <Button
- aria-label="关闭修改密码"
- className="shrink-0 text-muted-foreground"
- disabled={pending}
- onClick={onClose}
- size="icon"
- title="关闭(Esc)"
- type="button"
- variant="ghost"
- >
- <X aria-hidden="true" size={18} />
- </Button>
- </div>
- <div className="mt-4 grid gap-4">
- <label className="block text-sm">
- <span className="field-label mb-1.5 block">原密码</span>
- <Input
- autoComplete="current-password"
- disabled={pending}
- onChange={(e) => { setOldPassword(e.target.value); setError(""); }}
- ref={oldRef}
- type="password"
- value={oldPassword}
- />
- </label>
- <label className="block text-sm">
- <span className="field-label mb-1.5 block">新密码</span>
- <Input
- autoComplete="new-password"
- disabled={pending}
- onChange={(e) => { setNewPassword(e.target.value); setError(""); }}
- placeholder="8 至 32 位"
- type="password"
- value={newPassword}
- />
- </label>
- <label className="block text-sm">
- <span className="field-label mb-1.5 block">确认新密码</span>
- <Input
- autoComplete="new-password"
- disabled={pending}
- onChange={(e) => { setConfirmPassword(e.target.value); setError(""); }}
- type="password"
- value={confirmPassword}
- />
- </label>
- </div>
- {error && <p className="mt-3 text-sm text-destructive" role="alert">{error}</p>}
- <div className="mt-5 flex justify-end gap-2">
- <Button disabled={pending} onClick={onClose} type="button" variant="outline">取消</Button>
- <Button disabled={pending} type="submit">{pending ? "提交中…" : "保存新密码"}</Button>
- </div>
- </form>
- </div>
- );
- }
- /** 投递地址的就地编辑状态(目前只有邮箱通道用;⛔ 与通道开关混在一个 pending 里)。 */type AddressEdit = {
- editing: boolean;
- draft: string;
- pending: boolean;
- error: string;
- /** 已登记本人地址时给「清空登记」;为 `undefined` 表示当前是档案回落地址、无本人登记可清。 */
- onClear?: () => void;
- onDraftChange: (next: string) => void;
- onStart: () => void;
- onCancel: () => void;
- onSave: () => void;
- };
- /** 通道卡片:站内固定开启由 `fixed` 表达,其余两路由服务端 `available` 决定能否切换。 */
- function ChannelRow({
- icon,
- title,
- description,
- enabled,
- available,
- addressHint,
- fixed,
- pending,
- onToggle,
- addressEdit,
- emptyAddressText,
- }: {
- icon: ReactNode;
- title: string;
- description: string;
- enabled: boolean;
- available: boolean;
- addressHint: string | null;
- fixed?: boolean;
- pending: boolean;
- onToggle?: (next: boolean) => void;
- /** 给出即把「投递地址」做成可悬停编辑(业主 2026-11-14:邮箱地址就地录入,⛔ 另开一张卡片)。 */
- addressEdit?: AddressEdit;
- /** 地址为空时的占位文案(只在 `addressEdit` 存在时用得上:空地址也要留出入口)。 */
- emptyAddressText?: string;
- }) {
- const editing = addressEdit?.editing ?? false;
- const showAddressLine = editing || Boolean(addressHint) || Boolean(addressEdit);
- return (
- // `group/addr` 挂整行:悬停这张卡任意位置就露出地址编辑图标(⛔ 必须精准悬停到文字上)。
- <div className="group/addr flex items-start gap-4 border-t border-border px-6 py-4 first:border-t-0">
- <span className="mt-0.5 grid size-9 shrink-0 place-items-center rounded-md bg-primary-soft text-primary">{icon}</span>
- <div className="min-w-0 flex-1">
- <div className="flex items-center gap-2">
- <p className="font-medium">{title}</p>
- {fixed && <span className="rounded bg-muted px-1.5 py-0.5 text-[11px] text-muted-foreground">固定开启 · 不可关闭</span>}
- {!fixed && !available && <span className="rounded bg-muted px-1.5 py-0.5 text-[11px] text-muted-foreground">当前不可切换</span>}
- </div>
- <p className="mt-1 text-sm leading-6 text-muted-foreground">{description}</p>
- {addressEdit && editing ? (
- <div className="mt-1.5 flex flex-col gap-1.5">
- <div className="flex flex-wrap items-center gap-1.5">
- <Input
- aria-label={`${title}投递地址`}
- autoFocus
- className="h-8 min-w-0 flex-1 basis-full sm:basis-64"
- disabled={addressEdit.pending}
- maxLength={254}
- onChange={(e) => addressEdit.onDraftChange(e.target.value)}
- onKeyDown={(e) => {
- if (e.key === "Enter") { e.preventDefault(); addressEdit.onSave(); }
- // 同上:Esc 只取消本行编辑(⛔ 冒泡到资料浮层把整层关掉)。
- if (e.key === "Escape") { e.preventDefault(); e.stopPropagation(); addressEdit.onCancel(); }
- }}
- placeholder="例如 notify@example.com"
- type="email"
- value={addressEdit.draft}
- />
- <Button disabled={addressEdit.pending} onClick={addressEdit.onSave} size="sm" type="button">
- {addressEdit.pending ? "保存中…" : "保存"}
- </Button>
- <Button disabled={addressEdit.pending} onClick={addressEdit.onCancel} size="sm" type="button" variant="ghost">
- 取消
- </Button>
- {addressEdit.onClear && (
- <Button disabled={addressEdit.pending} onClick={addressEdit.onClear} size="sm" type="button" variant="outline">
- 清空登记
- </Button>
- )}
- </div>
- <p className="text-xs text-muted-foreground">
- 保存后立即生效;清除本人登记请点「清空登记」(运营商/合伙人会回落到主体档案邮箱)。
- </p>
- {addressEdit.error && <p className="text-xs text-destructive" role="alert">{addressEdit.error}</p>}
- </div>
- ) : showAddressLine ? (
- <p className="mt-1 flex items-start gap-1 text-sm text-muted-foreground">
- <span>
- 投递地址:
- <span className={addressHint ? "font-medium text-foreground" : undefined}>
- {addressHint ?? emptyAddressText ?? "未登记"}
- </span>
- </span>
- {addressEdit && !editing && (
- <button
- aria-label={`修改${title}投递地址`}
- className="cursor-pointer rounded p-1 text-muted-foreground opacity-0 transition-opacity hover:bg-accent hover:text-foreground focus-visible:opacity-100 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring group-hover/addr:opacity-100"
- onClick={addressEdit.onStart}
- title={`修改${title}投递地址`}
- type="button"
- >
- <Pencil aria-hidden="true" size={13} />
- </button>
- )}
- </p>
- ) : null}
- </div>
- {fixed ? (
- <span className="mt-1 text-sm font-medium text-primary">已开启</span>
- ) : (
- <label className="ops-form-check mt-1 shrink-0">
- <input
- checked={enabled && available}
- disabled={!available || pending}
- onChange={(e) => onToggle?.(e.target.checked)}
- type="checkbox"
- />
- {enabled && available ? "已开启" : "已关闭"}
- </label>
- )}
- </div>
- );
- }
- /**
- * 「通知投递通道」面板(AC-06)——并入「个人资料」页(需求 §3.4、总则 §16.2 的 `/bos/me`)。
- *
- * 三个开关是**本人账号**的投递偏好(⛔ 不动主体档案、⛔ 不影响通知是否产生):
- * 站内恒开不可关;邮箱默认开、需有生效收件地址;短信默认关、需已绑定手机号。
- * 邮箱地址在「邮箱」这一行内就地登记/清空(悬停行内地址出铅笔图标),为空时
- * 运营商/合伙人回落档案邮箱(页面如实标出来源)。⛔ 再单开一张「通知投递邮箱」卡片。
- */
- function NotifyPrefPanel() {
- const { ctx } = useSession();
- const [pref, setPref] = useState<AccountNotifyPrefResult | null>(null);
- const [loading, setLoading] = useState(true);
- const [loadError, setLoadError] = useState("");
- const [actionError, setActionError] = useState("");
- const [notice, setNotice] = useState("");
- const [pending, setPending] = useState(false);
- // 邮箱投递地址就地编辑(业主 2026-11-14:并入本卡片、⛔ 再单开一张「通知投递邮箱」卡)
- const [editingEmail, setEditingEmail] = useState(false);
- const [emailDraft, setEmailDraft] = useState("");
- const [emailPending, setEmailPending] = useState(false);
- const [emailError, setEmailError] = useState("");
- const load = useCallback(async (options?: { quiet?: boolean }) => {
- if (!options?.quiet) setLoading(true);
- setLoadError("");
- try {
- setPref(await accountNotifyPref());
- } catch (e) {
- setLoadError(apiMessage(e, "读取通知投递偏好失败,请稍后重试"));
- } finally {
- if (!options?.quiet) setLoading(false);
- }
- }, []);
- useEffect(() => {
- if (ctx) void load();
- }, [ctx, load]);
- async function saveToggles(next: { mail: boolean; sms: boolean }) {
- if (!pref || pending) return;
- setPending(true);
- setActionError("");
- setNotice("");
- try {
- await accountNotifyPrefSave({ mail_enabled: next.mail, sms_enabled: next.sms });
- setPref({
- ...pref,
- mail: { ...pref.mail, enabled: next.mail },
- sms: { ...pref.sms, enabled: next.sms },
- items: pref.items.map((item) =>
- item.channel === "mail" ? { ...item, enabled: next.mail }
- : item.channel === "sms" ? { ...item, enabled: next.sms }
- : item,
- ),
- });
- setNotice("通知投递偏好已保存。");
- } catch (e) {
- setActionError(apiMessage(e, "保存通知投递偏好失败,请稍后重试"));
- // 失败回读,避免界面停留在未落库的乐观值上。
- await load({ quiet: true });
- } finally {
- setPending(false);
- }
- }
- async function saveEmail(value: string) {
- if (!pref || emailPending) return;
- setEmailPending(true);
- setEmailError("");
- setActionError("");
- setNotice("");
- try {
- const result = await accountNotifyEmail({ email: value });
- setPref({
- ...pref,
- mail: {
- ...pref.mail,
- available: result.address_hint !== null,
- address_hint: result.address_hint,
- address_source: result.address_source,
- },
- items: pref.items.map((item) =>
- item.channel === "mail"
- ? { ...item, available: result.address_hint !== null, address_hint: result.address_hint }
- : item,
- ),
- });
- setEditingEmail(false);
- setEmailDraft("");
- setNotice(value.trim() ? "通知投递邮箱已保存。" : "通知投递邮箱已清空。");
- } catch (e) {
- setEmailError(apiMessage(e, "保存通知投递邮箱失败,请稍后重试"));
- } finally {
- setEmailPending(false);
- }
- }
- const mail = pref?.mail;
- const sms = pref?.sms;
- const inapp = pref?.inapp;
- const usingFallback = mail?.address_source === "operator_profile" || mail?.address_source === "partner_profile";
- const hasOwnEmail = mail?.address_source === "account";
- return (
- <>
- <section className="mt-6 overflow-hidden rounded-lg border border-border bg-card shadow-[var(--shadow-panel)]">
- <header className="flex items-center justify-between px-6 py-4">
- <div>
- <h2 className="flex items-center gap-2 text-base font-semibold"><BellRing aria-hidden="true" size={17} />通知投递通道</h2>
- <p className="mt-1 text-sm text-muted-foreground">开关只作用于本人账号;站内标记与待处理提示不受影响。</p>
- </div>
- {loading && <span className="text-sm text-muted-foreground">读取中…</span>}
- </header>
- {loadError && (
- <div className="border-t border-border px-6 py-4 text-sm text-destructive" role="alert">
- {loadError}
- <Button className="ml-3" onClick={() => void load()} size="sm" type="button" variant="outline">重试</Button>
- </div>
- )}
- {!loadError && !loading && !pref && (
- <p className="border-t border-border px-6 py-4 text-sm text-muted-foreground">后端未返回通知投递偏好。</p>
- )}
- {pref && (
- <>
- <ChannelRow
- addressHint={inapp?.address_hint ?? null}
- available
- description="站内消息与待处理提示始终送达,是业务留痕的兜底通道。"
- enabled
- fixed
- icon={<BellRing aria-hidden="true" size={17} />}
- pending={pending}
- title="站内"
- />
- <ChannelRow
- addressEdit={{
- editing: editingEmail,
- draft: emailDraft,
- pending: emailPending,
- error: emailError,
- // 只有本人登记过才给「清空登记」;当前是档案回落地址时没有本人登记可清。
- onClear: hasOwnEmail ? () => void saveEmail("") : undefined,
- onDraftChange: (next) => { setEmailDraft(next); setEmailError(""); },
- onStart: () => { setEmailDraft(""); setEmailError(""); setNotice(""); setActionError(""); setEditingEmail(true); },
- onCancel: () => { if (emailPending) return; setEditingEmail(false); setEmailError(""); },
- onSave: () => {
- const value = emailDraft.trim();
- if (!value) return setEmailError("请输入邮箱地址;如需清除本人登记,请点「清空登记」");
- void saveEmail(value);
- },
- }}
- addressHint={mail?.address_hint ?? null}
- available={mail?.available ?? false}
- description={
- mail?.available
- ? usingFallback
- ? "通知发往主体档案邮箱;点地址右侧图标可登记本人投递邮箱覆盖它。"
- : "通知发往本人登记的投递邮箱;可随时关闭或点地址右侧图标更换地址。"
- : "尚未登记通知投递邮箱;点地址右侧图标登记后才能开启邮箱投递。"
- }
- emptyAddressText="未登记(点右侧图标登记)"
- enabled={mail?.enabled ?? false}
- icon={<Mail aria-hidden="true" size={17} />}
- onToggle={(next) => void saveToggles({ mail: next, sms: sms?.enabled ?? false })}
- pending={pending}
- title="邮箱"
- />
- <ChannelRow
- addressHint={sms?.address_hint ?? null}
- available={sms?.available ?? false}
- description={
- sms?.available
- ? "通知以短信发往绑定手机号;换绑手机号请联系运营代办。"
- : "当前账号未绑定手机号,不能开启短信投递。"
- }
- enabled={sms?.enabled ?? false}
- icon={<Smartphone aria-hidden="true" size={17} />}
- onToggle={(next) => void saveToggles({ mail: mail?.enabled ?? true, sms: next })}
- pending={pending}
- title="手机短信"
- />
- </>
- )}
- {/* 卡内反馈区:⛔ 再单开「通知投递邮箱」卡(业主 2026-11-14 定案,地址已并入上面的行内编辑)。 */}
- {(actionError || notice) && (
- <div className="border-t border-border px-6 py-3">
- {actionError && <p className="text-sm text-destructive" role="alert">{actionError}</p>}
- {notice && !actionError && <p className="text-sm text-primary" role="status">{notice}</p>}
- </div>
- )}
- </section>
- </>
- );
- }
|