@harrylabsj/kiwi-merchant-cloud 0.12.33 → 0.12.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/app/cloud/access/open-inquiry-policy.js +1 -1
  2. package/app/cloud/access/open-inquiry-policy.js.map +1 -1
  3. package/app/cloud/access/workbuddy-share-link.d.ts +1 -0
  4. package/app/cloud/access/workbuddy-share-link.js +3 -0
  5. package/app/cloud/access/workbuddy-share-link.js.map +1 -1
  6. package/app/cloud/bootstrap.js +46 -12
  7. package/app/cloud/bootstrap.js.map +1 -1
  8. package/app/cloud/catalog-client.d.ts +1 -1
  9. package/app/cloud/catalog-client.js +1 -1
  10. package/app/cloud/catalog-client.js.map +1 -1
  11. package/app/cloud/connect-service.d.ts +2 -0
  12. package/app/cloud/connect-service.js +84 -23
  13. package/app/cloud/connect-service.js.map +1 -1
  14. package/app/cloud/file-listing-publication.js +1 -1
  15. package/app/cloud/file-listing-publication.js.map +1 -1
  16. package/app/cloud/merchant-connect.js +1 -1
  17. package/app/cloud/onboarding/catalog-pipeline.d.ts +1 -1
  18. package/app/cloud/onboarding/catalog-pipeline.js +1 -1
  19. package/app/cloud/onboarding/steps.js +1 -1
  20. package/app/cloud/readiness.d.ts +5 -1
  21. package/app/cloud/readiness.js +14 -12
  22. package/app/cloud/readiness.js.map +1 -1
  23. package/app/cloud-sample/products.json +3 -3
  24. package/app/http/customer-support-buyer.d.ts +24 -0
  25. package/app/http/customer-support-buyer.js +306 -0
  26. package/app/http/customer-support-buyer.js.map +1 -0
  27. package/app/http/merchant-management/api.d.ts +7 -12
  28. package/app/http/merchant-management/api.js +143 -30
  29. package/app/http/merchant-management/api.js.map +1 -1
  30. package/app/http/merchant-management/page.d.ts +1 -0
  31. package/app/http/merchant-management/page.js +532 -260
  32. package/app/http/merchant-management/page.js.map +1 -1
  33. package/app/mcp/merchant-runtime-assembly.d.ts +3 -0
  34. package/app/mcp/merchant-runtime-assembly.js +8 -0
  35. package/app/mcp/merchant-runtime-assembly.js.map +1 -1
  36. package/app/merchant/ai-runtime/owner-storage-admission.js.map +1 -1
  37. package/app/merchant/weixin-channel.d.ts +157 -1
  38. package/app/merchant/weixin-channel.js +127 -5
  39. package/app/merchant/weixin-channel.js.map +1 -1
  40. package/app/merchant/weixin-owner-agent-profile.d.ts +8 -0
  41. package/app/merchant/weixin-owner-agent-profile.js +29 -0
  42. package/app/merchant/weixin-owner-agent-profile.js.map +1 -0
  43. package/app/merchant/weixin-owner-agent.d.ts +42 -0
  44. package/app/merchant/weixin-owner-agent.js +97 -0
  45. package/app/merchant/weixin-owner-agent.js.map +1 -0
  46. package/app/merchant/weixin-owner-conversation.d.ts +156 -0
  47. package/app/merchant/weixin-owner-conversation.js +559 -0
  48. package/app/merchant/weixin-owner-conversation.js.map +1 -0
  49. package/app/merchant/weixin-owner-digest.d.ts +30 -0
  50. package/app/merchant/weixin-owner-digest.js +30 -0
  51. package/app/merchant/weixin-owner-digest.js.map +1 -0
  52. package/app/merchant/weixin-owner-inventory.d.ts +28 -0
  53. package/app/merchant/weixin-owner-inventory.js +48 -0
  54. package/app/merchant/weixin-owner-inventory.js.map +1 -0
  55. package/app/merchant/weixin-owner-source.d.ts +22 -0
  56. package/app/merchant/weixin-owner-source.js +104 -0
  57. package/app/merchant/weixin-owner-source.js.map +1 -0
  58. package/app/merchant/workbench-server.d.ts +4 -0
  59. package/app/merchant/workbench-server.js +63 -10
  60. package/app/merchant/workbench-server.js.map +1 -1
  61. package/app/merchant-core/operations-jobs/service.d.ts +11 -0
  62. package/app/merchant-core/operations-jobs/service.js +65 -34
  63. package/app/merchant-core/operations-jobs/service.js.map +1 -1
  64. package/app/merchant-core/ops/surface.d.ts +1 -0
  65. package/app/merchant-core/ops/surface.js +9 -0
  66. package/app/merchant-core/ops/surface.js.map +1 -1
  67. package/app/merchant-core/support/manual-service.d.ts +114 -0
  68. package/app/merchant-core/support/manual-service.js +300 -0
  69. package/app/merchant-core/support/manual-service.js.map +1 -0
  70. package/app/merchant-gateway/catalog-publications.js +2 -2
  71. package/app/merchant-gateway/catalog-publications.js.map +1 -1
  72. package/app/merchant-gateway/catalog-tools.d.ts +2 -3
  73. package/app/merchant-gateway/catalog-tools.js +16 -15
  74. package/app/merchant-gateway/catalog-tools.js.map +1 -1
  75. package/app/merchant-gateway/entry-server.js +1 -1
  76. package/app/negotiation/ledger/store.d.ts +2 -0
  77. package/app/negotiation/ledger/store.js +8 -0
  78. package/app/negotiation/ledger/store.js.map +1 -1
  79. package/app/net/safe-http.d.ts +2 -0
  80. package/app/net/safe-http.js +1 -1
  81. package/app/net/safe-http.js.map +1 -1
  82. package/app/weixin/ilink-client.js +50 -19
  83. package/app/weixin/ilink-client.js.map +1 -1
  84. package/app/weixin/types.d.ts +3 -1
  85. package/app/weixin/types.js.map +1 -1
  86. package/build-manifest.json +248 -128
  87. package/deployment-lock.json +85 -77
  88. package/package.json +7 -7
  89. package/runtime-package.json +13 -13
  90. package/shipping-sbom.cdx.json +150 -145
  91. package/shipping-source.json +12 -12
@@ -80,6 +80,7 @@ export function renderMerchantManagementPage(options = {}) {
80
80
  .pill { display: inline-flex; align-items: center; padding: 3px 10px; border-radius: 5px; font-size: 12px; line-height: 1.7; font-weight: 550; background: #edf1ed; color: #53665b; white-space: nowrap; }
81
81
  .ok { background: #e9f4ec; color: #24623b; } .warn { background: #fcf2de; color: #795309; } .bad { background: #fcebea; color: #a03933; }
82
82
  .act { display: inline-flex; align-items: center; justify-content: center; border: 1px solid #c8d5cc; background: #fff; color: #344c3d; border-radius: 7px; padding: 8px 14px; min-height: 38px; text-decoration: none; font-size: 13px; font-weight: 550; line-height: 1.5; }
83
+ .act[hidden] { display: none; }
83
84
  .act:hover { background: #f4f8f3; border-color: #8ba695; }
84
85
  .act.primary { background: var(--kiwi); color: #fff; border-color: var(--kiwi); }
85
86
  .act.primary:hover { background: #164a34; }
@@ -98,6 +99,7 @@ export function renderMerchantManagementPage(options = {}) {
98
99
  .consent-label { display: flex; align-items: flex-start; gap: 8px; min-width: 0; }
99
100
  .consent-label span { flex: 1; min-width: 0; }
100
101
  .consent-label input { flex: none; margin: 3px 0 0; }
102
+ .catalog-settings { margin-block: 12px; }
101
103
  details summary { cursor: pointer; color: var(--kiwi); text-decoration: underline; text-underline-offset: 3px; }
102
104
  .row > .muted { flex: 1 1 180px; }
103
105
  pre.digest { font: 11px ui-monospace, monospace; color: var(--muted); white-space: pre-wrap; overflow-wrap: anywhere; margin: 4px 0; max-width: 320px; }
@@ -161,10 +163,11 @@ export function renderMerchantManagementPage(options = {}) {
161
163
  <button data-v="sales">销售工作台</button>
162
164
  <button data-v="outcomes">经营结果</button>
163
165
  <button data-v="weixin" id="weixin-tab" hidden>微信通道</button>
166
+ <button data-v="support" id="support-tab" hidden>售后工单</button>
164
167
  <p class="nav-foot">日常经营,井然有序。<br>Kiwi Merchant</p>
165
168
  </nav>
166
169
  <div class="workspace">
167
- <section class="page-head" aria-labelledby="page-title"><p class="eyebrow">MERCHANT WORKSPACE</p><h1 id="page-title">服务状态</h1><p id="page-description">掌握接待状态,管理店铺与目录连接。</p></section>
170
+ <section class="page-head" aria-labelledby="page-title"><p class="eyebrow">MERCHANT WORKSPACE</p><h1 id="page-title">服务状态</h1><p id="page-description">掌握接待状态,管理店铺与名片发布。</p></section>
168
171
  <main id="view" tabindex="-1" aria-label="商家工作区"><div class="card">加载中…</div></main>
169
172
  </div>
170
173
  <div id="bar" role="status" aria-live="polite" aria-atomic="true"></div>
@@ -177,6 +180,7 @@ var FILE_PRODUCTS = ${options.productAuthority === "file"};
177
180
  var CATALOG_CONNECT = ${options.catalogConnection === true};
178
181
  var WORKBUDDY_ACCESS = ${options.workbuddyAccess === true};
179
182
  var WEIXIN_CHANNEL = ${options.weixinChannel === true};
183
+ var MANUAL_SUPPORT = ${options.manualSupport === true};
180
184
  var viewSequence = 0;
181
185
  // 与 cloud/access/workbuddy-share-link 的 WORKBUDDY_CUSTODY_TERMS_VERSION 同值;
182
186
  // 浏览器产物必须自包含(verify:browser-artifact 禁止模块导入),所以这里是字面量,
@@ -185,8 +189,6 @@ var ACCESS_CONSENT_TERMS = "workbuddy-share-v1-2026-10-27";
185
189
  var catalogBeginKey = "";
186
190
  var catalogBeginPending = false;
187
191
  var catalogConnectionState = null;
188
- var catalogAccessState = null;
189
- var catalogAccessDetection = null;
190
192
  var publicationDraft = null;
191
193
  var publicationPicks = null;
192
194
  var publicationBusy = false;
@@ -298,6 +300,7 @@ function send(method, path, body, apiBase, again) {
298
300
  function showLogin() {
299
301
  viewSequence++;
300
302
  if (WEIXIN_CHANNEL) $("weixin-tab").hidden = true;
303
+ if (MANUAL_SUPPORT) $("support-tab").hidden = true;
301
304
  $("view").innerHTML = '<div class="card login-card"><h2>登录你的商家工作台</h2>' +
302
305
  '<p class="muted">请使用商家管理员口令登录后回到本页。</p>' +
303
306
  '<p><a class="act primary" href="/admin/login?next=%2Fmerchant%2F">去登录</a></p></div>';
@@ -310,46 +313,72 @@ function statePill(state) {
310
313
  }
311
314
 
312
315
  var PAIRING_CODE_HINTS = {
313
- PAIRING_WINDOW_EXPIRED: "上次授权已过期:请重新发起,并在 10 分钟内到目录完成确认",
314
- BIND_REJECTED: "目录拒绝了本次绑定请求:请重新发起,反复出现请联系管理员",
315
- BIND_CLAIM_CARD_URL_MISMATCH: "目录名片地址与已确认的公开预览不一致:请重新核对后再发起",
316
- BIND_CLAIM_FIELD_MISMATCH: "目录绑定材料与预览不一致:请重新发起",
317
- BIND_CLAIM_INVALID: "目录签名材料未通过本机校验:请重新发起",
318
- CARD_PUBLISH_REJECTED: "名片发布被目录拒绝:请稍后重试或联系管理员",
316
+ PAIRING_WINDOW_EXPIRED: "上次授权已过期:请重新发起,并在 10 分钟内到Kiwi网关完成确认",
317
+ BIND_REJECTED: "Kiwi网关拒绝了本次绑定请求:请重新发起,反复出现请联系管理员",
318
+ BIND_CLAIM_CARD_URL_MISMATCH: "Kiwi网关名片地址与已确认的公开预览不一致:请重新核对后再发起",
319
+ BIND_CLAIM_FIELD_MISMATCH: "Kiwi网关绑定材料与预览不一致:请重新发起",
320
+ BIND_CLAIM_INVALID: "Kiwi网关签名材料未通过本机校验:请重新发起",
321
+ CARD_PUBLISH_REJECTED: "名片发布被Kiwi网关拒绝:请稍后重试或联系管理员",
319
322
  CARD_PUBLISH_INVALID: "名片发布回执不完整:请刷新后重试",
320
- PAIRING_COMMUNICATION_FAILED: "与目录通信暂时失败:请稍后刷新重试"
323
+ PRODUCT_TABLE_INVALID: "商品表无法校验或与当前商家不匹配,请先修复原实例的商品表",
324
+ MERCHANT_DISPLAY_NAME_REQUIRED: "请先在原实例配置商家显示名,无需在聊天里重填",
325
+ CATALOG_RUNTIME_NOT_READY: "名片发布前的运行时检查未通过,请核对实例状态",
326
+ PAIRING_COMMUNICATION_FAILED: "与Kiwi网关通信暂时失败:请稍后刷新重试"
321
327
  };
322
328
 
323
329
  function catalogStatusText(s) {
324
- if (s.published) return "目录名片已发布";
325
- var labels = { idle: "尚未连接", preparing: "等待你确认授权", pending: "等待你确认授权",
326
- awaiting_confirmation: "等待你确认授权", authorized: "授权已收到,正在连接",
327
- bound: "已连接,正在检查接待能力", expired: "授权已过期,请重新发起",
328
- blocked: "连接未完成,请核对状态", paused: "目录服务已暂停", revoked: "授权已撤销" };
329
- return labels[s.status] || "连接尚未完成";
330
- }
331
- function appendCatalogConnection(html) {
332
- // 调用方固定按 appendCatalogConnection(...).then(appendCatalogAccess) 串联:
333
- // 没有目录连接或非 owner 时必须返回已决议的 Promise,返回字符串会在默认
334
- // 「服务状态」页抛 TypeError(旧流程整页停在“加载中…”)。
330
+ if (s.published) return "名片已发布";
331
+ var labels = { idle: "待发布名片", preparing: "待确认", pending: "待确认", awaiting_confirmation: "待确认",
332
+ authorized: "发布中", bound: "发布中", expired: "发布失败:授权已过期,请重新发起",
333
+ blocked: "发布失败", paused: "Kiwi网关服务已暂停", revoked: "授权已撤销" };
334
+ if (s.errorCode && (s.status === "authorized" || s.status === "bound")) return "发布失败,可刷新续办";
335
+ return labels[s.status] || "名片尚未发布";
336
+ }
337
+ function waitingForCatalogConfirmation(s) {
338
+ return !s.bindingId && ["preparing", "pending", "awaiting_confirmation"].indexOf(s.status) >= 0;
339
+ }
340
+ function appendCatalogSetup(html) {
335
341
  if (!CATALOG_CONNECT || ROLE !== "owner") return Promise.resolve(html);
342
+ var sequence = viewSequence;
336
343
  catalogConnectionState = null;
337
344
  return call("GET", "/catalog/connect").then(function (s) {
338
- catalogConnectionState = s;
339
- return html + '<div class="card"><h2>连接 Kiwi 目录</h2><p id="catalog-status">' + esc(catalogStatusText(s)) +
340
- (s.errorCode ? ' <span class="muted">[' + esc(s.errorCode) + ']</span>' : '') + '</p>' +
341
- (s.bindingId ? '<p>Kiwi 账户:<span id="bound-account">正在核对…</span></p>' : '<p>Kiwi 账户:尚未连接</p>') +
342
- (s.errorCode && PAIRING_CODE_HINTS[s.errorCode] ? '<p class="muted">' + esc(PAIRING_CODE_HINTS[s.errorCode]) + '</p>' : '') +
343
- '<p class="muted">连接后,采购方可在目录发现本店。请本人核对配对码与公开资料,并在目录授权页确认发布。</p>' +
344
- (s.published ? "" : ('<p class="muted">接入方式默认自动检测:连接时先尝试无码读取;单次失败不会判定为已加白或未加白。如平台门禁要求分享码,请在下方提交并授权。</p>' +
345
- '<button class="act primary" onclick="beginCatalogConnect()">发起或继续连接</button> ' +
346
- (WORKBUDDY_ACCESS && !s.bindingId ? '<button class="act" onclick="stageAccessAndBegin()">提交分享链接并发起</button> ' : '') +
347
- '<button class="act" onclick="showCatalogPairing()">本人查看配对信息</button>')) +
348
- ' <button class="act" onclick="refresh()">刷新连接状态</button><div id="catalog-pairing"></div></div>';
345
+ return call("GET", "/catalog/access").then(function (result) {
346
+ return result.access || null;
347
+ }).catch(function () { return null; }).then(function (a) {
348
+ if (sequence === viewSequence) catalogConnectionState = s;
349
+ return html + renderCatalogSetup(s, a);
350
+ });
349
351
  }).catch(function () {
350
- return html + '<div class="card"><h2>连接 Kiwi 目录</h2><p>暂时无法读取连接状态,请稍后刷新。</p></div>';
352
+ return html + '<div class="card"><h2>Kiwi网关</h2><p>暂时无法读取连接状态。</p>' +
353
+ '<button class="act" onclick="refresh()">刷新状态</button></div>';
351
354
  });
352
355
  }
356
+ function renderCatalogSetup(s, a) {
357
+ var waiting = waitingForCatalogConfirmation(s);
358
+ var canBegin = !s.published && !s.bindingId && !waiting && s.status !== "authorized";
359
+ var shareForm = a ? renderShareSettings(a, WORKBUDDY_ACCESS && canBegin) : "";
360
+ return '<div class="card"><h2>Kiwi网关</h2><p id="catalog-status">' + esc(catalogStatusText(s)) + '</p>' +
361
+ (s.bindingId ? '<p>Kiwi 账户 <span id="bound-account">正在核对…</span></p>' : '') +
362
+ (s.errorCode ? '<p class="muted">' + (PAIRING_CODE_HINTS[s.errorCode] ? esc(PAIRING_CODE_HINTS[s.errorCode]) + ' ' : '') +
363
+ '[' + esc(s.errorCode) + ']</p>' : '') +
364
+ (s.published ? '<p id="public-inquiry-status" class="muted" aria-live="polite">正在读取开放询价状态…</p>' :
365
+ '<p class="muted">' + (WORKBUDDY_ACCESS
366
+ ? '首次发布后将尝试开启开放询价;商品发布后,请在买家端实测。可在高级设置暂停。'
367
+ : '发布后,采购方可在Kiwi网关发现本店。') + '</p>') +
368
+ (waiting ? '<div id="catalog-pairing" aria-live="polite">正在读取配对信息…</div>' : '') +
369
+ (WORKBUDDY_ACCESS && canBegin ? shareForm : '') +
370
+ '<div class="row">' +
371
+ (canBegin ? '<button id="catalog-begin" class="act primary" onclick="connectCatalog()">发布名片</button>' : '') +
372
+ '<button class="act" onclick="refreshCatalogSetup()">刷新状态</button></div>' +
373
+ (WORKBUDDY_ACCESS && !canBegin ? shareForm : '') +
374
+ (a || s.published ? '<details class="catalog-settings"><summary>高级设置</summary>' +
375
+ (a ? renderAccessSettings(a) : '<p class="muted">暂时无法读取接入设置。</p>') +
376
+ (!WORKBUDDY_ACCESS ? shareForm : '') +
377
+ (s.published ? '<div class="catalog-settings"><h3>开放询价</h3>' +
378
+ '<p class="muted">买家可通过Kiwi网关询价,无需注册或逐个审批。Kiwi网关转发询价与报价,可读取报文,不保存磋商内容。</p>' +
379
+ '<button id="inquiry-toggle" class="act" onclick="togglePublicInquiry()" disabled>正在读取状态…</button></div>' : '') +
380
+ '</details>' : '') + '</div>';
381
+ }
353
382
  function accessModeLabel(mode) {
354
383
  return { auto: "自动检测(默认)", share_code: "使用分享码", no_share_code: "无分享码(直连)" }[mode] || "自动检测(默认)";
355
384
  }
@@ -357,156 +386,111 @@ function safeAccessMode(mode) {
357
386
  return ["auto", "share_code", "no_share_code"].indexOf(mode) >= 0 ? mode : "auto";
358
387
  }
359
388
  function detectionText(d) {
360
- if (!d) {
361
- return "尚未检测。默认自动检测:连接时先尝试无码读取;单次失败不会判定为已加白或未加白,也不会删除已保存的分享码。";
362
- }
363
- if (d.status === "direct_ok") {
364
- return "检测结果:本次无码读取公开名片成功。这不等于平台已加白,检测本身不改写已保存的接入方式或买家路由状态;如后续仍被门禁拦截,请重新检测或改用分享码。";
365
- }
366
- if (d.status === "share_code_ok") {
367
- return "检测结果:本次分享码可通过平台门禁。平台是否加白仍未确认;切换接入方式或重新检测都不会删除已保存的分享码。";
368
- }
369
- return "检测结果:不确定。" + (d.guidance || "单次 401/403、网络错误或不完整响应不能判定实例未加白。请先确认服务、公网入口与 TLS 正常,再提交分享链接或稍后重试。");
389
+ if (!d) return "";
390
+ if (d.status === "direct_ok") return "本次可直接访问公开名片。平台白名单需由 WorkBuddy 确认。";
391
+ if (d.status === "share_code_ok") return "本次分享码可通过平台门禁。";
392
+ return d.guidance || "暂时无法确认接入方式,请检查公网服务或分享链接。";
370
393
  }
371
394
  function showAccessDetection(d) {
372
- catalogAccessDetection = d || null;
373
395
  var target = $("access-detection");
374
396
  if (target) target.textContent = detectionText(d);
375
397
  }
376
398
  function accessStatusText(a) {
377
- if (!a) return "暂时无法读取访问配置状态";
378
- // 目录已有分享码,本次随绑凭据被按版本冲突跳过(目录保留原配置、不覆盖)。
379
- // 唯一可恢复路径是商家用权威版本显式更新,所以文案必须指向这个动作。
380
- if (a.statusReason === "catalog_access_exists")
381
- return "目录已有分享码配置,本次绑定没有覆盖它(不会用旧码顶掉已生效的码)。如需更换或启用,请重新粘贴分享链接并「提交或更新」,会按目录当前版本更新。";
382
- if (a.status === "active") {
383
- var routeSynced = a.catalogSync === undefined || a.catalogSync === "synced";
384
- if (a.mode === "no_share_code") {
385
- return routeSynced
386
- ? "目录已保存分享码(配置版本 " + a.configVersion + "),但当前接入方式为无分享码:买家按无码直连,已保存的分享码不用于路由,也不会被删除。"
387
- : "目录已保存分享码(配置版本 " + a.configVersion + "),本机选择无分享码,但目录路由状态尚未同步;买家仍按旧状态路由。已保存的分享码不会删除。";
388
- }
389
- return routeSynced
390
- ? "分享码已生效(配置版本 " + a.configVersion + "):买家按目录中继传输,中继只传输,不决定磋商。"
391
- : "分享码已生效(配置版本 " + a.configVersion + "),但目录路由状态尚未同步;买家仍按旧状态路由。目录中继只传输,不决定磋商。";
392
- }
393
- if (a.statusReason === "local_saved_not_used") return "分享码已加密保存,但当前接入方式为无分享码;连接时不会提交或发送。切换为自动检测或使用分享码后可继续,已保存的分享码不会被删除。";
394
- if (a.status === "pending" && a.statusReason === "local_staged") return "分享码已加密暂存,请继续连接;绑定完成后由目录验证";
395
- if (a.status === "pending") return "分享码已提交,等待平台校验";
396
- if (a.status === "invalid") return "分享码校验未通过,请重新提交分享链接";
397
- if (a.status === "revoked") {
398
- return a.statusReason === "superseded"
399
- ? "分享码已被新版本替换(旧码不再发放)"
400
- : "分享码已撤回,采购方当前无法经平台网关访问";
399
+ if (!a) return "分享链接状态暂不可用。";
400
+ if (a.statusReason === "catalog_access_exists") return "Kiwi网关保留了原分享码。更换请粘贴新链接并保存。";
401
+ if (a.statusReason === "local_saved_not_used" || (a.status === "active" && a.mode === "no_share_code")) {
402
+ return "分享码已保存,当前接入方式为无分享码。";
401
403
  }
402
- return "未保存分享码;当前接入方式:" + accessModeLabel(a.mode) + "。默认自动检测会先尝试无码读取;如平台门禁拦截,请提交本实例分享链接。";
404
+ if (a.status === "active") return "分享码已生效。";
405
+ if (a.status === "pending" && a.statusReason === "local_staged") return "分享码已加密保存,发布时由Kiwi网关校验。";
406
+ if (a.status === "pending") return "分享码等待平台校验。";
407
+ if (a.status === "invalid") return "分享码校验未通过,请更新分享链接。";
408
+ if (a.status === "revoked") return "分享码已撤销。";
409
+ return "平台要求分享码时,请填写本应用的分享链接。";
403
410
  }
404
411
  function accessSyncText(a) {
405
412
  var state = a && a.catalogSync;
406
- if (state === "synced")
407
- return "目录路由状态已同步" + (a.catalogRevision ? "(revision " + a.catalogRevision + ")" : "") + "。";
408
- if (state === "pending")
409
- return "本机已保存,但目录尚未确认同步;买家仍按旧状态路由。请点击“保存接入方式”或“刷新状态”重试。已保存的分享码不会被删除。";
410
- if (state === "failed")
411
- // 错误码只做纯文本插值:HTML 调用点在拼接时统一 esc(),textContent
412
- // 调用点本身安全;此处再转义会变成双重转义。
413
- return "目录同步失败(" + (a.catalogSyncError || "SYNC_FAILED") + ");本机选择已保留,买家仍按旧状态路由。已保存的分享码不会被删除。";
414
- if (state === "unsupported")
415
- return "目录不支持接入方式同步,或当前没有可用的活动绑定;买家仍按旧规则/安全回退。请确认目录连接后重新保存,本机选择与已保存分享码均已保留。";
416
- if (state === "unbound")
417
- return "尚未完成目录连接:接入方式只在本机保存,完成绑定后会自动同步。";
418
- return "目录同步状态未知;请点击“刷新状态”核对。买家不会按未经确认的本机选择路由。";
419
- }
420
- function appendCatalogAccess(html) {
421
- if (!CATALOG_CONNECT || ROLE !== "owner") return html;
422
- return call("GET", "/catalog/access").then(function (result) {
423
- var a = result.access || {};
424
- catalogAccessState = a;
425
- catalogAccessDetection = a.detection || null;
426
- var mode = safeAccessMode(a.mode);
427
- var modeOptions = ["auto", "share_code", "no_share_code"].map(function (value) {
428
- return '<option value="' + value + '"' + (mode === value ? ' selected' : '') + '>' + accessModeLabel(value) + '</option>';
429
- }).join("");
430
- var firstConnect = WORKBUDDY_ACCESS && catalogConnectionState && !catalogConnectionState.bindingId && !catalogConnectionState.published;
431
- return html + '<div class="card"><h2>接入方式与平台门禁</h2>' +
432
- '<div class="row"><label for="access-mode">接入方式</label>' +
433
- '<select id="access-mode" aria-label="接入方式">' + modeOptions + '</select>' +
434
- '<button class="act" onclick="saveAccessMode()">保存接入方式</button>' +
435
- '<button class="act" onclick="detectAccessMode()">检测当前接入方式</button></div>' +
436
- '<p id="access-detection" class="muted">' + esc(detectionText(a.detection)) + '</p>' +
437
- '<p id="access-sync" class="muted">' + esc(accessSyncText(a)) + '</p>' +
438
- '<p class="muted">默认自动检测。接入方式经目录公开为买家路由状态:使用分享码时买家中继,无分享码时买家直连,自动检测按目录已有分享码保守回退。无分享码不等于平台已确认加白;平台没有公开的白名单验证接口。切换接入方式不会删除已保存分享码;只有「撤回授权并撤销」会清除分享码。</p>' +
439
- (a.modeInvalid ? '<p class="warn">接入方式偏好无法解析,已安全回退自动检测;未删除任何分享码。</p>' : '') +
440
- '<h3>WorkBuddy 分享码(平台门禁)</h3>' +
441
- '<p id="access-status">' + esc(accessStatusText(a)) + '</p>' +
442
- (a.custodyEnabled === false
443
- ? '<p class="warn">目录代管当前已被运营关闭,无法新建或更新;你仍可撤回授权。</p>'
444
- : '') +
445
- '<div class="row"><input id="access-link" type="text" oninput="updateAccessButton()" autocomplete="off" spellcheck="false" aria-label="WorkBuddy 分享链接" placeholder="粘贴 WorkBuddy 分享链接">' +
446
- (firstConnect ? '<button class="act primary" onclick="stageAccessAndBegin()">授权并发起或继续连接</button>' : '<button id="access-submit" class="act primary" onclick="submitAccessLink()" disabled>提交或更新</button>') +
447
- '<button class="act" onclick="refreshAccess()">刷新状态</button>' +
448
- (a.consentGranted ? '<button id="access-revoke" class="act danger" onclick="revokeAccess()">撤回授权并撤销</button>' : '') + '</div>' +
449
- '<div class="row"><label class="consent-label"><input id="access-consent" type="checkbox" aria-label="同意分享码代管授权">' +
450
- ' <span>我同意 Kiwi 目录代管分享码,并按需向我已接受的采购方出示。</span></label> ' +
451
- '<details><summary>查看代管授权与使用说明</summary><p>平台仍以分享码门禁拦截时,请以本应用的具体应用 ID 向腾讯云提交工单,申请白名单并取消平台分享码门禁。本页不会自动识别平台是否加白。</p>' +
452
- '<p>加白前,新版买家经 Kiwi 目录中继传输询价与磋商。平台确认加白且你自行核实无需分享码后,可在本页选择「无分享码(直连)」并保存;待目录同步确认后,新版买家改为直连,不再经中继。</p>' +
453
- '<p>旧客户端可能仍使用旧访问机制,并非所有买家都会立即切换;已接受的采购方仍可能按旧访问机制获取分享码,目录仅向他们按需出示并按买家限流。</p>' +
454
- '<p>分享码用于通过 WorkBuddy 网关,不是商家身份或业务授权;本页不会显示分享码。你可随时撤回,撤回后目录会清空已保存的分享码。</p>' +
455
- '<p>关闭「发布为网站」后链接立即失效;删除源任务或源文件不会使分享链接失效,需要主动下线。平台不承诺分享码的固定有效期或自动轮换;换码后请重新提交。</p>' +
456
- '<p>分享码等价于访问凭据,平台未提供机器专用入口,网关不透传 Authorization,也不适合大规模机器自动分发。如不希望依赖此方式,可使用自托管公网入口。</p></details></div>' +
457
- (a.recentFaults > 0
458
- ? '<p class="warn">最近 24 小时有 ' + a.recentFaults +
459
- ' 次采购方报告门禁失败' + (a.recentFaultsLastAt ? '(最后一次 ' + esc(a.recentFaultsLastAt) + ')' : '') +
460
- '。请核对分享码是否已在平台侧更换或失效,必要时重新粘贴分享链接。</p>'
461
- : '') +
462
- (a.consentGranted && a.status === "active" ? '<p class="muted">目录代管授权已生效' + (a.consentTermsVersion ? '(条款 ' + esc(a.consentTermsVersion) + ')' : '') + '</p>' : '') +
463
- (a.authMode ? '<p class="muted">业务认证:' + esc(a.authMode) + ' · 适配版本:' + esc(a.adapterVersion || "-") + '</p>' : '') +
464
- '</div>' +
465
- '<div class="card"><h2>开放询价</h2>' +
466
- '<p id="public-inquiry-status" class="muted">请查询当前授权状态。</p>' +
467
- '<div class="row"><label class="consent-label"><input id="public-inquiry-consent" type="checkbox" aria-label="同意开放询价"> <span>我同意买家无需注册或事先批准即可通过 Kiwi 目录询价。</span></label> <details><summary>查看开放询价授权与使用说明</summary><p>允许买家无需注册、关注或商家逐个批准,通过 Kiwi 目录主动询价。询价与报价报文会经 Kiwi 目录转发给商家;目录不裁决条款、不保存磋商内容,但能读到报文明文。Kiwi 负责通过平台门禁;商家后台仍仅供管理员登录。需先完成目录连接;如平台门禁要求分享码,还需完成受控代管。本项为单独授权,可随时关闭;关闭后停止新的开放询价。</p></details></div>' +
468
- '<div class="row"><button id="inquiry-submit" class="act primary" onclick="setPublicInquiry(true)" disabled>授权开放询价</button> ' +
469
- '<button class="act" onclick="refreshPublicInquiry()">查询授权状态</button> ' +
470
- '<button class="act danger" onclick="setPublicInquiry(false)">关闭开放询价</button></div></div>';
471
- }).catch(function () {
472
- return html + '<div class="card"><h2>接入方式与平台门禁</h2><p>暂时无法读取访问配置状态,请稍后刷新。</p></div>';
473
- });
474
- }
475
- function showPublicInquiryStatus(consent, notify) {
476
- var target = $("public-inquiry-status");
477
- var text = !consent || !consent.granted ? "尚未授权开放询价" :
478
- consent.policyActive && consent.localEnabled ? "已授权;买家接入仍需完成实际验证" : "已授权,当前尚未就绪;请检查目录连接和分享码";
413
+ if (state === "synced") return "Kiwi网关路由已同步。";
414
+ var retryHint = "接入方式已保存在本机。买家仍按Kiwi网关原有设置接入,可刷新重试。";
415
+ if (state === "failed") return "同步失败(" + (a.catalogSyncError || "SYNC_FAILED") + ")。" + retryHint;
416
+ if (state === "pending") return retryHint;
417
+ if (state === "unsupported") return "当前Kiwi网关暂不支持同步接入方式。";
418
+ if (state === "unbound") return "接入方式将在完成名片发布后同步。";
419
+ return "";
420
+ }
421
+ function renderAccessSettings(a) {
422
+ var mode = safeAccessMode(a.mode);
423
+ var options = ["auto", "share_code", "no_share_code"].map(function (value) {
424
+ return '<option value="' + value + '"' + (mode === value ? ' selected' : '') + '>' + accessModeLabel(value) + '</option>';
425
+ }).join("");
426
+ return '<h3>接入方式</h3><div class="row"><label for="access-mode">买家接入</label>' +
427
+ '<select id="access-mode">' + options + '</select>' +
428
+ '<button class="act" onclick="saveAccessMode()">保存</button>' +
429
+ '<button class="act" onclick="detectAccessMode()">检测</button></div>' +
430
+ '<p id="access-detection" class="muted">' + esc(a.detection ? detectionText(a.detection) : "") + '</p>' +
431
+ '<p id="access-sync" class="muted">' + esc(accessSyncText(a)) + '</p>' +
432
+ '<p class="muted">自动检测优先尝试直接访问。切换接入方式会保留分享码;撤销代管可清除。平台白名单需由 WorkBuddy 确认。</p>' +
433
+ (a.modeInvalid ? '<p class="warn">已将无法识别的接入方式恢复为自动检测。</p>' : '');
434
+ }
435
+ function renderShareSettings(a, connectUsesLink) {
436
+ var needsLink = a.status === "invalid" || (a.mode === "share_code" && !a.consentGranted);
437
+ return '<details class="catalog-settings" id="access-share-settings"' + (needsLink ? ' open' : '') + '>' +
438
+ '<summary>WorkBuddy 分享链接' + (a.statusReason === "local_staged" ? '(已保存)' : connectUsesLink ? '(按需填写)' : '') + '</summary>' +
439
+ '<p id="access-status" class="muted">' + esc(accessStatusText(a)) + '</p>' +
440
+ (a.custodyEnabled === false ? '<p class="warn">分享码代管暂不可用。</p>' : '') +
441
+ '<input id="access-link" type="text" oninput="updateAccessButton()" autocomplete="off" spellcheck="false" aria-label="WorkBuddy 分享链接" placeholder="粘贴本应用的 WorkBuddy 分享链接">' +
442
+ '<p><label class="consent-label"><input id="access-consent" type="checkbox" aria-label="同意分享码代管授权">' +
443
+ '<span>我同意Kiwi网关代管分享码,用于通过平台门禁转发询价,并按需向已接受的采购方出示。</span></label></p>' +
444
+ '<details class="catalog-settings"><summary>查看代管授权与使用说明</summary>' +
445
+ '<p class="muted">WorkBuddy 要求分享码时,请填写本应用的分享链接。链接失效或换码后,请在此更新。撤销代管会清除已保存的分享码。</p></details>' +
446
+ '<div class="row">' +
447
+ (connectUsesLink ? '<p class="muted">填写后,点击下方「发布名片」。</p>' : '<button id="access-submit" class="act" onclick="submitAccessLink()" disabled>保存分享链接</button>') +
448
+ '<button id="access-revoke" class="act danger" onclick="revokeAccess()"' + (a.consentGranted ? '' : ' hidden') + '>撤销代管</button>' +
449
+ '</div>' +
450
+ (a.recentFaults > 0 ? '<p class="warn">最近 24 小时有 ' + esc(a.recentFaults) + ' 次门禁失败,请检查分享链接。</p>' : '') +
451
+ '</details>';
452
+ }
453
+ var publicInquiryConsent = null;
454
+ function showPublicInquiryStatus(consent) {
455
+ publicInquiryConsent = consent || null;
456
+ var target = $("public-inquiry-status"), button = $("inquiry-toggle");
457
+ var text = !consent ? "开放询价状态暂不可用" :
458
+ consent.pending && consent.granted && !consent.policyActive ? "开放询价暂未生效,请检查接入方式和分享链接" :
459
+ consent.pending ? "开放询价正在开通或待同步,请稍后刷新" :
460
+ !consent.granted ? "开放询价已暂停" :
461
+ consent.policyActive && consent.localEnabled ? "开放询价已开启" : "开放询价暂不可用,请检查接入方式和分享链接";
479
462
  if (target) target.textContent = text;
480
- var button = $("inquiry-submit");
481
- if (button) button.disabled = !!(consent && consent.granted);
482
- // 后台刷新不得覆盖全局状态条(例如“已撤销”)。只有显式查询/操作才写 bar。
483
- if (notify !== false) bar(text, false);
463
+ if (button) {
464
+ button.textContent = consent && (consent.granted || consent.pending) ? "暂停开放询价" : "开启开放询价";
465
+ button.disabled = !consent;
466
+ }
484
467
  }
485
- function refreshPublicInquiry(notify) {
486
- var button = $("inquiry-submit"); if (button) button.disabled = true;
468
+ function refreshPublicInquiry(sequence) {
469
+ var button = $("inquiry-toggle"); if (button) button.disabled = true;
487
470
  return call("POST", "/catalog/public-inquiry/status", {}).then(function (result) {
488
- showPublicInquiryStatus(result.consent, notify);
489
- }).catch(function (error) {
490
- // 后台刷新失败同样不得覆盖全局状态条。
491
- if (notify !== false) bar(error && error.message ? error.message : "无法查询开放询价状态", true);
471
+ if (sequence === viewSequence) showPublicInquiryStatus(result.consent);
472
+ }).catch(function () {
473
+ if (sequence === viewSequence) showPublicInquiryStatus(null);
492
474
  });
493
475
  }
494
- function setPublicInquiry(granted) {
495
- var button = $("inquiry-submit");
496
- if (granted && button && button.disabled) return;
497
- var checkbox = $("public-inquiry-consent");
498
- if (granted && (!checkbox || !checkbox.checked)) { bar("请先勾选开放询价授权。", true); return; }
499
- if (!granted && !window.confirm("关闭后,新的买家将不能通过 Kiwi 目录向本店发起开放询价。确定关闭?")) return;
500
- if (button) button.disabled = true;
476
+ function togglePublicInquiry() {
477
+ var button = $("inquiry-toggle");
478
+ if (!button || button.disabled || !publicInquiryConsent) return;
479
+ var granted = !publicInquiryConsent.granted && !publicInquiryConsent.pending;
480
+ button.disabled = true;
481
+ var sequence = viewSequence;
501
482
  return call("POST", "/catalog/public-inquiry/consent", { granted: granted, terms_version: "open-inquiry-v1" }).then(function (result) {
502
- if (checkbox) checkbox.checked = false;
503
- showPublicInquiryStatus(result.consent);
504
- }).catch(function () { bar("开放询价状态尚未确认,请先查询授权状态后再决定是否重试。", true); });
483
+ if (sequence === viewSequence) showPublicInquiryStatus(result.consent);
484
+ }).catch(function () {
485
+ bar("开放询价状态尚未确认,请刷新状态。", true);
486
+ if (sequence === viewSequence) showPublicInquiryStatus(null);
487
+ });
505
488
  }
506
489
  function accessSummaryBar(a, verb) {
507
490
  var target = $("access-status"); if (target) target.textContent = accessStatusText(a);
508
491
  var sync = $("access-sync"); if (sync) sync.textContent = accessSyncText(a);
509
492
  if (a && a.detection !== undefined) showAccessDetection(a.detection);
493
+ var revoke = $("access-revoke"); if (revoke && a) revoke.hidden = !a.consentGranted;
510
494
  bar(verb + ":" + accessStatusText(a) + " " + accessSyncText(a), a && (a.status === "invalid" || (a.catalogSync && a.catalogSync !== "synced")));
511
495
  }
512
496
  function saveAccessMode() {
@@ -517,11 +501,12 @@ function saveAccessMode() {
517
501
  return;
518
502
  }
519
503
  return call("POST", "/catalog/access/mode", { mode: mode }).then(function (result) {
520
- catalogAccessState = result.access || {};
521
- showAccessDetection(catalogAccessState.detection);
522
- var sync = $("access-sync"); if (sync) sync.textContent = accessSyncText(catalogAccessState);
523
- var synced = catalogAccessState.catalogSync === "synced";
524
- bar("接入方式已保存:" + accessModeLabel(mode) + "。" + accessSyncText(catalogAccessState) + " 已保存的分享码没有被删除。", !synced);
504
+ var a = result.access || {};
505
+ var status = $("access-status"); if (status) status.textContent = accessStatusText(a);
506
+ var sync = $("access-sync"); if (sync) sync.textContent = accessSyncText(a);
507
+ showAccessDetection(a.detection);
508
+ bar("已保存接入方式。" + accessSyncText(a));
509
+ if (catalogConnectionState && catalogConnectionState.published) refreshPublicInquiry(viewSequence);
525
510
  }).catch(function (error) {
526
511
  bar(error && error.message ? error.message : "接入方式未保存,请刷新后重试。", true);
527
512
  });
@@ -539,112 +524,100 @@ function detectAccessMode() {
539
524
  var accessSubmitPending = false;
540
525
  function updateAccessButton() {
541
526
  var button = $("access-submit"), input = $("access-link");
542
- if (button) button.disabled = accessSubmitPending || !input || !String(input.value || "").trim();
527
+ if (button) button.disabled = accessSubmitPending || !input || !input.value.trim();
543
528
  }
544
- function submitAccessLink() {
545
- var button = $("access-submit");
546
- if (button && button.disabled) return;
547
- var input = $("access-link");
548
- var consent = $("access-consent");
549
- var link = input ? String(input.value || "").trim() : "";
550
- if (!link) { bar("请先粘贴 WorkBuddy 分享链接。", true); return; }
551
- if (!consent || !consent.checked) {
552
- bar("请先勾选并同意《分享码代管授权》。", true);
553
- return;
554
- }
555
- accessSubmitPending = true;
556
- if (button) button.disabled = true;
557
- return call("POST", "/catalog/access/share-link", {
558
- link: link,
559
- consent_terms_version: ACCESS_CONSENT_TERMS
560
- }).then(function (result) {
561
- catalogAccessState = result.access;
529
+ function saveShareLink(link) {
530
+ var input = $("access-link"), consent = $("access-consent");
531
+ return call("POST", "/catalog/access/share-link", { link: link, consent_terms_version: ACCESS_CONSENT_TERMS }).then(function (result) {
562
532
  if (input) input.value = "";
563
- accessSubmitPending = false;
564
- updateAccessButton();
565
533
  if (consent) consent.checked = false;
566
- accessSummaryBar(result.access, "已提交");
567
- }).catch(function (e) {
568
- // 错误文本由服务端给自有码说明;不把链接回显到页面。
569
- // 审查 C-11:失败时保留输入框内容,允许用户核对后直接重试。
534
+ return result.access;
535
+ });
536
+ }
537
+ function submitAccessLink() {
538
+ if (accessSubmitPending) return;
539
+ var input = $("access-link"), consent = $("access-consent");
540
+ var link = input ? input.value.trim() : "";
541
+ if (!link) return;
542
+ if (!consent || !consent.checked) { bar("请先勾选并同意分享码代管。", true); return; }
543
+ accessSubmitPending = true;
544
+ updateAccessButton();
545
+ return saveShareLink(link).then(function (a) {
546
+ accessSummaryBar(a, "已保存");
547
+ if (catalogConnectionState && catalogConnectionState.published) refreshPublicInquiry(viewSequence);
548
+ }).catch(function (error) {
549
+ bar(error.message || "保存未完成,请重试。", true);
550
+ }).finally(function () {
570
551
  accessSubmitPending = false;
571
552
  updateAccessButton();
572
- bar(e && e.message ? e.message : "提交未完成,请先刷新状态后再试。", true);
573
553
  });
574
554
  }
575
- function stageAccessAndBegin() {
576
- if (catalogBeginPending) return;
577
- var input = $("access-link");
578
- var consent = $("access-consent");
579
- var link = input ? String(input.value || "").trim() : "";
580
- var staged = catalogAccessState && catalogAccessState.status === "pending" && catalogAccessState.statusReason === "local_staged";
581
- if (!link && staged) return beginCatalogConnect();
582
- if (!link) { bar("请先粘贴 WorkBuddy 分享链接。", true); return; }
583
- if (!consent || !consent.checked) { bar("请先勾选并同意《分享码代管授权》。", true); return; }
584
- catalogBeginPending = true;
585
- return call("POST", "/catalog/access/share-link", { link: link, consent_terms_version: ACCESS_CONSENT_TERMS })
586
- .then(function (result) {
587
- catalogAccessState = result.access;
588
- if (input) input.value = "";
589
- if (consent) consent.checked = false;
590
- // Only a successful owner-authorized stage may advance to enrollment.
591
- catalogBeginPending = false;
592
- return beginCatalogConnect();
593
- }).catch(function () {
594
- bar("分享链接暂存未确认,请先刷新状态;确认已暂存后可继续连接。", true);
595
- }).finally(function () { catalogBeginPending = false; });
596
- }
597
- function refreshAccess() {
598
- call("POST", "/catalog/access/refresh", {}).then(function (result) {
599
- catalogAccessState = result.access;
600
- accessSummaryBar(result.access, "已刷新");
601
- }).catch(function () { bar("刷新访问配置状态失败。", true); });
555
+ function refreshCatalogSetup() {
556
+ if (!catalogConnectionState || !catalogConnectionState.bindingId) return refresh();
557
+ return call("POST", "/catalog/access/refresh", {}).catch(function () {
558
+ bar("暂时无法同步接入设置。", true);
559
+ }).then(refresh);
602
560
  }
603
561
  function revokeAccess() {
604
- if (!window.confirm("撤回授权后:采购方将无法经 WorkBuddy 访问本店,目录会清空已保存的分享码。确定撤回?")) return;
562
+ if (!window.confirm("撤回授权后:采购方将无法经 WorkBuddy 访问本店,Kiwi网关会清空已保存的分享码。确定撤回?")) return;
605
563
  call("POST", "/catalog/access/revoke", {}).then(function (result) {
606
- catalogAccessState = result.access;
607
564
  accessSummaryBar(result.access, "已撤销");
608
565
  var button = $("access-revoke"); if (button) button.disabled = true;
609
- refreshPublicInquiry(false);
610
566
  refresh();
611
567
  }).catch(function () { bar("撤销未完成,请稍后重试。", true); });
612
568
  }
613
- function beginCatalogConnect() {
569
+ function connectCatalog() {
614
570
  if (catalogBeginPending) return;
571
+ var input = $("access-link"), consent = $("access-consent"), button = $("catalog-begin");
572
+ var link = WORKBUDDY_ACCESS && input ? input.value.trim() : "";
573
+ if (link && (!consent || !consent.checked)) { bar("请先勾选并同意分享码代管。", true); return; }
615
574
  catalogBeginPending = true;
575
+ if (button) button.disabled = true;
576
+ var stage = link ? saveShareLink(link).then(function (a) { accessSummaryBar(a, "已保存"); }) : Promise.resolve();
577
+ return stage.then(beginCatalogConnect).catch(function (error) {
578
+ bar(error.message || "分享链接保存未确认,请重试。", true);
579
+ }).finally(function () {
580
+ catalogBeginPending = false;
581
+ if (button) button.disabled = false;
582
+ });
583
+ }
584
+ function beginCatalogConnect() {
616
585
  try { if (!catalogBeginKey) catalogBeginKey = sessionStorage.getItem("kiwi-catalog-connect-key") || ""; } catch (_) {}
617
586
  if (!catalogBeginKey) catalogBeginKey = key("catalog-connect");
618
587
  try { sessionStorage.setItem("kiwi-catalog-connect-key", catalogBeginKey); } catch (_) {}
619
588
  return call("POST", "/catalog/connect/begin", { idempotency_key: catalogBeginKey }).then(function (receipt) {
620
589
  if (receipt.status === "succeeded") { catalogBeginKey = ""; try { sessionStorage.removeItem("kiwi-catalog-connect-key"); } catch (_) {} }
621
- bar(receipt.status === "succeeded" ? "连接申请已准备,请本人查看配对信息并确认公开资料。" : "连接结果尚未确认,请先查看状态,不要重复提交。");
590
+ bar(receipt.status === "succeeded" ? "连接申请已准备,请核对配对码和公开资料,并在Kiwi网关确认发布。" : "连接结果尚未确认,请先查看状态,不要重复提交。");
622
591
  refresh();
623
592
  }).catch(function (error) {
624
593
  var message = error && error.message ? error.message : "连接申请暂未完成,请先刷新状态后再试。";
625
594
  // 连接时的公开探测失败同样只能呈现“不确定”,不能把单次失败说成未加白。
626
595
  if (/接入方式|公开名片|公网入口|商家服务|分享码|不确定|超时/.test(message)) {
596
+ var share = $("access-share-settings"); if (share) share.open = true;
627
597
  showAccessDetection({
628
598
  status: "uncertain",
629
599
  guidance: message + " 单次失败不能判定实例未加白;已保存的分享码不会被删除。"
630
600
  });
631
601
  }
632
602
  bar(message, true);
633
- })
634
- .finally(function () { catalogBeginPending = false; });
603
+ });
635
604
  }
636
- function showCatalogPairing() {
605
+ function showCatalogPairing(sequence) {
637
606
  return call("GET", "/catalog/connect/pairing").then(function (result) {
607
+ if (sequence !== viewSequence) return;
638
608
  var box = $("catalog-pairing");
639
609
  if (!box) return;
640
610
  var p = result.pairing;
641
611
  if (!p) { box.textContent = "当前没有待确认的配对信息,请先发起连接或刷新状态。"; return; }
642
612
  var url = new URL(p.verification_uri);
643
613
  if (url.protocol !== "https:" || url.username || url.password) throw new Error("invalid link");
644
- box.innerHTML = '<p>请在目录授权页核对相同配对码:<strong>' + esc(p.user_code) + '</strong></p>' +
645
- '<p class="muted">有效期至 ' + esc(p.expires_at) + '。配对码请自行核对,不要发送给助手。</p>' +
646
- '<a target="_blank" rel="noopener noreferrer" href="' + esc(url.href) + '">前往 Kiwi 目录核对并授权</a>';
647
- }).catch(function () { bar("无法读取配对信息,请刷新连接状态。", true); });
614
+ box.innerHTML = '<p>请在Kiwi网关核对配对码 <strong>' + esc(p.user_code) + '</strong></p>' +
615
+ '<p class="muted">有效期至 ' + esc(new Date(p.expires_at).toLocaleString()) + '。请勿转发配对码。</p>' +
616
+ '<p><a class="act primary" target="_blank" rel="noopener noreferrer" href="' + esc(url.href) + '">前往Kiwi网关确认发布</a></p>';
617
+ }).catch(function () {
618
+ var box = $("catalog-pairing");
619
+ if (sequence === viewSequence && box) box.textContent = "无法读取配对信息,请刷新状态。";
620
+ });
648
621
  }
649
622
 
650
623
  /** 商品状态文案:报价有效期不影响上架状态。 */
@@ -656,11 +629,11 @@ function productStatusLabel(it) {
656
629
  }
657
630
  /** 目录发布状态文案:known=false 时不把“读不到”说成“未发布”。 */
658
631
  function publicationStateLabel(state, known) {
659
- if (!known) return "状态未知";
632
+ if (!known) return "待核验";
660
633
  var normalized = String(state || "").toLowerCase();
661
- if (normalized === "active") return "已发布";
662
- if (normalized === "paused") return "已暂停";
663
- return "未发布";
634
+ if (normalized === "active") return "商品已发布";
635
+ if (normalized === "paused" || normalized === "suspended") return "已暂停";
636
+ return "已导入,待发布";
664
637
  }
665
638
  function togglePublicationPicks(box) {
666
639
  if (publicationDraft !== null) {
@@ -837,14 +810,14 @@ function renderProducts(items, publication) {
837
810
  esc(it.authority_version == null ? "-" : it.authority_version) + "</td></tr>";
838
811
  }).join("");
839
812
  var summary = canPublish
840
- ? '<p class="muted" id="pub-summary">已发布到目录 ' + publishedCount + " 件,未发布 " + (list.length - publishedCount) + " 件。" +
841
- (known ? "" : "目录发布状态暂不可读取,刷新后重试。") + "</p>"
813
+ ? '<p class="muted" id="pub-summary">已发布到Kiwi网关 ' + publishedCount + " 件,未发布 " + (list.length - publishedCount) + " 件。" +
814
+ (known ? "" : "Kiwi网关发布状态暂不可读取,刷新后重试。") + "</p>"
842
815
  : "";
843
816
  var selectAll = canPublish && list.length > 0
844
817
  ? '<div class="row"><label class="consent-label"><input type="checkbox" id="pub-select-all" aria-label="全选商品" onchange="togglePublicationPicks(this)"> 全选当前商品</label></div>'
845
818
  : "";
846
819
  var header = canPublish
847
- ? "<tr><th>选择</th><th>SKU</th><th>名称</th><th>状态</th><th>目录状态</th><th>分类(必填)</th><th>单价</th><th>起订量</th><th>数据有效期</th><th>交易平台</th><th>平台商品ID</th><th>平台商品页链接</th></tr>"
820
+ ? "<tr><th>选择</th><th>SKU</th><th>名称</th><th>状态</th><th>Kiwi网关状态</th><th>分类(必填)</th><th>单价</th><th>起订量</th><th>数据有效期</th><th>交易平台</th><th>平台商品ID</th><th>平台商品页链接</th></tr>"
848
821
  : (FILE_PRODUCTS
849
822
  ? "<tr><th>SKU</th><th>名称</th><th>单价</th><th>起订量</th><th>数据有效期</th><th>状态</th><th>交易平台</th><th>平台商品ID</th><th>平台商品页链接</th></tr>"
850
823
  : "<tr><th>SKU</th><th>名称</th><th>价格(最小币单位)</th><th>库存</th><th>版本</th></tr>");
@@ -856,20 +829,21 @@ function renderProducts(items, publication) {
856
829
  '<button class="act" id="pub-clear" onclick="clearPublicationPicks()">清空选择</button></div>' +
857
830
  '<p class="muted">公开名称、SKU、分类、商品页链接与其余公开字段;价格、库存及私有规则不会发布。' +
858
831
  '可发布已导入或已有商品,不涉及下架其他商品。预览后选择会锁定,修改需先「重新选择」。' +
859
- '上架状态和报价有效期在目录商家后台设置。报价过期后商品仍保持上架。</p>' +
832
+ '上架状态和报价有效期在Kiwi网关商家后台设置。报价过期后商品仍保持上架。</p>' +
860
833
  '<button class="act" id="pub-preview" onclick="previewPublication()">预览公开内容</button>' +
861
834
  '<div id="publication-result"></div>'
862
835
  : (canPublish ? '<p class="muted">暂无商品,请先在下方导入商品后再选择发布。</p>' : "");
863
- return '<div class="card"><h2>' + (canPublish ? "商品与目录发布" : "当前商品") + "(" + list.length + ")</h2>" + summary +
836
+ return '<div class="card"><h2>' + (canPublish ? "商品与Kiwi网关发布" : "当前商品") + "(" + list.length + ")</h2>" + summary +
864
837
  selectAll + (rows ? table : '<p class="muted">暂无商品,请先导入商品。没有商品时服务不会报价或发布名片。</p>') +
865
838
  publishControls + "</div>" +
866
839
  '<div class="card"><h2>导入商品表(按 SKU 追加/更新)</h2>' +
867
840
  '<p class="muted">交易链接仅为平台商品页;是否成交、实际价格与付款结果由交易平台确认。</p>' +
868
- '<p class="muted">支持 CSV、Excel(.xlsx)与商品表 JSON:先转换校验、预览新增/更新/保留,确认后整批生效(不成功的批次不改动现有商品)。文件里没有的商品会保留,只有同 SKU 会被更新;导入不会撤回已发布到目录的商品。</p>' +
841
+ '<p class="muted">支持 CSV、Excel(.xlsx)与商品表 JSON:先转换校验、预览新增/更新/保留,确认后整批生效(不成功的批次不改动现有商品)。文件里没有的商品会保留,只有同 SKU 会被更新;导入不会撤回已发布到Kiwi网关的商品。</p>' +
869
842
  '<p class="muted">模板:<a href="/merchant/api/v1/products/import-template?format=csv" download="kiwi-product-import-template.csv">下载 CSV 模板</a> · <a href="/merchant/api/v1/products/import-template?format=xlsx" download="kiwi-product-import-template.xlsx">下载 Excel 模板</a>(中文表头与示例行;中英文常见列名会自动识别)。</p>' +
870
843
  '<div class="row"><input type="file" id="pfile" aria-label="选择商品导入文件" accept=".json,.csv,.xlsx">' +
871
844
  '<button class="act" onclick="previewImport()">校验预览</button></div><div id="presult"></div></div>';
872
845
  }
846
+
873
847
  var ALIPAY_COLLECTION = "/collection/alipay";
874
848
  function renderAlipayCollection(config) {
875
849
  return '<section class="card"><h2>支付宝收款</h2>' +
@@ -1015,10 +989,85 @@ function bindAlipayApprovals() {
1015
989
  });
1016
990
  }
1017
991
 
992
+ function mountSupport(sequence) {
993
+ var root = $("support-content");
994
+ var idPattern = /^CS-[0-9a-f-]{36}$/;
995
+ var labels = { WAITING_MERCHANT: "等待领取", IN_PROGRESS: "人工处理中", RESOLVED: "本地工单已解决" };
996
+ var selection = 0;
997
+ function visible() { return current === "support" && sequence === viewSequence; }
998
+ function problem(error) { if (visible()) bar(error.message, true); }
999
+ function text(parent, tag, value) {
1000
+ var node = document.createElement(tag); node.textContent = value; parent.appendChild(node); return node;
1001
+ }
1002
+ function button(parent, title, action) {
1003
+ var node = text(parent, "button", title);
1004
+ node.type = "button"; node.className = "act"; node.onclick = action; return node;
1005
+ }
1006
+ function detail(id) {
1007
+ if (!idPattern.test(id)) return;
1008
+ var selected = ++selection;
1009
+ var panel = $("support-detail"); panel.textContent = "";
1010
+ call("GET", "/support/cases/" + id).then(function (item) {
1011
+ if (!visible() || selection !== selected) return;
1012
+ text(panel, "h3", item.case_id + " · " + labels[item.state]);
1013
+ if (item.sku) text(panel, "p", "SKU:" + item.sku);
1014
+ text(panel, "p", item.description);
1015
+ item.replies.forEach(function (entry) {
1016
+ text(panel, "p", (entry.kind === "resolved" ? "解决说明:" : "已保存:") + entry.text);
1017
+ });
1018
+ if (!item.access_active) { text(panel, "p", "买家原会话不可继续,不代表工单已关闭。"); return; }
1019
+ function change(action, payload) {
1020
+ var controls = Array.from(panel.querySelectorAll("button"));
1021
+ controls.forEach(function (control) { control.disabled = true; });
1022
+ payload.expected_revision = item.revision;
1023
+ call("POST", "/support/cases/" + id + "/" + action, payload).then(function () {
1024
+ if (!visible() || selection !== selected) return;
1025
+ bar(action === "reply" ? "已保存,可供原会话查看;未核实交付。" : "工单已更新。", false);
1026
+ load(); detail(id);
1027
+ }).catch(function (error) {
1028
+ if (selection === selected) problem(error);
1029
+ }).finally(function () { controls.forEach(function (control) { control.disabled = false; }); });
1030
+ }
1031
+ if (item.state === "WAITING_MERCHANT") {
1032
+ button(panel, "领取工单", function () { change("claim", {}); });
1033
+ } else if (item.state === "IN_PROGRESS" && item.held_by_me) {
1034
+ var label = text(panel, "label", "人工说明 ");
1035
+ var field = document.createElement("textarea"); field.maxLength = 2000; label.appendChild(field);
1036
+ function save(action) {
1037
+ if (!field.value.trim()) { bar("请先填写人工说明。", true); return; }
1038
+ if (!window.confirm(action === "reply" ? "将说明保存为买家在原会话可见的人工回复?" : "标记本地工单已解决并保存处理说明?")) return;
1039
+ change(action, { text: field.value });
1040
+ }
1041
+ button(panel, "保存给买家查看", function () { save("reply"); });
1042
+ button(panel, "记录解决", function () { save("resolve"); });
1043
+ }
1044
+ button(panel, "撤销买家会话访问", function () {
1045
+ if (window.confirm("使同一浏览器会话无法继续查看所有关联工单?此操作不会关闭工单。")) change("revoke", {});
1046
+ });
1047
+ }).catch(function (error) { if (selection === selected) problem(error); });
1048
+ }
1049
+ function load() {
1050
+ call("GET", "/support/cases").then(function (data) {
1051
+ if (!visible()) return;
1052
+ root.textContent = "";
1053
+ if (!data.cases.length) text(root, "p", "没有已登记的工单。");
1054
+ data.cases.forEach(function (item) {
1055
+ button(root, item.case_id + " · " + labels[item.state], function () { detail(item.case_id); });
1056
+ });
1057
+ }).catch(problem);
1058
+ }
1059
+ $("support-refresh").onclick = load;
1060
+ load();
1061
+ }
1062
+
1018
1063
  var views = {
1064
+ support: function () {
1065
+ return Promise.resolve('<section class="card"><h2>人工售后</h2><p>仅本地预览:不验证订单,不代表外部平台处理。</p>' +
1066
+ '<button class="act" type="button" id="support-refresh">刷新待办</button><div id="support-content"></div><div id="support-detail"></div></section>');
1067
+ },
1019
1068
  weixin: function () {
1020
1069
  return Promise.resolve('<div class="card"><h2>个人微信连接</h2>' +
1021
- '<p class="muted">用手机微信扫一扫并确认连接。仅配对的微信账号可以发送 /ping 测试连接,不会开启群发或微信审批。</p>' +
1070
+ '<p class="muted">扫码确认后,只有配对的店主可以与 Kiwi 对话。微信不发送买家消息,也不批准正式操作。</p>' +
1022
1071
  '<div id="weixin-panel"><p role="status">正在读取连接状态…</p></div></div>');
1023
1072
  },
1024
1073
  collection: async function () {
@@ -1051,7 +1100,7 @@ var views = {
1051
1100
  html += '<p class="muted">请设置商家显示名;该名称会用于 Catalog 公开资料。</p>';
1052
1101
  }
1053
1102
  if (failed.indexOf("PRODUCTS_NOT_CONFIGURED") >= 0) {
1054
- html += '<p class="muted">请先导入商品。商品导入后会自动继续服务检查;当前不会报价或发布名片。</p>';
1103
+ html += '<p class="muted">名片可先发布。请在「商品与导入」准备首个可报价 SKU;导入与商品发布是两次确认。</p>';
1055
1104
  }
1056
1105
  if (s.service_state === "OPERATING") {
1057
1106
  html += '<div class="row"><button class="act danger" onclick="pauseService()">暂停接待</button>' +
@@ -1064,7 +1113,7 @@ var views = {
1064
1113
  html += '<p class="muted">恢复接待需要 owner 角色。</p>';
1065
1114
  }
1066
1115
  }
1067
- return appendCatalogConnection(html + "</div>").then(appendCatalogAccess);
1116
+ return appendCatalogSetup(html + "</div>");
1068
1117
  });
1069
1118
  },
1070
1119
  products: function () {
@@ -1237,7 +1286,7 @@ var views = {
1237
1286
  "<tr><th>规则版本</th><td>" + esc(pol.policy_revision) + "</td></tr>" +
1238
1287
  '<tr><th>内容摘要</th><td><pre class="digest">' + esc(pol.digest) + "</pre></td></tr></table>" +
1239
1288
  '<p class="muted">底价等敏感值不出现在本页接口;提交草稿后由系统校验并原子生效。' +
1240
- "上架/下架在目录商家后台设置;商品上架后长期有效。数据有效期只影响自动报价,不改变上架状态。</p></div>" +
1289
+ "上架/下架在Kiwi网关商家后台设置;商品上架后长期有效。数据有效期只影响自动报价,不改变上架状态。</p></div>" +
1241
1290
  (ROLE === "owner" || ROLE === "operator"
1242
1291
  ? '<div class="card"><h2>常用规则表单</h2>' +
1243
1292
  '<p class="muted">留空的全局字段不会写进补丁;按商品规则只提交你填写或勾选清除的行。提交后生成规则变更草稿,再确认才生效。</p>' +
@@ -1635,15 +1684,20 @@ function refreshPublicationStatus() {
1635
1684
  for (var j = 0; j < trs.length; j++) {
1636
1685
  var rowSku = String(trs[j].getAttribute("data-sku") || "");
1637
1686
  var current = published[rowSku];
1638
- if (current) publishedCount += 1;
1687
+ if (String(current || "").toLowerCase() === "active") publishedCount += 1;
1639
1688
  var cell = trs[j].querySelector(".pub-state");
1640
1689
  if (cell) cell.textContent = publicationStateLabel(current, true);
1641
1690
  }
1642
1691
  var summary = $("pub-summary");
1643
1692
  if (summary) {
1644
- summary.textContent = "已发布到目录 " + publishedCount + " 件,未发布 " + Math.max(0, trs.length - publishedCount) + " 件。";
1693
+ summary.textContent = "已发布到Kiwi网关 " + publishedCount + " 件,未发布 " + Math.max(0, trs.length - publishedCount) + " 件。";
1645
1694
  }
1646
- }).catch(function () { bar("目录发布状态暂不可读取,请稍后刷新。", true); });
1695
+ }).catch(function () {
1696
+ document.querySelectorAll("#pub-rows .pub-state").forEach(function (cell) { cell.textContent = "待核验"; });
1697
+ var summary = $("pub-summary");
1698
+ if (summary) summary.textContent = "Kiwi网关商品发布状态待核验。";
1699
+ bar("Kiwi网关发布状态暂不可读取,请稍后刷新。", true);
1700
+ });
1647
1701
  }
1648
1702
  function commitPublication() {
1649
1703
  if (publicationBusy || !publicationDraft) return;
@@ -1760,7 +1814,7 @@ function renderParseReport(r) {
1760
1814
  (defaulted ? '<p class="muted">未提供、按默认值处理的列:' + esc(defaulted) +
1761
1815
  "(状态默认「在售」;更新时间默认取导入时刻)。</p>" : "") +
1762
1816
  '<p class="muted">确认列映射无误后继续;下一步做严格校验与合并预览。' +
1763
- '只会新增或更新文件中的 SKU,现有商品与已发布到目录的商品都不会被删除或撤回。</p>' +
1817
+ '只会新增或更新文件中的 SKU,现有商品与已发布到Kiwi网关的商品都不会被删除或撤回。</p>' +
1764
1818
  '<button class="act primary" id="pcontinue">已确认列映射,继续校验预览</button></div>';
1765
1819
  var btn = $("pcontinue");
1766
1820
  if (btn) btn.addEventListener("click", function () {
@@ -1777,7 +1831,7 @@ function submitImportTable(table) {
1777
1831
  "<div>草稿 <b>" + esc(draft.draft_id) + "</b>:新增 " + pv.added + ",更新 " + pv.updated +
1778
1832
  ",不变 " + pv.unchanged + ",保留现有 " + (pv.kept || 0) + "(文件共 " + pv.rows_total + " 行)。</div>" +
1779
1833
  '<pre class="digest">' + esc(draft.digest) + "</pre>" +
1780
- '<p class="muted">确认后只合并本文件中的 SKU;不会撤回已发布到目录的商品。</p>' +
1834
+ '<p class="muted">确认后只合并本文件中的 SKU;不会撤回已发布到Kiwi网关的商品。</p>' +
1781
1835
  '<button class="act primary" onclick="commitImport()">确认导入(整批生效)</button></div>';
1782
1836
  })
1783
1837
  .catch(function (e) { bar("校验未通过:" + e.message, true); });
@@ -1830,8 +1884,204 @@ function mountWeixin(sequence) {
1830
1884
  var statusTimer;
1831
1885
  var previousState = "";
1832
1886
  var busy = false;
1887
+ var selectedDraft = new URLSearchParams(window.location.search).get("weixin_draft") || "";
1888
+ var draftsLoaded = false;
1889
+ var latestPreview;
1890
+ var latestProposal;
1891
+ var rulesLoaded = false;
1833
1892
  function active() { return current === "weixin" && sequence === viewSequence; }
1834
1893
  function problem(e) { if (active()) bar(e.message, true); }
1894
+ function ownerAction(action, prompt, fields) {
1895
+ if (prompt && !window.confirm(prompt)) return;
1896
+ call("POST", endpoint + "/" + action, fields || {}).then(function () {
1897
+ if (!active()) return;
1898
+ draftsLoaded = false;
1899
+ reset();
1900
+ }).catch(problem);
1901
+ }
1902
+ function renderConversation(conversation, configured, operationsAvailable) {
1903
+ var area = $("weixin-owner-preview");
1904
+ if (!area || !conversation || !conversation.enabled) return;
1905
+ var direct = conversation.delivery === "paired_owner";
1906
+ var agent = conversation.agent;
1907
+ var modelReady = agent && agent.state === "configured";
1908
+ var modelUnavailable = agent && agent.state === "unavailable";
1909
+ var modelService = modelReady ? agent.provider + " / " + agent.model + "(" + agent.endpoint + ")" : "";
1910
+ var title = $("weixin-conversation-title");
1911
+ if (title) title.textContent = modelReady ? "店主微信对话 · Kiwi 智能体" : direct ? "店主微信对话" : "店主对话 · 工作台预览";
1912
+ var preview = $("weixin-preview-text");
1913
+ if (preview) preview.textContent = conversation.unavailable ? "店主对话暂不可用。" :
1914
+ modelUnavailable ? "对话模型配置不可用,未启用模型回信。" :
1915
+ modelReady ? (conversation.authorized
1916
+ ? "打开微信与 Kiwi 自然对话。模型服务:" + modelService + (conversation.unknown_count > 0 ? " 有回信结果无法确认,系统不会自动重发。" : "")
1917
+ : "微信扫码不等于模型授权。请确认是否允许把你的私聊和有限近期上下文交给 " + modelService + " 处理。不要在聊天中发送密码、密钥或客户隐私。") :
1918
+ direct ? (conversation.authorized
1919
+ ? "打开微信查询待跟进询价或预览草稿。此模式不调用大模型。" + (conversation.unknown_count > 0 ? " 有回信结果无法确认,系统不会自动重发。" : "")
1920
+ : "现有扫码绑定还未授权微信回信,请在下方确认。") :
1921
+ conversation.preview || (conversation.authorized
1922
+ ? "可发送:今天哪三笔询价最该跟?或查库存 SKU-001。结果仅在本工作台显示。"
1923
+ : "当前绑定未授权业务预览。");
1924
+ var controls = $("weixin-conversation-controls");
1925
+ if (controls) {
1926
+ controls.innerHTML = "";
1927
+ function action(label, name, confirmation, fields) {
1928
+ var b = document.createElement("button");
1929
+ b.type = "button"; b.className = "act"; b.textContent = label;
1930
+ b.onclick = function () { ownerAction(name, confirmation, fields); };
1931
+ controls.appendChild(b);
1932
+ }
1933
+ if (conversation.authorized) {
1934
+ action("清空对话", "clear-conversation", "清空对话和未出站结果?已落库草稿保留。");
1935
+ action(direct ? "停止微信对话" : "停止预览", "revoke-conversation", "停止店主对话?微信 /ping 继续可用。");
1936
+ } else if (configured && !conversation.unavailable && !modelUnavailable) {
1937
+ if (modelReady) action("授权 Kiwi 模型对话", "confirm-conversation",
1938
+ "允许将你发送的微信私聊正文及最近最多 4 轮对话文本提交给 " + modelService + "?Kiwi 不会自动附上买家原文、身份或商家查询明细。请勿发送密码、密钥或客户隐私。",
1939
+ { model_consent: agent.consent });
1940
+ else action(direct ? "授权微信回信" : "确认店主归属", "confirm-conversation", direct
1941
+ ? "仅允许向当前扫码绑定的店主账号回复本人的私聊。确认开启?" : undefined);
1942
+ }
1943
+ }
1944
+ if ((!draftsLoaded || latestPreview !== conversation.preview) && (configured || selectedDraft)) {
1945
+ var first = !draftsLoaded;
1946
+ latestPreview = conversation.preview;
1947
+ loadDrafts();
1948
+ if (first && selectedDraft) loadDraft(selectedDraft);
1949
+ }
1950
+ renderDigest(conversation.rule_proposal, operationsAvailable);
1951
+ }
1952
+ function renderDigest(proposal, available) {
1953
+ var target = $("weixin-digest-proposal");
1954
+ if (!target) return;
1955
+ var key = proposal ? JSON.stringify(proposal) : "";
1956
+ if (latestProposal !== key) {
1957
+ latestProposal = key;
1958
+ target.textContent = "";
1959
+ if (proposal) {
1960
+ var stamp = proposal.prepare.trigger_spec;
1961
+ var detail = document.createElement("p");
1962
+ detail.textContent = "待回复询价日报 · 周一至周五 " + stamp.local_time + "(" + stamp.timezone +
1963
+ ") · 经营投影 · 仅工作台 · 起草时预计下次 " + proposal.next_due_at;
1964
+ target.appendChild(detail);
1965
+ if (available) {
1966
+ var button = document.createElement("button");
1967
+ button.type = "button"; button.className = "act"; button.textContent = "确认启用日报";
1968
+ button.onclick = function () {
1969
+ if (!window.confirm("启用这个规则?日报只出现在工作台。")) return;
1970
+ button.disabled = true;
1971
+ call("POST", "/automation/rules/prepare", proposal.prepare).then(function (candidate) {
1972
+ return call("POST", "/automation/rules/activate", { candidate_ref: candidate.candidate_ref });
1973
+ }).then(function () { if (active()) { bar("日报规则已启用。", false); loadRules(); } }).catch(problem);
1974
+ };
1975
+ target.appendChild(button);
1976
+ } else {
1977
+ var hint = document.createElement("p"); hint.textContent = "此实例未装配经营调度,规则不能启用。";
1978
+ target.appendChild(hint);
1979
+ }
1980
+ }
1981
+ }
1982
+ if (!rulesLoaded && available) loadRules();
1983
+ }
1984
+ function loadRules() {
1985
+ rulesLoaded = true;
1986
+ call("GET", "/automation/rules").then(function (data) {
1987
+ var list = $("weixin-digest-rules");
1988
+ if (!active() || !list) return;
1989
+ list.textContent = "";
1990
+ var rules = data.rules.filter(function (rule) {
1991
+ return rule.template === "unfinished_inquiry_digest" && rule.action_type === "internal_summary" && rule.trigger_spec.day_filter === "weekdays";
1992
+ });
1993
+ rules.slice(0, 20).forEach(function (rule) {
1994
+ var paused = rule.state === "PAUSED";
1995
+ var row = document.createElement("p");
1996
+ row.textContent = rule.trigger_spec.local_time + "(" + rule.timezone + ") · " +
1997
+ (paused ? "已暂停" : "下次:" + (rule.next_due_at || "无")) + " ";
1998
+ var action = document.createElement("button");
1999
+ action.type = "button"; action.className = "act"; action.textContent = paused ? "恢复" : "暂停";
2000
+ action.onclick = function () {
2001
+ if (!window.confirm(paused ? "按新的有效时间恢复日报?" : "暂停日报规则?")) return;
2002
+ action.disabled = true;
2003
+ call("POST", "/automation/rules/" + encodeURIComponent(rule.rule_id) + "/" + (paused ? "resume" : "pause"), paused
2004
+ ? { expected_version: rule.version, expected_enable_epoch: rule.enable_epoch }
2005
+ : { expected_version: rule.version, reason: "owner_workbench" }).then(loadRules).catch(problem);
2006
+ };
2007
+ row.appendChild(action); list.appendChild(row);
2008
+ });
2009
+ var digestIds = new Set(rules.map(function (rule) { return rule.rule_id; }));
2010
+ return call("GET", "/automation/artifacts").then(function (result) {
2011
+ var results = $("weixin-digest-results");
2012
+ if (!active() || !results) return;
2013
+ results.textContent = "";
2014
+ result.artifacts.filter(function (artifact) { return digestIds.has(artifact.rule_id); }).slice(0, 5)
2015
+ .forEach(function (artifact) {
2016
+ var advice = artifact.payload.advice || {};
2017
+ var row = document.createElement("p");
2018
+ row.textContent = artifact.created_at + " · " + (advice.summary || artifact.payload.summary || "已记录经营任务结果") + " · " + artifact.receipt_ref;
2019
+ results.appendChild(row);
2020
+ });
2021
+ });
2022
+ }).catch(problem);
2023
+ }
2024
+ function loadPreference() {
2025
+ var target = $("weixin-followup-preference");
2026
+ if (!target) return;
2027
+ call("GET", endpoint + "/followup-preference").then(function (preference) {
2028
+ if (!active() || target !== $("weixin-followup-preference")) return;
2029
+ target.innerHTML = '<label>普通跟进语气 <select id="weixin-style"><option value="">默认(撤回偏好)</option>' +
2030
+ '<option value="concise">简洁</option><option value="warm">友好</option></select></label> ' +
2031
+ '<button type="button" class="act" id="weixin-style-save">确认偏好</button>';
2032
+ var style = $("weixin-style");
2033
+ var save = $("weixin-style-save");
2034
+ style.value = preference.style || "";
2035
+ save.onclick = function () {
2036
+ save.disabled = true;
2037
+ call("POST", endpoint + "/followup-preference", { version: preference.version, style: style.value || null })
2038
+ .then(function () { if (active()) { bar("偏好已更新。正式报价规则未更改。", false); loadPreference(); } })
2039
+ .catch(problem).finally(function () { save.disabled = false; });
2040
+ };
2041
+ }).catch(problem);
2042
+ }
2043
+ function loadDrafts() {
2044
+ draftsLoaded = true;
2045
+ return call("GET", endpoint + "/reply-drafts").then(function (data) {
2046
+ if (!active() || !$("weixin-draft-list")) return;
2047
+ var list = $("weixin-draft-list");
2048
+ list.textContent = data.items.length ? "" : "没有已保存的回复草稿。";
2049
+ data.items.forEach(function (draft) {
2050
+ var b = document.createElement("button");
2051
+ b.type = "button"; b.className = "act"; b.textContent = draft.inquiry + " · v" + draft.version;
2052
+ b.onclick = function () { loadDraft(draft.id); };
2053
+ list.appendChild(b);
2054
+ });
2055
+ }).catch(problem);
2056
+ }
2057
+ function loadDraft(id) {
2058
+ if (!/^[a-f0-9-]{36}$/.test(id)) return;
2059
+ selectedDraft = id;
2060
+ call("GET", endpoint + "/reply-drafts/" + id).then(function (draft) {
2061
+ var target = $("weixin-draft-editor");
2062
+ if (!active() || !target || selectedDraft !== id) return;
2063
+ target.innerHTML = '<p>询价:' + esc(draft.inquiry) + ' · 草稿 v' + esc(draft.version) + '</p>' +
2064
+ '<p>仅保存到工作台。正式发布未接通,不会发送给买家。' +
2065
+ (draft.current ? '' : ' 询价已变化或无法核实,此稿不可编辑。') + '</p>' +
2066
+ '<form id="weixin-draft-form"><label>回复正文 <textarea id="weixin-draft-body" maxlength="600" required></textarea></label> ' +
2067
+ '<button class="act" type="submit">保存草稿</button></form>';
2068
+ var body = $("weixin-draft-body");
2069
+ body.value = draft.body;
2070
+ var form = $("weixin-draft-form");
2071
+ var save = form.querySelector("button");
2072
+ if (!draft.current) { body.readOnly = true; save.disabled = true; }
2073
+ form.onsubmit = function (event) {
2074
+ event.preventDefault();
2075
+ if (save.disabled) return;
2076
+ save.disabled = true;
2077
+ call("POST", endpoint + "/reply-drafts/" + id, { version: draft.version, body: body.value }).then(function () {
2078
+ if (!active()) return;
2079
+ bar("草稿已保存,未发送。", false);
2080
+ loadDrafts(); loadDraft(id);
2081
+ }).catch(problem).finally(function () { if (draft.current) save.disabled = false; });
2082
+ };
2083
+ }).catch(problem);
2084
+ }
1835
2085
  function reset() {
1836
2086
  clearTimeout(expiryTimer);
1837
2087
  clearTimeout(statusTimer);
@@ -1857,10 +2107,22 @@ function mountWeixin(sequence) {
1857
2107
  panel.innerHTML = '<p role="status">' + esc(labels[state] || "连接状态未知") + '</p>' +
1858
2108
  (connection.configured
1859
2109
  ? '<button type="button" class="act" id="weixin-disconnect">断开连接</button>'
1860
- : '<button type="button" class="act primary" id="weixin-begin">生成微信二维码</button>');
2110
+ : '<button type="button" class="act primary" id="weixin-begin">生成微信二维码</button>') +
2111
+ (connection.conversation && connection.conversation.enabled
2112
+ ? '<section id="weixin-owner-preview"><h3 id="weixin-conversation-title">店主对话</h3>' +
2113
+ '<pre id="weixin-preview-text" style="white-space:pre-wrap"></pre><div id="weixin-conversation-controls"></div>' +
2114
+ '<h3>普通跟进草稿</h3><div id="weixin-draft-list"></div><div id="weixin-draft-editor"></div>' +
2115
+ '<div id="weixin-followup-preference"></div>' +
2116
+ '<div id="weixin-digest-proposal"></div><div id="weixin-digest-rules"></div><div id="weixin-digest-results"></div></section>'
2117
+ : '');
1861
2118
  if (connection.configured) $("weixin-disconnect").onclick = disconnect;
1862
2119
  else $("weixin-begin").onclick = begin;
2120
+ draftsLoaded = false;
2121
+ rulesLoaded = false;
2122
+ latestProposal = undefined;
2123
+ if (connection.configured && connection.conversation && connection.conversation.enabled) loadPreference();
1863
2124
  }
2125
+ renderConversation(connection.conversation, connection.configured, connection.operations_available);
1864
2126
  if (state === "checking" || state === "retrying" || state === "connected") statusTimer = setTimeout(updateStatus, 4000);
1865
2127
  }).catch(function (e) {
1866
2128
  if (!active()) return;
@@ -1968,7 +2230,7 @@ function mountWeixin(sequence) {
1968
2230
  }
1969
2231
  var current = "status";
1970
2232
  var VIEW_COPY = {
1971
- status: ["服务状态", "掌握接待状态,管理店铺与目录连接。"],
2233
+ status: ["服务状态", "掌握接待状态,管理店铺与名片发布。"],
1972
2234
  products: ["商品与导入", "维护商品资料,先预览,再确认变更。"],
1973
2235
  approvals: ["待审批", "核对每一项变更,重要决定由你确认。"],
1974
2236
  negotiations: ["会话旁观", "查看询价与磋商进展,掌握需要关注的会话。"],
@@ -1978,7 +2240,8 @@ var VIEW_COPY = {
1978
2240
  operating: ["商品经营", "选择商品、设定条件与授权,控制生效区间。"],
1979
2241
  sales: ["销售工作台", "处理待办、下一动作与持久跟进。"],
1980
2242
  outcomes: ["经营结果", "分开查看订单证据、归因与贡献。"],
1981
- weixin: ["微信通道", "连接店主的个人微信,查看连接状态。"]
2243
+ weixin: ["微信通道", "连接店主的个人微信,查看连接状态。"],
2244
+ support: ["售后工单", "领取本地售后工单并保存人工回访。"]
1982
2245
  };
1983
2246
  function decorateView() {
1984
2247
  var view = $("view");
@@ -2014,7 +2277,8 @@ function refresh() {
2014
2277
  var sequence = ++viewSequence;
2015
2278
  var copy = VIEW_COPY[current];
2016
2279
  var view = $("view");
2017
- if (previousView === "collection" || current === "collection" || previousView === "weixin" || current === "weixin") {
2280
+ if (previousView === "collection" || current === "collection" || previousView === "weixin" || current === "weixin" ||
2281
+ previousView === "support" || current === "support") {
2018
2282
  view.innerHTML = '<div class="card">加载中…</div>';
2019
2283
  }
2020
2284
  previousView = current;
@@ -2026,13 +2290,14 @@ function refresh() {
2026
2290
  view.innerHTML = html; decorateView();
2027
2291
  if (current === "weixin") mountWeixin(sequence);
2028
2292
  if (current === "collection") { bindAlipayCollection(); bindAlipayApprovals(); }
2293
+ if (current === "support") mountSupport(sequence);
2029
2294
  if (current === "status" && CATALOG_CONNECT && ROLE === "owner") {
2295
+ if (catalogConnectionState && waitingForCatalogConfirmation(catalogConnectionState)) showCatalogPairing(sequence);
2030
2296
  if (catalogConnectionState && catalogConnectionState.bindingId) {
2031
2297
  call("GET", "/catalog/bound-account").then(function (result) {
2032
- var target = $("bound-account"); if (target) target.textContent = result.email || "无法确定,请到 Kiwi 商家门户核对";
2033
- }).catch(function () { var target = $("bound-account"); if (target) target.textContent = "暂时无法读取,请稍后刷新"; });
2034
- // 后台刷新只更新局部状态,不覆盖全局提示条。
2035
- refreshPublicInquiry(false);
2298
+ var target = $("bound-account"); if (sequence === viewSequence && target) target.textContent = result.email || "请到 Kiwi 商家门户核对";
2299
+ }).catch(function () { var target = $("bound-account"); if (sequence === viewSequence && target) target.textContent = "暂时无法读取,请稍后刷新"; });
2300
+ if (catalogConnectionState.published) refreshPublicInquiry(sequence);
2036
2301
  }
2037
2302
  }
2038
2303
  })
@@ -2053,9 +2318,16 @@ fetch("/merchant/api/session", { credentials: "same-origin" }).then(function (re
2053
2318
  if (!session) return;
2054
2319
  CSRF = session.csrf_token || "";
2055
2320
  ROLE = session.role || "";
2056
- if (WEIXIN_CHANNEL && ROLE === "owner") $("weixin-tab").hidden = false;
2057
2321
  $("collection-tab").hidden = session.can_manage_alipay_collection !== true;
2058
2322
  $("who").textContent = "商家 " + session.merchant_id + " · " + ROLE;
2323
+ if (MANUAL_SUPPORT && ROLE === "owner") $("support-tab").hidden = false;
2324
+ if (WEIXIN_CHANNEL && ROLE === "owner") {
2325
+ $("weixin-tab").hidden = false;
2326
+ if (new URLSearchParams(window.location.search).has("weixin_draft")) {
2327
+ $("weixin-tab").click();
2328
+ return;
2329
+ }
2330
+ }
2059
2331
  refresh();
2060
2332
  });
2061
2333
  </script>