CustomerEditorPage.tsx 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /**
  2. * 客户建档 / 编辑档案 —— 提交走 klt-bos 写口,⛔ localStorage。
  3. *
  4. * 新建:`POST /customer/create@v1`(必填 customer_name / subject_type / operator_id,运维团队必填 1 支)
  5. *    → 回执若未落运维团队,再 `POST /customer/update@v1` 回填 `ops_team_ids`(单支定案:最多 1 支)。
  6. * 编辑:`POST /customer/update@v1`(资料)+ `POST /customer/attribution_update@v1`(运营商/合伙人)
  7. *    + 区域变更时 `POST /customer/move_region@v1`(必填变更依据)。
  8. * 边界:登记材料(营业执照影像)由浏览器直传平台 `unified-oss`(凭证自取、⛔ 经后端),
  9. * 建档载荷只回报 `{object_key, file_name}`;合同文档区本期仍无路由(保持禁用并说明)。
  10. * 错误一律显示后端/平台 `message` 原文。
  11. */
  12. import { useEffect, useMemo, useRef, useState, type FormEvent, type ReactNode } from "react";
  13. import { ArrowLeft, Upload } from "lucide-react";
  14. import { Link, useLocation, useNavigate, useParams } from "react-router";
  15. import { FetchError } from "../components/fetchError";
  16. import { Button } from "../components/ui/button";
  17. import { Input } from "../components/ui/input";
  18. import { TreeSelect } from "../components/ui/tree-select";
  19. import { FieldHint } from "../components/ui/field-hint";
  20. import { CustomerTeamSelect } from "../components/CustomerTeamSelect";
  21. import { regionTreeNodes } from "../lib/region-tree";
  22. import { lockIfCandidate, pickOwnedId, regionAncestorChain, teamsCoveringRegion } from "../lib/region-ownership";
  23. import { Tri } from "../components/triState";
  24. import { useLoad } from "../lib/useLoad";
  25. import { detailLinkState, formBack } from "../lib/detail-navigation";
  26. import { ApiError } from "../lib/api/http";
  27. import { SUBJECT_TYPE, label } from "../lib/api/labels";
  28. import { typeLabel } from "../lib/api/ticket";
  29. import { customerAttributionUpdate, customerCreate, customerGet, customerMoveRegion, customerUpdate } from "../lib/api/bos";
  30. import { uploadCustomerLicense, type UploadedObject } from "../lib/api/oss-upload";
  31. import { invalidateCustomerList } from "../lib/customer-list-store";
  32. import { useDemoIdentity } from "../lib/demo-session";
  33. import { useOrganizationData } from "../lib/organization-demo";
  34. import "../styles/customer-onboarding.css";
  35. import "../styles/operations.css";
  36. import "../styles/record-editor-fields.css";
  37. const MISSING = "后端未下发";
  38. /** 客户名称上限(与后端 `customer.customer_name`/建档守卫同口径)。 */
  39. const CUSTOMER_NAME_MAX = 64;
  40. /** 联系电话:11 位手机号,或含区号的固话(数字/空格/-/(),7–20 字符)。 */
  41. const PHONE_OK = /^(1\d{10}|[\d\s\-()]{7,20})$/;
  42. /**
  43. * 客户建档允许提交的主体类型:后端 `POST /customer/create@v1` 只收
  44. * `enterprise/individual/sole_proprietor`(实测其余值回 `60004`,原文:
  45. * 「主体类型只能是 enterprise/individual/sole_proprietor」)。
  46. * ⛔ 直接摊开 `SUBJECT_TYPE` 全字典——那是"组织主体"字典,含 `hq/region/ops_team` 等
  47. * 仅用于展示的历史码,选到就会被后端拒。
  48. */
  49. const CUSTOMER_SUBJECT_CODES = ["enterprise", "individual", "sole_proprietor"] as const;
  50. const SUBJECT_OPTIONS = CUSTOMER_SUBJECT_CODES.map((code) => ({ code, name: SUBJECT_TYPE[code] ?? code }));
  51. type Draft = {
  52. name: string; subject: string; idNo: string; contact: string; phone: string; address: string;
  53. regionId: string; operatorId: string; partnerId: string; /** 仅 UI:按区域反查,⛔ 不入写口 */ operationsTeamId: string; teamIds: string[];
  54. login: string; masterPhone: string; basisNote: string;
  55. };
  56. export function CustomerEditorPage() {
  57. const { no } = useParams(), location = useLocation(), navigate = useNavigate();
  58. const identity = useDemoIdentity();
  59. const data = useOrganizationData(["regions", "operators", "partners", "operationsTeams", "maintenanceTeams"]);
  60. /**
  61. * 返回目标=入口页(客户编辑页有「客户清单」与「客户档案」两个入口):
  62. * 清单进 ⇒ 回清单并保留搜索/筛选/分页;档案进 ⇒ 回档案,且把档案自己的返回链一起带回。
  63. * 直接敲 URL(无 `from`)才兜底回客户档案。
  64. */
  65. const back = formBack({ state: location.state, detail: no ? `/customers/${encodeURIComponent(no)}` : "/customers", detailLabel: no ? "返回客户档案" : "返回客户管理", list: "/customers", listLabel: "返回客户管理" });
  66. const form = useRef<HTMLFormElement>(null);
  67. const detail = useLoad(() => (no ? customerGet({ customer_id: no }) : Promise.resolve(null)), [no]);
  68. const original = detail.data?.customer ?? null;
  69. const originalTeams = detail.data?.team_ids ?? [];
  70. const originalRegion = detail.data?.region_mounts?.[0]?.parent_node_id ?? "";
  71. const regionTree = useMemo(() => regionTreeNodes(data.regions, data.regions.map((item) => item.id)), [data]);
  72. const [manualLogin, setManualLogin] = useState(false);
  73. const [draft, setDraft] = useState<Draft>({ name: "", subject: SUBJECT_OPTIONS[0]?.code ?? "", idNo: "", contact: "", phone: "", address: "", regionId: "", operatorId: "", partnerId: "", operationsTeamId: "", teamIds: [], login: "", masterPhone: "", basisNote: "" });
  74. const [seeded, setSeeded] = useState(false);
  75. if (original && !seeded) {
  76. setSeeded(true);
  77. setDraft((current) => ({ ...current, name: original.customer_name ?? "", subject: original.subject_type ?? current.subject, idNo: original.identity_no_masked ?? "", regionId: originalRegion, operatorId: original.operator_id ?? "", partnerId: original.partner_id ?? "", teamIds: originalTeams, contact: original.contact_name ?? "", phone: original.contact_mobile ?? "", address: original.address ?? "" }));
  78. }
  79. /** 运营团队=按挂载区域实时反查(⛔ 档案快照);身份命中候选时只读带入。 */
  80. const operationsTeamCandidates = useMemo(() => {
  81. if (!draft.regionId) return [];
  82. return teamsCoveringRegion(data.operationsTeams, regionAncestorChain(data.regions, draft.regionId));
  83. }, [data.operationsTeams, data.regions, draft.regionId]);
  84. const lockedOperationsTeamId = lockIfCandidate(identity?.operationsTeamId, operationsTeamCandidates);
  85. useEffect(() => {
  86. setDraft((current) => {
  87. if (!current.regionId) {
  88. return current.operationsTeamId ? { ...current, operationsTeamId: "" } : current;
  89. }
  90. const next = pickOwnedId(current.operationsTeamId, operationsTeamCandidates, lockedOperationsTeamId);
  91. return next === current.operationsTeamId ? current : { ...current, operationsTeamId: next };
  92. });
  93. }, [draft.regionId, operationsTeamCandidates, lockedOperationsTeamId]);
  94. const [errors, setErrors] = useState<Record<string, string>>({}), [saveError, setSaveError] = useState(""), [busy, setBusy] = useState(false);
  95. const [created, setCreated] = useState<{ id: string; userId: string | null; code: string | null } | null>(null);
  96. /** 登记材料:`licenseObj`=已直传成功的对象(建案载荷据此回报);⛔ 存签名 URL/⛔ 存文件本体。 */
  97. const [licenseObj, setLicenseObj] = useState<UploadedObject | null>(null);
  98. const [licenseBusy, setLicenseBusy] = useState(false);
  99. const [licenseError, setLicenseError] = useState("");
  100. function set<K extends keyof Draft>(key: K, value: Draft[K]) { setDraft((current) => ({ ...current, [key]: value })); setErrors((current) => { const next = { ...current }; delete next[key]; return next; }); }
  101. function field(key: keyof Draft, label: string, control: ReactNode, required = false, wide = false, hint?: ReactNode) {
  102. return <label className={`${wide ? "customer-form-wide " : ""}${errors[key] ? "has-error" : ""}`}><span className="ops-field-title">{label}{required && <b>*</b>}{hint && <FieldHint label={`${label}说明`}>{hint}</FieldHint>}</span>{control}{errors[key] && <small className="customer-field-error" id={`error-${key}`}>{errors[key]}</small>}</label>;
  103. }
  104. const input = (key: keyof Draft, placeholder = "") => <Input name={key} value={draft[key]} placeholder={placeholder} aria-invalid={Boolean(errors[key])} aria-describedby={errors[key] ? `error-${key}` : undefined} onChange={(event) => set(key, event.target.value)} />;
  105. const select = (key: keyof Draft, options: { id: string; name: string }[], placeholder: string) => (
  106. <select
  107. name={key}
  108. value={draft[key]}
  109. data-placeholder={!draft[key]}
  110. aria-invalid={Boolean(errors[key])}
  111. aria-describedby={errors[key] ? `error-${key}` : undefined}
  112. onChange={(event) => set(key, event.target.value)}
  113. >
  114. <option value="">{placeholder}</option>
  115. {options.map((item) => <option key={item.id} value={item.id}>{item.name}</option>)}
  116. </select>
  117. );
  118. // 编辑态识别号=后端下发的脱敏值;与当前输入不同才算「改过」⇒ 才作为 id_code 提交
  119. // (⛔ 把 `91AB****XYZ0` 这样的脱敏串原样提交,后端归一化会直接拒)。
  120. const seedMasked = (original?.identity_no_masked ?? "").replace(/[\s-]/g, "").toUpperCase();
  121. const typedIdNo = draft.idNo.trim().replace(/[\s-]/g, "").toUpperCase();
  122. const identityPatch = typedIdNo && typedIdNo !== seedMasked ? typedIdNo : undefined;
  123. // 运维团队=单支定案:`ops_team_ids` 最多 1 个元素(后端「本次只受理 1 支」守卫)。
  124. // 没改动 ⇒ ⛔ 随单提交:未改动时提交=把一个现状当成"要写入的归属",语义不对(不提交=不改动);
  125. // 挂了 2 支的历史脏数据一并提交还会被后端拒,导致改任何字段都整体保存失败。
  126. const teamsKey = (ids: string[]) => [...ids].sort().join("|");
  127. const teamsTouched = teamsKey(draft.teamIds) !== teamsKey(originalTeams);
  128. const teamsPatch = teamsTouched ? draft.teamIds : undefined;
  129. const teamName = (id: string) => data.maintenanceTeams.find((item) => item.id === id)?.name ?? id;
  130. // 后端当期回了 2 支以上=历史脏数据 ⇒ 按实呈现并提示改派,⛔ 前端截断成一支假装正常。
  131. const staleTeamIds = originalTeams.length > 1 ? originalTeams : [];
  132. function validate(): Record<string, string> {
  133. const next: Record<string, string> = {};
  134. const name = draft.name.trim();
  135. if (!name) next.name = "请填写客户名称";
  136. else if ([...name].length > CUSTOMER_NAME_MAX) next.name = `客户名称最多 ${CUSTOMER_NAME_MAX} 字`;
  137. if (!draft.subject) next.subject = "请选择主体类型";
  138. if (!draft.regionId) next.regionId = "请选择挂载区域";
  139. if (!draft.operatorId) next.operatorId = "请选择运营商";
  140. if (!no && !draft.idNo.trim()) next.idNo = "请填写客户识别号";
  141. // 运维团队单支定案:建档必须恰好 1 支;编辑态被改动过 ⇒ 同样必须恰好 1 支(⛔ 清空成 0 支)。
  142. // 原本就挂着多支的历史脏数据且未改派 ⇒ 只提示不拦(本次不提交运维团队,其它字段照常可存)。
  143. if (draft.teamIds.length !== 1 && (teamsTouched || !no)) next.teamIds = "请选择 1 支运维团队";
  144. const phone = draft.phone.trim();
  145. if (phone && !PHONE_OK.test(phone)) next.phone = "联系电话格式不符:应为 11 位手机号或含区号的固话";
  146. const masterPhone = draft.masterPhone.trim();
  147. if (masterPhone && !/^\d{11}$/.test(masterPhone)) next.masterPhone = "主账号绑定手机号须为 11 位纯数字";
  148. // 登记材料(客户§2.1):营业执照影像**选填**(业主 2026-11-19 定案,⛔ 强制登记);
  149. // 个人/个体工商户不适用、⛔ 拦(控件也不渲染)。
  150. // 变更依据**选填**(业主 2026-11-12 裁定);改了识别号才校验其格式。
  151. if (identityPatch && !/^[A-Z0-9]{18}$|^\d{15}$/.test(identityPatch)) {
  152. next.idNo = "识别号格式不符:应为 18 位统一社会信用代码/组织代码,或 15 位旧营业执照号";
  153. }
  154. return next;
  155. }
  156. /**
  157. * 营业执照影像:选中即直传(两段式:平台取凭证 → 直传桶),成功后才把对象键挂进建案载荷。
  158. * ⛔ 存文件本体进 state 后等提交再传:一次建档要等两段网络,且失败点被推迟到"保存"按钮上,
  159. * 用户会以为是建档失败。
  160. */
  161. async function pickLicense(file: File | null) {
  162. if (!file) return;
  163. setLicenseError("");
  164. setLicenseBusy(true);
  165. try {
  166. setLicenseObj(await uploadCustomerLicense(file));
  167. setErrors((current) => {
  168. const next = { ...current };
  169. delete next.license;
  170. return next;
  171. });
  172. } catch (e) {
  173. // 上传失败=本次没有可信对象键 ⇒ 清掉旧值,⛔ 让"上一次成功"的文件冒充本次选择。
  174. setLicenseObj(null);
  175. setLicenseError((e as Error)?.message || "影像上传失败。");
  176. } finally {
  177. setLicenseBusy(false);
  178. }
  179. }
  180. async function save(event: FormEvent) { event.preventDefault();
  181. if (busy) return;
  182. const next = validate();
  183. setErrors(next);
  184. setSaveError("");
  185. if (Object.keys(next).length) { window.setTimeout(() => form.current?.querySelector<HTMLElement>('[aria-invalid="true"]')?.focus(), 0); return; }
  186. setBusy(true);
  187. try {
  188. if (!no) {
  189. const res = await customerCreate({
  190. customer_name: draft.name.trim(),
  191. subject_type: draft.subject,
  192. operator_id: draft.operatorId,
  193. parent_node_id: draft.regionId || undefined,
  194. id_code: draft.idNo.trim() || undefined,
  195. contact_name: draft.contact.trim() || undefined,
  196. contact_mobile: draft.phone.trim() || undefined,
  197. address: draft.address.trim() || undefined,
  198. partner_id: draft.partnerId || undefined,
  199. ops_team_ids: draft.teamIds.length === 1 ? draft.teamIds : undefined,
  200. license: licenseObj ?? undefined,
  201. primary_username: manualLogin ? draft.login.trim() || undefined : undefined,
  202. primary_mobile: draft.masterPhone.trim() || undefined,
  203. });
  204. const customerId = res?.customer_id;
  205. if (!customerId) throw new ApiError(null, `${MISSING}:/customer/create@v1 未回 customer_id`);
  206. // 运维团队随建档同批落库(2026-11-15 起 `customer/create@v1` 在原子批里写
  207. // `customer_team_belong`)⇒ 不再补发一次 update 回填,避免同一次建档产生两条归属留痕。
  208. invalidateCustomerList();
  209. setCreated({ id: customerId, userId: res.user_id ?? null, code: res.activation_code ?? null });
  210. return;
  211. }
  212. const rowVersion = original?.row_version;
  213. if (typeof rowVersion !== "number" || !Number.isSafeInteger(rowVersion) || rowVersion < 1) {
  214. throw new ApiError(null, "未取得客户档案版本,请刷新后重试。");
  215. }
  216. await customerUpdate({
  217. customer_id: no,
  218. customer_name: draft.name.trim(),
  219. subject_type: draft.subject,
  220. id_code: identityPatch,
  221. change_basis: draft.basisNote.trim() || undefined,
  222. contact_name: draft.contact.trim() || undefined,
  223. contact_mobile: draft.phone.trim() || undefined,
  224. address: draft.address.trim() || undefined,
  225. ops_team_ids: teamsPatch,
  226. row_version: rowVersion,
  227. });
  228. if (draft.operatorId !== (original?.operator_id ?? "") || draft.partnerId !== (original?.partner_id ?? "")) {
  229. await customerAttributionUpdate({ customer_id: no, operator_id: draft.operatorId || undefined, partner_id: draft.partnerId || undefined });
  230. }
  231. if (draft.regionId && draft.regionId !== originalRegion) {
  232. await customerMoveRegion({ customer_id: no, new_region_node_id: draft.regionId, ops_team_ids: teamsPatch });
  233. }
  234. invalidateCustomerList();
  235. // 保存后回入口页:清单进回清单(筛选还在),档案进回档案。
  236. navigate(back.to, { state: back.state });
  237. } catch (e) {
  238. setSaveError((e as ApiError)?.message || "保存失败。");
  239. } finally {
  240. setBusy(false);
  241. }
  242. }
  243. if (no && detail.error) return <section className="ops-page"><Link to={back.to} state={back.state} className="ops-back-link"><ArrowLeft size={16} />{back.label}</Link><FetchError message={detail.error.message} onRetry={detail.reload} /></section>;
  244. if (created) return <section className="ops-page ops-record-page"><div className="ops-record-card customer-created-result"><h1>客户已建档</h1><p>以下为 <code>POST /customer/create@v1</code> 的回执内容,仅本次展示。</p><dl><dt>客户号</dt><dd>{created.id}</dd><dt>主账号 user_id</dt><dd>{created.userId ?? <Tri tone="missing" text={MISSING} title={`${MISSING}:create 回执未带 user_id`} />}</dd><dt>激活码</dt><dd className="customer-created-secret">{created.code ?? <Tri tone="missing" text={MISSING} title={`${MISSING}:create 回执未带 activation_code`} />}</dd></dl><Button onClick={() => navigate(`/customers/${encodeURIComponent(created.id)}`, { replace: true, state: detailLinkState(back.to, back.state) })}>查看客户档案</Button></div></section>;
  245. const fieldGrid = no ? "ops-form-grid" : "ops-form-grid ops-form-grid--business";
  246. const formError = saveError || (Object.keys(errors).length > 0
  247. ? `有 ${Object.keys(errors).length} 项未通过校验,请修正标记的字段。`
  248. : "");
  249. const submitLabel = busy ? "正在保存…" : no ? "保存档案" : "新建客户";
  250. const formActions = (
  251. <div className="ops-form-actions">
  252. <Button type="button" variant="outline" disabled={busy} onClick={() => navigate(back.to, { state: back.state })}>取消</Button>
  253. <Button type="submit" disabled={busy}>{submitLabel}</Button>
  254. </div>
  255. );
  256. const sections = (
  257. <>
  258. <section className="ops-record-card">
  259. <div className="customer-form-section-title"><span>01</span><div><h2>主体资料</h2><p>客户允许重名,以识别号查重、以客户号关联业务。</p></div></div>
  260. <div className={fieldGrid}>
  261. {no && (
  262. <label>
  263. <span className="ops-field-title">客户号</span>
  264. <Input disabled value={no} />
  265. </label>
  266. )}
  267. {field("name", "客户名称", <Input name="name" value={draft.name} maxLength={CUSTOMER_NAME_MAX} placeholder="请输入营业执照名称或个人姓名" aria-invalid={Boolean(errors.name)} aria-describedby={errors.name ? "error-name" : undefined} onChange={(event) => set("name", event.target.value)} />, true, false, `最多 ${CUSTOMER_NAME_MAX} 字`)}
  268. {field("subject", "主体类型", <select value={draft.subject} onChange={(event) => set("subject", event.target.value)}>{SUBJECT_OPTIONS.map((item) => <option key={item.code} value={item.code}>{item.name}</option>)}</select>)}
  269. {no && original?.subject_type === "enterprise" && draft.subject !== "enterprise" && <div className="customer-form-wide"><small className="customer-form-note">保存后主体类型改为「{SUBJECT_OPTIONS.find((item) => item.code === draft.subject)?.name ?? draft.subject}」,系统将<b>同时撤销</b>该客户已留存的营业执照影像(非企业不采集证件影像);本次撤销写入客户档案变更留痕,之后如需恢复只能按企业主体重新上传。</small></div>}
  270. {field("idNo", "客户识别号", input("idNo", "统一社会信用代码 / 身份证号"), !no, false,
  271. no ? (original?.identity_no_masked
  272. ? "当前为后端脱敏识别号(明文不出后端);更正请清空后填入完整号码,不改动=不提交。"
  273. : "该客户尚未登记识别号;填入完整号码即视为登记(后端查重+加密落库)。")
  274. : "统一社会信用代码 / 身份证号;是查重键,建案时按它判重。")}
  275. {no && field("basisNote", "变更依据(选填)", input("basisNote", "例如:客户补充证件,原识别号录入有误"), false, true)}
  276. {field("contact", "联系人", input("contact", "客户方联系人,个人客户填本人"))}
  277. {field("phone", "联系电话", <Input name="phone" value={draft.phone} maxLength={20} placeholder="11 位手机号或含区号固话" aria-invalid={Boolean(errors.phone)} aria-describedby={errors.phone ? "error-phone" : undefined} onChange={(event) => set("phone", event.target.value)} />, false, false, "11 位手机号,或含区号的固话(数字/空格/-/括号)")}
  278. {field("address", "客户地址", input("address", "请输入完整地址,包括楼栋与房间号"), false, Boolean(no))}
  279. </div>
  280. </section>
  281. <section className="ops-record-card">
  282. <div className="customer-form-section-title"><span>02</span><div><h2>归属与挂载</h2><p>选择所属区域、运营商和服务团队。</p></div></div>
  283. <div className={fieldGrid}>
  284. {field("regionId", "挂载区域", <TreeSelect treeData={regionTree} value={draft.regionId} onChange={(value) => set("regionId", value)} ariaLabel="挂载区域" placeholder="请选择区域节点" name="regionId" required invalid={Boolean(errors.regionId)} describedBy={errors.regionId ? "error-regionId" : undefined} />, true)}
  285. {field("operatorId", "运营商", select("operatorId", data.operators.map((item) => ({ id: item.id, name: item.name })), "请选择运营商"), true)}
  286. {field("partnerId", "合伙人", select("partnerId", data.partners.map((item) => ({ id: item.id, name: `${item.name} · ${item.title}` })), "不绑定"))}
  287. {field("operationsTeamId", "运营团队",
  288. <select name="operationsTeamId" disabled={!draft.regionId || Boolean(lockedOperationsTeamId)} value={draft.operationsTeamId} data-placeholder={!draft.operationsTeamId} aria-invalid={Boolean(errors.operationsTeamId)} aria-describedby={errors.operationsTeamId ? "error-operationsTeamId" : undefined} onChange={(event) => set("operationsTeamId", event.target.value)}>
  289. <option value="">{!draft.regionId ? "请先选取所属区域" : operationsTeamCandidates.length ? "请选择运营团队" : "所挂区域未匹配运营团队"}</option>
  290. {operationsTeamCandidates.map((item) => <option key={item.id} value={item.id}>{item.name}</option>)}
  291. </select>,
  292. false, false,
  293. "按所挂区域实时反查,仅用于展示、不写入客户档案;总运营可选手选,其它运营按账号带入只读;唯一候选自动选中。")}
  294. <div className="customer-team-field"><span className="ops-field-title">运维团队{!no && <b>*</b>}</span><CustomerTeamSelect options={data.maintenanceTeams.map((item) => ({ id: item.id, name: item.name, hint: (item.ticketTypes ?? []).map(typeLabel).join("、") }))} value={draft.teamIds[0] ?? ""} onChange={(id) => set("teamIds", [id])} />{errors.teamIds && <small className="customer-field-error">{errors.teamIds}</small>}{staleTeamIds.length > 1 && <small className="customer-form-note">后端当期回显该客户挂了 {staleTeamIds.length} 支运维团队({staleTeamIds.map(teamName).join("、")})——历史脏数据,需改派为单支;改派前本次不提交运维团队。</small>}</div>
  295. </div>
  296. </section>
  297. <section className="ops-record-card">
  298. <div className="customer-form-section-title">
  299. <span>03</span>
  300. <div><h2>客户主账号</h2><p>一个客户一个主账号,不占套餐席位。</p></div>
  301. </div>
  302. {no ? (
  303. <p className="customer-form-note">主账号登录名与激活状态:{MISSING}(<code>/account/*</code> 无读接口);激活码重置请到客户档案办理。</p>
  304. ) : (
  305. <div className={fieldGrid}>
  306. {field("login", "主账号登录名", (
  307. <Input name="login" value={draft.login} placeholder="留空将自动生成" autoComplete="off"
  308. onChange={(event) => { setManualLogin(true); set("login", event.target.value); }} />
  309. ), false, false, "建档成功后,回执中的激活码仅展示一次。")}
  310. {field("masterPhone", "主账号绑定手机号", (
  311. <Input name="masterPhone" value={draft.masterPhone} inputMode="numeric" maxLength={11}
  312. autoComplete="off" placeholder="11 位手机号" onChange={(event) => set("masterPhone", event.target.value)} />
  313. ), false, false, "用于主账号登录验证与找回。")}
  314. </div>
  315. )}
  316. </section>
  317. {!no && (
  318. <section className="ops-record-card">
  319. <div className="customer-form-section-title">
  320. <span>04</span>
  321. <div><h2>登记材料与合同文档</h2><p>企业客户可在建档时上传营业执照影像,也可稍后在客户档案补传。</p></div>
  322. </div>
  323. <div className="customer-document-grid">
  324. {draft.subject === "enterprise" ? (
  325. <div className="customer-document-picker">
  326. <div>
  327. <strong>登记材料 · 营业执照影像</strong>
  328. <label className="customer-upload-button" title="选择后立即直传平台对象存储(字节流不经过本系统)">
  329. <Upload size={15} />{licenseBusy ? "正在上传…" : licenseObj ? "重新选择" : "选择文件"}
  330. <input aria-label="上传登记材料" type="file" disabled={licenseBusy || busy}
  331. onChange={(event) => {
  332. const file = event.target.files?.[0] ?? null;
  333. event.target.value = "";
  334. void pickLicense(file);
  335. }} />
  336. </label>
  337. </div>
  338. <p>{licenseObj ? `已上传:${licenseObj.file_name}` : licenseBusy ? "正在上传影像…" : "尚未选择文件"}</p>
  339. {licenseError && <small className="customer-field-error">{licenseError}</small>}
  340. </div>
  341. ) : (
  342. <div className="customer-document-picker">
  343. <div><strong>登记材料</strong></div>
  344. <p>不适用(个人与个体工商户不采集证件影像)</p>
  345. </div>
  346. )}
  347. <div className="customer-document-picker">
  348. <div>
  349. <strong>合同文档</strong>
  350. <label className="customer-upload-button" title="合同文档随合同登记上传(合同详情 → 附件),建档页不采集">
  351. <Upload size={15} />选择文件<input aria-label="上传合同文档" type="file" multiple disabled />
  352. </label>
  353. </div>
  354. <p>合同文档由合同模块登记,本页不采集</p>
  355. </div>
  356. </div>
  357. </section>
  358. )}
  359. </>
  360. );
  361. return (
  362. <form ref={form} noValidate onSubmit={save} className="ops-page ops-record-page customer-editor record-editor-fields">
  363. {no ? (
  364. <>
  365. <Link to={back.to} state={back.state} className="ops-back-link"><ArrowLeft size={16} />{back.label}</Link>
  366. <header className="ops-record-header">
  367. <div>
  368. <p>客户管理 / 编辑档案</p>
  369. <h1>编辑 {original?.customer_name ?? no}</h1>
  370. <p>识别号可更正,留空或保持原值时不提交变更。</p>
  371. </div>
  372. <Button type="submit" disabled={busy}>{submitLabel}</Button>
  373. </header>
  374. {formError && <div className="ops-form-error" role="alert">{formError}</div>}
  375. {sections}
  376. {formActions}
  377. </>
  378. ) : (
  379. <>
  380. <header className="ops-page-head">
  381. <div>
  382. <p className="ops-eyebrow"><span />客户管理</p>
  383. <h1>新增客户</h1>
  384. <p>登记客户资料和业务归属,并开通客户主账号。</p>
  385. </div>
  386. <Link to={back.to} state={back.state} className="ops-back-link"><ArrowLeft size={16} />{back.label}</Link>
  387. </header>
  388. <div className="ops-record-layout">
  389. <div className="ops-record-stack">{sections}</div>
  390. <aside className="ops-record-side" aria-label="建档操作">
  391. <section className="ops-record-card">
  392. <h2>提交建档</h2>
  393. {formError && <div className="ops-form-error" role="alert">{formError}</div>}
  394. {formActions}
  395. </section>
  396. </aside>
  397. </div>
  398. </>
  399. )}
  400. </form>
  401. );
  402. }
  403. /** 主体类型原文 → 中文(走 `labels.ts` 的 `SUBJECT_TYPE` 字典)。 */
  404. export function subjectTypeLabel(raw: string | null | undefined): string | null {
  405. if (!raw) return null;
  406. const text = label(SUBJECT_TYPE, raw);
  407. return text || null;
  408. }