|
|
@@ -1,4 +1,4 @@
|
|
|
-import { useEffect, useMemo, useRef, useState } from "react";
|
|
|
+import { useEffect, useMemo, useRef, useState, type KeyboardEvent } from "react";
|
|
|
import { ArrowLeft, Plus, Trash2, Upload, X } from "lucide-react";
|
|
|
import { FetchError } from "../components/fetchError";
|
|
|
import { useNavigate, useParams } from "react-router";
|
|
|
@@ -7,7 +7,7 @@ import { Input } from "../components/ui/input";
|
|
|
import { TreeSelect } from "../components/ui/tree-select";
|
|
|
import { CustomerTeamSelect } from "../components/CustomerTeamSelect";
|
|
|
import { contractRawByParam } from "../lib/contract-demo";
|
|
|
-import { contractCreate, contractCustomerLookup, contractUpdate, rowsOf } from "../lib/api/bos";
|
|
|
+import { contractAttachmentDiscard, contractCreate, contractCustomerLookup, contractUpdate, rowsOf } from "../lib/api/bos";
|
|
|
import { ApiError } from "../lib/api/http";
|
|
|
import { OPTIONS, label, PAY_CYCLE, SUBJECT_TYPE, USE_CATEGORY } from "../lib/api/labels";
|
|
|
import { WORD_UNCONFIGURED } from "../lib/api/lockedCopy";
|
|
|
@@ -20,43 +20,33 @@ import { regionTreeNodes } from "../lib/region-tree";
|
|
|
import { useOrganizationData } from "../lib/organization-demo";
|
|
|
import type { ContractCreateResult } from "../lib/api/types";
|
|
|
import { uploadContractAttachment, uploadCustomerLicense, type UploadedObject } from "../lib/api/oss-upload";
|
|
|
+import { contractAttachmentLabel } from "../lib/contract-attachments";
|
|
|
import { useLoad } from "../lib/useLoad";
|
|
|
import { useDemoIdentity } from "../lib/demo-session";
|
|
|
import "../styles/operations.css";
|
|
|
|
|
|
-/** 客户候选:`contract/customer_lookup` 三种命中方式,未命中转自由录入(⛔ 红叉、⛔ 11102)。 */
|
|
|
+/**
|
|
|
+ * 客户候选:`contract/customer_lookup` 三种命中方式,未命中转自由录入(⛔ 红叉、⛔ 11102)。
|
|
|
+ *
|
|
|
+ * **输入防抖**:连击期间只重置计时器,`CUSTOMER_LOOKUP_DEBOUNCE_MS` 内不再发第二次请求;
|
|
|
+ * 检索词去空白后才作为依赖 ⇒ 尾部空格/前后空白变动不触发重取。
|
|
|
+ */
|
|
|
+const CUSTOMER_LOOKUP_DEBOUNCE_MS = 300;
|
|
|
function useCustomerCandidates(term: string) {
|
|
|
const [debounced, setDebounced] = useState(term);
|
|
|
useEffect(() => {
|
|
|
- const timer = window.setTimeout(() => setDebounced(term), 300);
|
|
|
+ const timer = window.setTimeout(() => setDebounced(term), CUSTOMER_LOOKUP_DEBOUNCE_MS);
|
|
|
return () => window.clearTimeout(timer);
|
|
|
}, [term]);
|
|
|
/** 服务端只认 `query_type` + `value` 两键(`identity` 走 HMAC 密钥通路、dev 未接线会回 `KLT-S406`)⇒ 主体号走 `credit_code`,其余走名称模糊。 */
|
|
|
- return useLoad(() => {
|
|
|
- const termText = debounced.trim();
|
|
|
- if (termText.length < 2) return Promise.resolve(null);
|
|
|
- const queryType = /^KL-\d{4}-\d{6}$/.test(termText) ? "credit_code" : "keyword";
|
|
|
- return contractCustomerLookup({ query_type: queryType, value: termText });
|
|
|
- }, [debounced]);
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * 合同附件的展示名(后端只存单值 `attachment_url`,⛔ 另存文件名):
|
|
|
- * 外部链接取末段;对象键末段是随机串 ⇒ 按后缀显示「合同附件.<ext>」,⛔ 把随机串糊给用户。
|
|
|
- */
|
|
|
-export function contractAttachmentLabel(raw: string): string {
|
|
|
- const value = raw.trim();
|
|
|
- if (!value) return "";
|
|
|
- const base = value.split("/").pop() ?? value;
|
|
|
- if (/^https?:\/\//i.test(value)) {
|
|
|
- try {
|
|
|
- return decodeURIComponent(base) || value;
|
|
|
- } catch {
|
|
|
- return base || value;
|
|
|
- }
|
|
|
- }
|
|
|
- const dot = base.lastIndexOf(".");
|
|
|
- return dot > 0 ? `合同附件${base.slice(dot).toLowerCase()}` : "合同附件";
|
|
|
+ const query = debounced.trim();
|
|
|
+ const load = useLoad(() => {
|
|
|
+ if (query.length < 2) return Promise.resolve(null);
|
|
|
+ const queryType = /^KL-\d{4}-\d{6}$/.test(query) ? "credit_code" : "keyword";
|
|
|
+ return contractCustomerLookup({ query_type: queryType, value: query });
|
|
|
+ }, [query]);
|
|
|
+ /** 回带结果对应的检索词:调用方据此判断「候选还是上一次的」(⛔ 据此清空候选,见页面内注释)。 */
|
|
|
+ return { ...load, term: query };
|
|
|
}
|
|
|
|
|
|
export function ContractEditorPage() {
|
|
|
@@ -102,9 +92,33 @@ export function ContractEditorPage() {
|
|
|
const [attachmentError, setAttachmentError] = useState("");
|
|
|
/** 本次刚上传的原始文件名(键里不含文件名 ⇒ 刷新后按后缀显示「合同附件.<ext>」)。 */
|
|
|
const [attachmentName, setAttachmentName] = useState("");
|
|
|
+ /**
|
|
|
+ * 本页**刚上传、还没被任何合同引用**的对象键(孤儿回收清单)。
|
|
|
+ *
|
|
|
+ * 平台只有「即时删除」一条通道(`object/delete@v1`,见 unified-oss §6.6.10:不做后台遍历删对象),
|
|
|
+ * 所以「换了/移除了/取消了」的那一刻必须由本页把没人要的那一份退回去([`discardPendingAttachments`])。
|
|
|
+ * 服务端还会再查一次引用面(仍被合同引用就拒删)⇒ 这里可以放宽心调,⛔ 当成前端独占真相。
|
|
|
+ */
|
|
|
+ const pendingAttachmentKeys = useRef<string[]>([]);
|
|
|
useEffect(() => {
|
|
|
if (record.data) setDraft(draftFromRow(record.data));
|
|
|
}, [record.data]);
|
|
|
+ /**
|
|
|
+ * 把清单里的对象键交给服务端回收(best-effort):失败⛔ 拦用户、⛔ 回滚草稿——
|
|
|
+ * 用户的动作(换文件/移除/取消)已经完成,回收只是桶侧的清尾。
|
|
|
+ */
|
|
|
+ const discardPendingAttachments = (keys?: string[]) => {
|
|
|
+ const targets = keys ?? pendingAttachmentKeys.current;
|
|
|
+ if (!targets.length) return;
|
|
|
+ pendingAttachmentKeys.current = pendingAttachmentKeys.current.filter(
|
|
|
+ (key) => !targets.includes(key),
|
|
|
+ );
|
|
|
+ for (const objectKey of targets) {
|
|
|
+ void contractAttachmentDiscard({ object_key: objectKey }).catch(() => {});
|
|
|
+ }
|
|
|
+ };
|
|
|
+ /** 离开本页(返回/取消)时把没落库的上传一起退掉:`useRef` 读的是最新清单,⛔ 闭包快照。 */
|
|
|
+ useEffect(() => () => discardPendingAttachments(), []);
|
|
|
/**
|
|
|
* 新建态的「运营商经办人」姓名默认取当前登录身份(与姓名同一来源、可改,⛔ 写死号码);
|
|
|
* `auth/context@v1` 实测只回 7 个键、不含手机号 ⇒ 取不到就留空由必填校验拦下,⛔ 造号。
|
|
|
@@ -115,14 +129,67 @@ export function ContractEditorPage() {
|
|
|
if (editing || !identity) return;
|
|
|
setDraft((current) => ({ ...current, agent: current.agent || identity.name, agentPhone: current.agentPhone || identity.phone || "" }));
|
|
|
}, [editing, identity]);
|
|
|
+ /**
|
|
|
+ * 客户检索=**输入防抖**(300ms 内的连击只打一次 `contract/customer_lookup@v1`)
|
|
|
+ * + **沿用上一次结果**:防抖窗口里⛔ 把候选清空——否则每敲一个字下拉就消失再出现,列表整体抖一下。
|
|
|
+ */
|
|
|
const lookup = useCustomerCandidates(customerTerm);
|
|
|
+ const trimmedTerm = customerTerm.trim();
|
|
|
+ /** 输入已变、检索还没跟上(防抖计时中或请求在飞):候选沿用旧结果,只在列表底部标「正在检索…」。 */
|
|
|
+ const lookupStale = lookup.term !== trimmedTerm;
|
|
|
+ const searching = trimmedTerm.length >= 2 && (lookupStale || lookup.loading);
|
|
|
const candidates = lookup.data?.customers ?? [];
|
|
|
- const freeText = !lookup.data?.matched && customerTerm.trim().length >= 2;
|
|
|
+ const freeText = !lookup.data?.matched && trimmedTerm.length >= 2;
|
|
|
const customerLabel = record.data?.customer_name || (record.data?.customer_id ? record.data.customer_id : WORD_UNCONFIGURED);
|
|
|
+ /**
|
|
|
+ * 客户检索=**输入框内下拉**(业主 2026-11-16 定案):命中项直接挂在输入框下方的候选列表,
|
|
|
+ * ⛔ 另起一个「命中的客户档案」下拉。选中后输入框锁定该客户并显示 × 清除,再输入即重新检索。
|
|
|
+ */
|
|
|
+ const customerInput = useRef<HTMLInputElement | null>(null);
|
|
|
+ const [customerOpen, setCustomerOpen] = useState(false);
|
|
|
+ const [customerActive, setCustomerActive] = useState(0);
|
|
|
+ const showCustomerOptions = customerOpen && !customerId && trimmedTerm.length >= 2 && (candidates.length > 0 || searching);
|
|
|
+ const activeCustomer = showCustomerOptions && candidates.length ? (customerActive < candidates.length ? customerActive : 0) : -1;
|
|
|
+ const pickCustomer = (hit: (typeof candidates)[number]) => {
|
|
|
+ setCustomerId(hit.customer_id);
|
|
|
+ setCustomerTerm(hit.customer_name);
|
|
|
+ setCustomerOpen(false);
|
|
|
+ setCustomerActive(0);
|
|
|
+ setError("");
|
|
|
+ };
|
|
|
+ const clearCustomer = () => {
|
|
|
+ setCustomerId(null);
|
|
|
+ setCustomerTerm("");
|
|
|
+ setCustomerOpen(false);
|
|
|
+ setCustomerActive(0);
|
|
|
+ setError("");
|
|
|
+ customerInput.current?.focus();
|
|
|
+ };
|
|
|
+ const onCustomerKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {
|
|
|
+ if (customerId) {
|
|
|
+ if (event.key === "Escape") { event.preventDefault(); clearCustomer(); }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (event.key === "ArrowDown" || event.key === "ArrowUp") {
|
|
|
+ if (!candidates.length) return;
|
|
|
+ event.preventDefault();
|
|
|
+ if (!customerOpen) { setCustomerOpen(true); return; }
|
|
|
+ const delta = event.key === "ArrowDown" ? 1 : -1;
|
|
|
+ setCustomerActive((current) => (current + delta + candidates.length) % candidates.length);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (event.key === "Enter") {
|
|
|
+ if (!showCustomerOptions || activeCustomer < 0) return;
|
|
|
+ event.preventDefault();
|
|
|
+ pickCustomer(candidates[activeCustomer]);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (event.key === "Escape" && customerOpen) { event.preventDefault(); setCustomerOpen(false); }
|
|
|
+ };
|
|
|
|
|
|
/**
|
|
|
* 客户内联建档段(需求 §5.3 CU-13;业主 2026-11-16 定案):
|
|
|
- * **命中已有客户 ⇒ 本段整段不出现**(只显示「命中的客户档案」下拉并锁定);
|
|
|
+ * **命中已有客户 ⇒ 本段整段不出现**(命中项直接选在上方输入框内的候选下拉,选中后输入框锁定并带 × 清除);
|
|
|
* 未命中 ⇒ 在客户名称下方展开建档必填项,含运营商与运维团队两个下拉。
|
|
|
* ⚠️ 本段只收集 CU-13 第 1 步字段,逐项与 `/customer/create@v1` 同源;
|
|
|
* 企业营业执照影像**可留空**(本通道例外),建档后到客户档案「登记材料」格补传。
|
|
|
@@ -381,7 +448,9 @@ export function ContractEditorPage() {
|
|
|
<dt>服务开通状态</dt><dd>未开通(业务开通走工单)</dd>
|
|
|
<dt>账号激活状态</dt><dd>未激活(待激活)</dd>
|
|
|
<dt>激活码</dt><dd>{c.code_issued ? "已发放(明文不在本页展示)" : "本次未发码,见建档回执"}</dd>
|
|
|
- <dt>营业执照影像</dt><dd>{c.license_pending ? "本次留空 ⇒ 请到客户档案「登记材料」格补传" : "已登记"}</dd>
|
|
|
+ <dt>营业执照影像</dt><dd>{c.subject_type && c.subject_type !== "enterprise"
|
|
|
+ ? "不适用(非企业主体无需营业执照)"
|
|
|
+ : c.license_pending ? "本次留空 ⇒ 请到客户档案「登记材料」格补传" : "已登记"}</dd>
|
|
|
</dl>
|
|
|
<p className="ops-form-note">激活码请用客户列表的「导出激活码」经平台凭证通道取;⛔ 本页不留存明文。</p>
|
|
|
<div className="ops-form-actions">
|
|
|
@@ -416,24 +485,54 @@ export function ContractEditorPage() {
|
|
|
</div>
|
|
|
) : (
|
|
|
<div className="ops-form-grid ops-form-grid--business">
|
|
|
- <label htmlFor="contract-customer" className="ops-form-span-2">
|
|
|
- <span className="ops-field-title">客户名称或信用码 <b title="必填">*</b></span>
|
|
|
- <Input id="contract-customer" placeholder="客户名称至少 2 个字,或输入统一社会信用代码" value={customerTerm} disabled={Boolean(customerId)} onChange={(event) => { setCustomerTerm(event.target.value); setCustomerId(null); setError(""); }} />
|
|
|
- </label>
|
|
|
- {!!candidates.length && (
|
|
|
- <label htmlFor="contract-customer-pick" className="ops-form-span-2">
|
|
|
- <span className="ops-field-title">命中的客户档案</span>
|
|
|
- <select id="contract-customer-pick" value={customerId ?? ""} onChange={(event) => { const hit = candidates.find((item) => item.customer_id === event.target.value); setCustomerId(hit?.customer_id ?? null); if (hit) setCustomerTerm(hit.customer_name); setError(""); }}>
|
|
|
- <option value="">请选择</option>
|
|
|
- {candidates.map((item) => <option key={item.customer_id} value={item.customer_id}>{item.customer_name}{item.identity_no_masked ? `(${item.identity_no_masked})` : ""}</option>)}
|
|
|
- </select>
|
|
|
- </label>
|
|
|
- )}
|
|
|
+ {/* 客户检索=输入框内下拉(业主 2026-11-16 定案):命中项直接挂在输入框下方,⛔ 另起「命中的客户档案」下拉框。 */}
|
|
|
+ <div className="contract-customer-combobox ops-form-span-2" onBlur={(event) => { if (!event.currentTarget.contains(event.relatedTarget as Node | null)) setCustomerOpen(false); }}>
|
|
|
+ <label htmlFor="contract-customer" className="ops-field-title">客户名称或信用码 <b title="必填">*</b></label>
|
|
|
+ <div className="contract-customer-combobox-control">
|
|
|
+ <Input
|
|
|
+ ref={customerInput}
|
|
|
+ id="contract-customer"
|
|
|
+ role="combobox"
|
|
|
+ autoComplete="off"
|
|
|
+ aria-expanded={showCustomerOptions}
|
|
|
+ aria-controls="contract-customer-options"
|
|
|
+ aria-autocomplete="list"
|
|
|
+ aria-activedescendant={showCustomerOptions && activeCustomer >= 0 ? `contract-customer-option-${activeCustomer}` : undefined}
|
|
|
+ placeholder="客户名称至少 2 个字,或输入统一社会信用代码"
|
|
|
+ value={customerTerm}
|
|
|
+ readOnly={Boolean(customerId)}
|
|
|
+ onFocus={() => { if (!customerId) setCustomerOpen(true); }}
|
|
|
+ onKeyDown={onCustomerKeyDown}
|
|
|
+ onChange={(event) => { setCustomerTerm(event.target.value); setCustomerId(null); setCustomerOpen(true); setCustomerActive(0); setError(""); }}
|
|
|
+ />
|
|
|
+ {customerId && <button type="button" className="contract-customer-combobox-clear" aria-label="清除已选客户" title="清除已选客户" onClick={clearCustomer}><X size={14} /></button>}
|
|
|
+ {showCustomerOptions && (
|
|
|
+ <ul id="contract-customer-options" role="listbox" aria-label="命中的客户档案" className="contract-customer-combobox-options">
|
|
|
+ {candidates.map((item, index) => <li
|
|
|
+ key={item.customer_id}
|
|
|
+ id={`contract-customer-option-${index}`}
|
|
|
+ role="option"
|
|
|
+ aria-selected={index === activeCustomer}
|
|
|
+ className={index === activeCustomer ? "is-active" : undefined}
|
|
|
+ onMouseDown={(event) => event.preventDefault()}
|
|
|
+ onMouseEnter={() => setCustomerActive(index)}
|
|
|
+ onClick={() => pickCustomer(item)}
|
|
|
+ >
|
|
|
+ <strong>{item.customer_name}</strong>
|
|
|
+ {item.identity_no_masked && <span>{item.identity_no_masked}</span>}
|
|
|
+ {item.province_name && <small>{item.province_name}</small>}
|
|
|
+ </li>)}
|
|
|
+ {/* 检索在飞时只加一行状态,⛔ 收起整个候选列表(收起再展开就是「抖动」)。 */}
|
|
|
+ {searching && <li className="contract-customer-combobox-status" role="presentation">正在检索…</li>}
|
|
|
+ </ul>
|
|
|
+ )}
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
</div>
|
|
|
)}
|
|
|
- {lookup.loading && !editing && <p className="contract-form-summary">正在检索签约主体…</p>}
|
|
|
{lookup.error && !editing && <FetchError message={lookup.error.message} onRetry={lookup.reload} />}
|
|
|
- {!editing && !customerId && <p className="contract-form-summary">{CONTRACT_FREE_TEXT_HINT}</p>}
|
|
|
+ {/* 提示行常驻(⛔ 随请求起落显隐):检索状态只在候选列表内以一行「正在检索…」呈现,避免整块内容上下跳。 */}
|
|
|
+ {!editing && !customerId && !lookup.error && <p className="contract-form-summary">{freeText ? CONTRACT_FREE_TEXT_HINT : CONTRACT_CUSTOMER_LOOKUP_HINT}</p>}
|
|
|
{/**
|
|
|
* 未命中 ⇒ 展开客户建档必填段(CU-13 第 1 步;业主 2026-11-16 定案):
|
|
|
* 客户名称沿用上方检索框(保存时按它建档);命中已有客户时本段整段不出现。
|
|
|
@@ -635,3 +734,5 @@ export function ContractEditorPage() {
|
|
|
}
|
|
|
|
|
|
const CONTRACT_FREE_TEXT_HINT = "未命中客户档案:继续填下方建档必填项,保存时先建客户档案(含客户主账号)再签本合同;命中后本段自动收起。";
|
|
|
+/** 检索进行中/尚无候选时的常驻提示(⛔ 用「正在检索」替换本行,那句话改成候选列表里的一行)。 */
|
|
|
+const CONTRACT_CUSTOMER_LOOKUP_HINT = "输入客户名称(至少 2 个字)或统一社会信用代码检索:命中项直接列在输入框下方,选中后可用 × 清除重选;未命中则继续填下方建档必填项。";
|