trustbase-seller-backend 0.1.11 → 0.1.12

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "trustbase-seller-backend",
3
- "version": "0.1.11",
3
+ "version": "0.1.12",
4
4
  "description": "TrustBase Seller Backend (merchant edition) — 商品上链/订单/支付/质押 write API for TrustBase 保障链生态. 单卖家自托管形态: 签名 key 即卖家身份, 私钥不出门.",
5
5
  "main": "run-unified.js",
6
6
  "bin": {
@@ -89,10 +89,12 @@ const EXEC_TIMEOUT_MS = 30_000;
89
89
  class HttpError extends Error {
90
90
  status;
91
91
  code;
92
- constructor(status, code, message) {
92
+ field;
93
+ constructor(status, code, message, field) {
93
94
  super(message);
94
95
  this.status = status;
95
96
  this.code = code;
97
+ this.field = field; // 可选: 出错字段名 (前端定位高亮用, 2026-10-03 进件表单错误提示)
96
98
  }
97
99
  }
98
100
  exports.HttpError = HttpError;
@@ -650,7 +652,7 @@ function createChainWritesRouter(deps) {
650
652
  }
651
653
  function handleError(res, e, tag) {
652
654
  if (e instanceof HttpError) {
653
- return res.status(e.status).json({ error: e.code, message: e.message });
655
+ return res.status(e.status).json({ error: e.code, message: e.message, ...(e.field ? { field: e.field } : {}) });
654
656
  }
655
657
  console.error(`[chain-writes] ${tag} 失败:`, e?.message ?? e);
656
658
  return res.status(502).json({ error: 'chain_write_error', message: e?.message || String(e) });
@@ -85,9 +85,23 @@ img.auprev{display:none;width:100%;max-height:140px;object-fit:cover;border-radi
85
85
  #apply-status[data-state="pending"]{background:#FFF7ED;border-color:#FED7AA}
86
86
  #apply-status a{display:inline-block;background:#C4633C;color:#fff !important;border-radius:8px;padding:10px 18px;font-size:15px;font-weight:600;text-decoration:none;margin-top:8px}
87
87
  #apply-status .aqr{display:flex;align-items:center;justify-content:center;width:120px;height:120px;border:2px dashed #93C5FD;border-radius:10px;background:#fff;color:#8A857B;font-size:12px;text-align:center;margin:10px 0 2px;line-height:1.6}
88
+ /* 经营场景选择卡 (2026-10-03 三场景) */
89
+ #apply-form .ascene-row{display:grid;grid-template-columns:repeat(3,1fr);gap:10px}
90
+ #apply-form .ascene{display:flex;flex-direction:column;align-items:center;gap:2px;border:2px solid #EAE3D6;border-radius:12px;background:#FFFDF9;padding:14px 8px;cursor:pointer;text-align:center;transition:border-color .15s,background .15s}
91
+ #apply-form .ascene input{display:none}
92
+ #apply-form .ascene .ascene-ico{font-size:24px;line-height:1}
93
+ #apply-form .ascene .ascene-name{font-size:14px;font-weight:600;color:#33302B}
94
+ #apply-form .ascene .ascene-sub{font-size:11px;color:#8A857B;line-height:1.5}
95
+ #apply-form .ascene.selected{border-color:#C4633C;background:#FBF1EA;box-shadow:0 0 0 3px rgba(196,99,60,.12)}
96
+ #apply-form .ahint{font-size:12px;color:#B45309;background:#FFFBEB;border:1px solid #FDE68A;border-radius:8px;padding:8px 10px;margin-top:10px}
97
+ /* 错误提示红卡 + 字段高亮 (2026-10-03 xiaomu 实测 "没反应" fix) */
98
+ #apply-err{background:#FEF2F2;border:1px solid #FCA5A5;border-left:4px solid #DC2626;border-radius:10px;padding:12px 14px;font-size:14px;color:#991B1B;margin:4px 0 12px;line-height:1.8;word-break:break-all}
99
+ #apply-form .aerr-field,#apply-form input.aerr-field,#apply-form select.aerr-field{border-color:#DC2626 !important;box-shadow:0 0 0 3px rgba(220,38,38,.18) !important}
100
+ #apply-form div[id^="a_up_"].aerr-field label.sbtn{border-color:#DC2626;background:#FEF2F2}
88
101
  @media (max-width:640px){
89
102
  #apply-form .agrid{grid-template-columns:1fr}
90
103
  #apply-form .auploads{grid-template-columns:1fr}
104
+ #apply-form .ascene-row{grid-template-columns:1fr}
91
105
  }
92
106
  @media print{
93
107
  body *{visibility:hidden}
@@ -128,6 +142,18 @@ img.auprev{display:none;width:100%;max-height:140px;object-fit:cover;border-radi
128
142
  <div id="apply-form" style="display:none;margin-top:12px">
129
143
  <div class="desc" style="margin-bottom:12px">以下资料由你的电脑<b>本地加密</b>后交给服务商,向微信申请特约商户收款账户。身份证号/银行卡号在你电脑上就已加密,任何人 (含服务商) 都看不到明文。</div>
130
144
 
145
+ <div class="acard">
146
+ <div class="acard-h">经营场景 <span class="aopt">三选一, 决定第 4/5 张卡片要什么资料</span></div>
147
+ <div class="ascene-row">
148
+ <label class="ascene selected"><input type="radio" name="sales_scene" value="SALES_SCENES_STORE" checked onchange="setScene(this.value)">
149
+ <span class="ascene-ico">🏬</span><span class="ascene-name">线下门店</span><span class="ascene-sub">门店地址 + 门头/店内照片</span></label>
150
+ <label class="ascene"><input type="radio" name="sales_scene" value="SALES_SCENES_WEB" onchange="setScene(this.value)">
151
+ <span class="ascene-ico">🌐</span><span class="ascene-name">互联网网站</span><span class="ascene-sub">网址 + 截图, 需 ICP 备案</span></label>
152
+ <label class="ascene"><input type="radio" name="sales_scene" value="SALES_SCENES_MP" onchange="setScene(this.value)">
153
+ <span class="ascene-ico">📱</span><span class="ascene-name">公众号/小程序</span><span class="ascene-sub">AppID + 页面截图</span></label>
154
+ </div>
155
+ </div>
156
+
131
157
  <div class="acard">
132
158
  <div class="acard-h"><span class="anum">1</span>主体资料</div>
133
159
  <div class="agrid">
@@ -190,8 +216,8 @@ img.auprev{display:none;width:100%;max-height:140px;object-fit:cover;border-radi
190
216
  </div>
191
217
 
192
218
  <div class="acard">
193
- <div class="acard-h"><span class="anum">4</span>门店信息</div>
194
- <div class="agrid">
219
+ <div class="acard-h"><span class="anum">4</span><span id="acard4-title">门店信息</span></div>
220
+ <div class="agrid" id="scene_fields_store">
195
221
  <div class="afield"><label for="a_store_name">门店名称</label><input id="a_store_name" placeholder="线下场所名称"></div>
196
222
  <div class="afield"><label for="a_store_address_code">门店省市编码</label>
197
223
  <select id="a_store_address_code" data-citycodes>
@@ -201,6 +227,20 @@ img.auprev{display:none;width:100%;max-height:140px;object-fit:cover;border-radi
201
227
  <input id="a_storecode_manual" class="amanual" placeholder="6 位国标区划码, 如: 320200" style="display:none"></div>
202
228
  <div class="afield afield-full"><label for="a_store_address">门店详细地址</label><input id="a_store_address" placeholder="xx 路 xx 号"></div>
203
229
  </div>
230
+ <div id="scene_fields_web" style="display:none">
231
+ <div class="agrid">
232
+ <div class="afield afield-full"><label for="a_website_url">网站网址</label><input id="a_website_url" placeholder="https:// 开头, 需已上线可访问"></div>
233
+ </div>
234
+ <div class="ahint">⚠️ 网站需已上线且有 ICP 备案, 微信审核会实际访问验证</div>
235
+ </div>
236
+ <div id="scene_fields_mp" style="display:none">
237
+ <div class="agrid">
238
+ <div class="afield"><label for="a_appid">公众号/小程序 AppID</label><input id="a_appid" placeholder="wx 开头"></div>
239
+ <div class="afield"><label for="a_appid_type">类型</label>
240
+ <select id="a_appid_type"><option value="mp">公众号</option><option value="mini">小程序</option></select></div>
241
+ </div>
242
+ <div class="ahint">⚠️ 需已认证的公众号或小程序, 且主体与本商户一致</div>
243
+ </div>
204
244
  </div>
205
245
 
206
246
  <div class="acard">
@@ -209,11 +249,12 @@ img.auprev{display:none;width:100%;max-height:140px;object-fit:cover;border-radi
209
249
  <div id="a_up_license"></div>
210
250
  <div id="a_up_idfront"></div>
211
251
  <div id="a_up_idback"></div>
212
- <div id="a_up_entrance"></div>
213
- <div id="a_up_indoor"></div>
252
+ <div id="a_up_s1"></div>
253
+ <div id="a_up_s2"></div>
214
254
  </div>
215
255
  </div>
216
256
 
257
+ <div id="apply-err" style="display:none"></div>
217
258
  <button class="abtn" id="btn-apply-submit" onclick="submitApplyment()">加密并提交进件</button>
218
259
  <div class="aprivacy">🔒 资料只保存在这台电脑; 敏感字段本机加密后交服务商; 服务商暂时不可达时加密暂存本机, 可重试。</div>
219
260
  </div>
@@ -408,11 +449,36 @@ function mountApplyUpload(slotId,field,label){
408
449
  mountApplyUpload('a_up_license','license_media_id','营业执照照片 (必传)');
409
450
  mountApplyUpload('a_up_idfront','legal_id_front_media_id','身份证人像面 (必传)');
410
451
  mountApplyUpload('a_up_idback','legal_id_back_media_id','身份证国徽面 (必传)');
411
- mountApplyUpload('a_up_entrance','store_entrance_media_id','门店门头照片 (必传)');
412
- mountApplyUpload('a_up_indoor','store_indoor_media_id','门店内部照片 (必传)');
452
+ // 经营场景 (2026-10-03 xiaomu 拍板三选一): 场景决定第 4 卡片字段 + 第 5 卡片场景照片槽
453
+ var SCENE_UPLOADS={
454
+ SALES_SCENES_STORE:[['a_up_s1','store_entrance_media_id','门店门头照片 (必传)'],['a_up_s2','store_indoor_media_id','门店内部照片 (必传)']],
455
+ SALES_SCENES_WEB:[['a_up_s1','web_home_media_id','网站首页截图 (必传)'],['a_up_s2','web_product_media_id','商品/服务页截图 (必传)']],
456
+ SALES_SCENES_MP:[['a_up_s1','mp_media_id','公众号/小程序页面截图 (必传)']]
457
+ };
458
+ var SCENE_TITLES={SALES_SCENES_STORE:'门店信息',SALES_SCENES_WEB:'网站信息',SALES_SCENES_MP:'公众号 / 小程序'};
459
+ function currentScene(){
460
+ var r=document.querySelector('input[name="sales_scene"]:checked');
461
+ return r?r.value:'SALES_SCENES_STORE';
462
+ }
463
+ function setScene(scene){
464
+ document.getElementById('scene_fields_store').style.display=scene==='SALES_SCENES_STORE'?'':'none';
465
+ document.getElementById('scene_fields_web').style.display=scene==='SALES_SCENES_WEB'?'':'none';
466
+ document.getElementById('scene_fields_mp').style.display=scene==='SALES_SCENES_MP'?'':'none';
467
+ document.getElementById('acard4-title').textContent=SCENE_TITLES[scene]||'门店信息';
468
+ document.querySelectorAll('.ascene').forEach(function(c){
469
+ c.classList.toggle('selected', c.querySelector('input').value===scene);
470
+ });
471
+ // 场景照片槽重挂 + 清掉旧场景 media_id (避免跨场景串数据)
472
+ ['store_entrance_media_id','store_indoor_media_id','web_home_media_id','web_product_media_id','mp_media_id'].forEach(function(k){delete _aMedia[k];});
473
+ var slots=SCENE_UPLOADS[scene]||SCENE_UPLOADS.SALES_SCENES_STORE;
474
+ slots.forEach(function(s){var el=document.getElementById(s[0]);el.style.display='';mountApplyUpload(s[0],s[1],s[2]);});
475
+ if(!slots.some(function(s){return s[0]==='a_up_s2';})) document.getElementById('a_up_s2').style.display='none';
476
+ }
477
+ setScene('SALES_SCENES_STORE'); // 默认线下门店
413
478
  function aval(id){var e=document.getElementById(id);return e?e.value.trim():''}
414
479
  function applyFormBody(){
415
- return {
480
+ var scene=currentScene();
481
+ var body={
416
482
  subject_type:aval('a_subject_type'),merchant_shortname:aval('a_shortname'),merchant_name:aval('a_merchant_name'),
417
483
  license_number:aval('a_license_number'),service_phone:aval('a_service_phone'),
418
484
  settlement_id:aval('a_settlement_id'),qualification_type:aval('a_qualification_type'),
@@ -421,12 +487,48 @@ function applyFormBody(){
421
487
  legal_id_period_begin:aval('a_period_begin'),legal_id_period_end:aval('a_period_end'),
422
488
  account_type:aval('a_account_type'),account_name:aval('a_account_name'),account_number:aval('a_account_number'),
423
489
  account_bank:aval('a_account_bank'),bank_name:aval('a_bank_name'),bank_address_code:aval('a_bank_address_code'),
424
- store_name:aval('a_store_name'),store_address_code:aval('a_store_address_code'),store_address:aval('a_store_address'),
490
+ sales_scene:scene,
425
491
  license_media_id:_aMedia.license_media_id||'',legal_id_front_media_id:_aMedia.legal_id_front_media_id||'',
426
- legal_id_back_media_id:_aMedia.legal_id_back_media_id||'',store_entrance_media_id:_aMedia.store_entrance_media_id||'',
427
- store_indoor_media_id:_aMedia.store_indoor_media_id||''
492
+ legal_id_back_media_id:_aMedia.legal_id_back_media_id||''
428
493
  };
494
+ if(scene==='SALES_SCENES_STORE'){
495
+ body.store_name=aval('a_store_name');body.store_address_code=aval('a_store_address_code');body.store_address=aval('a_store_address');
496
+ body.store_entrance_media_id=_aMedia.store_entrance_media_id||'';body.store_indoor_media_id=_aMedia.store_indoor_media_id||'';
497
+ }else if(scene==='SALES_SCENES_WEB'){
498
+ body.website_url=aval('a_website_url');
499
+ body.web_home_media_id=_aMedia.web_home_media_id||'';body.web_product_media_id=_aMedia.web_product_media_id||'';
500
+ }else{
501
+ body.appid=aval('a_appid');body.appid_type=aval('a_appid_type');
502
+ body.mp_media_id=_aMedia.mp_media_id||'';
503
+ }
504
+ return body;
505
+ }
506
+ // 错误提示 (2026-10-03 硬性要求): 红卡 + 平滑滚动 + 字段级红框定位
507
+ var FIELD_EL={ // 字段名 → 页面元素 id (媒体字段 → 上传槽; 文本字段默认 a_+字段)
508
+ license_media_id:'a_up_license',legal_id_front_media_id:'a_up_idfront',legal_id_back_media_id:'a_up_idback',
509
+ store_entrance_media_id:'a_up_s1',store_indoor_media_id:'a_up_s2',
510
+ web_home_media_id:'a_up_s1',web_product_media_id:'a_up_s2',mp_media_id:'a_up_s1',
511
+ website_url:'a_website_url',appid:'a_appid'
512
+ };
513
+ function showApplyErr(msg,field){
514
+ var card=document.getElementById('apply-err');
515
+ card.textContent='⚠️ '+msg;
516
+ card.style.display='block';
517
+ // 清旧高亮
518
+ document.querySelectorAll('#apply-form .aerr-field').forEach(function(e){e.classList.remove('aerr-field');});
519
+ var t=null;
520
+ if(field){
521
+ // 手填接管 id 的场景 (其他银行/手动编码): getElementById 拿到的就是当前持有 id 的可见控件
522
+ t=document.getElementById(FIELD_EL[field]||('a_'+field));
523
+ }
524
+ if(t){
525
+ t.classList.add('aerr-field');
526
+ t.scrollIntoView({behavior:'smooth',block:'center'}); // 滚动到字段, 提示卡跟随在提交按钮上方
527
+ }else{
528
+ card.scrollIntoView({behavior:'smooth',block:'center'});
529
+ }
429
530
  }
531
+ function hideApplyErr(){document.getElementById('apply-err').style.display='none';}
430
532
  // 状态机渲染: 未提交(表单) → 已提交(business_code+轮询) → TO_BE_SIGNED(签约链接) → FINISHED(sub_mchid+引导金)
431
533
  function renderApplyment(j){
432
534
  var box=document.getElementById('apply-status');
@@ -451,20 +553,33 @@ function renderApplyment(j){
451
553
  async function submitApplyment(){
452
554
  var btn=document.getElementById('btn-apply-submit');
453
555
  var msg=document.getElementById('apply-msg');
454
- btn.disabled=true;btn.textContent='加密并提交中...';
556
+ hideApplyErr();
557
+ btn.disabled=true;btn.textContent='加密并提交中…';
558
+ // 前端 40s 兜底超时 (后端 SP 转发自身有 15s 超时; AbortController 防页面卡死)
559
+ var ctrl=new AbortController();
560
+ var timer=setTimeout(function(){ctrl.abort();},40000);
455
561
  try{
456
- var j=await readJson(await fetch('/api/onboard/applyment',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(applyFormBody())}));
562
+ var j=await readJson(await fetch('/api/onboard/applyment',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(applyFormBody()),signal:ctrl.signal}));
563
+ clearTimeout(timer);
457
564
  if(j.ok){
458
565
  msg.style.display='block';
459
566
  msg.textContent='✅ 已提交微信审核 (敏感字段已在本机加密)'+(j.idempotent?' — 此前已提交过, 幂等忽略':'');
460
567
  document.getElementById('apply-form').style.display='none';
461
568
  queryApplyment();
462
569
  } else {
463
- msg.style.display='block';
464
- msg.textContent='提交失败: '+(j.message||j.error||'未知错误');
570
+ // 后端/SP/微信错误 → 醒目红卡 + 字段定位 (message 已是中文, 含字段名)
571
+ showApplyErr(j.message||j.error||'未知错误', j.field);
465
572
  loadApplymentStatus(); // PENDING_PUSH 时状态框给出重试按钮
466
573
  }
467
- }catch(e){msg.style.display='block';msg.textContent='提交失败: 服务暂时不可用,请刷新重试 ('+e.message+')';}
574
+ }catch(e){
575
+ clearTimeout(timer);
576
+ if(e && e.name==='AbortError'){
577
+ showApplyErr('服务商响应超时,资料已加密暂存本机,可点重试');
578
+ loadApplymentStatus();
579
+ }else{
580
+ showApplyErr('提交失败: 服务暂时不可用,请刷新重试 ('+e.message+')');
581
+ }
582
+ }
468
583
  btn.disabled=false;btn.textContent='加密并提交进件';
469
584
  }
470
585
  async function retryApplyment(){
package/routes/onboard.js CHANGED
@@ -460,12 +460,31 @@ function createOnboardRouter(deps) {
460
460
  /** 在卖家本机就要加密的敏感字段 (与 sp-applyment buildApplymentBody 加密清单一致;
461
461
  * 注意 legal_name 不在内: 营业执照 legal_person 按微信文档是明文, 加密版单独走 legal_name_cipher) */
462
462
  const APPLY_SENSITIVE = ["contact_name", "contact_mobile", "contact_email", "legal_id_number", "account_name", "account_number"];
463
- const APPLY_REQUIRED = ["contact_name", "contact_mobile", "contact_email", "merchant_shortname", "merchant_name", "license_number",
463
+ // 字段名 → 中文 (错误提示直接给中文, 前端无需翻译表 — 2026-10-03 xiaomu 实测 "没反应" fix)
464
+ const APPLY_LABELS = {
465
+ contact_name: "超管姓名", contact_mobile: "超管手机号", contact_email: "联系邮箱",
466
+ merchant_shortname: "商户简称", merchant_name: "营业执照名称", license_number: "统一社会信用代码",
467
+ legal_name: "法人/经营者姓名", legal_id_number: "法人身份证号",
468
+ legal_id_period_begin: "身份证有效期开始", legal_id_period_end: "身份证有效期结束",
469
+ account_name: "开户名称", account_number: "银行账号", account_bank: "开户银行",
470
+ store_name: "门店名称", store_address_code: "门店省市编码", store_address: "门店详细地址",
471
+ website_url: "网站网址", appid: "公众号/小程序 AppID",
472
+ service_phone: "客服电话", settlement_id: "经营类目",
473
+ license_media_id: "营业执照照片", legal_id_front_media_id: "身份证人像面", legal_id_back_media_id: "身份证国徽面",
474
+ store_entrance_media_id: "门店门头照片", store_indoor_media_id: "门店内部照片",
475
+ web_home_media_id: "网站首页截图", web_product_media_id: "商品/服务页截图", mp_media_id: "公众号/小程序页面截图",
476
+ };
477
+ // 通用必填 (qualification_type 不在内: 通用类目留空, 后端默认 "无" — 2026-10-03 实测秒拒 fix)
478
+ const APPLY_REQUIRED_COMMON = ["contact_name", "contact_mobile", "contact_email", "merchant_shortname", "merchant_name", "license_number",
464
479
  "legal_name", "legal_id_number", "legal_id_period_begin", "legal_id_period_end",
465
- "account_name", "account_number", "account_bank", "store_name", "store_address_code", "store_address",
466
- "service_phone", "settlement_id", "qualification_type"];
467
- const APPLY_MEDIA = [["license_media_id", "营业执照照片"], ["legal_id_front_media_id", "身份证人像面"],
468
- ["legal_id_back_media_id", "身份证国徽面"], ["store_entrance_media_id", "门店门头照片"], ["store_indoor_media_id", "门店内部照片"]];
480
+ "account_name", "account_number", "account_bank", "service_phone", "settlement_id"];
481
+ // 场景化必填 (2026-10-03 xiaomu 拍板三场景: 线下门店 / 互联网网站 / 公众号·小程序)
482
+ const APPLY_SCENE_CFG = {
483
+ SALES_SCENES_STORE: { fields: ["store_name", "store_address_code", "store_address"], media: ["store_entrance_media_id", "store_indoor_media_id"] },
484
+ SALES_SCENES_WEB: { fields: ["website_url"], media: ["web_home_media_id", "web_product_media_id"] },
485
+ SALES_SCENES_MP: { fields: ["appid"], media: ["mp_media_id"] },
486
+ };
487
+ const APPLY_MEDIA_COMMON = ["license_media_id", "legal_id_front_media_id", "legal_id_back_media_id"];
469
488
 
470
489
  /** SP 网关配置 (懒读 env, 测试可注入 deps.fetcher) */
471
490
  function spGatewayUrl() {
@@ -539,19 +558,24 @@ CREATE TABLE IF NOT EXISTS onboard_applyments (
539
558
  if (latest && latest.business_code) {
540
559
  return res.json({ ok: true, idempotent: true, business_code: latest.business_code, applyment_id: latest.applyment_id, status: latest.status });
541
560
  }
542
- const hasForm = APPLY_REQUIRED.some((k) => b[k]) || Object.keys(b).some((k) => /_media_id$/.test(k));
561
+ const hasForm = APPLY_REQUIRED_COMMON.some((k) => b[k]) || Object.keys(b).some((k) => /_media_id$/.test(k));
543
562
  let payload;
544
563
  const now = Math.floor(Date.now() / 1000);
545
564
  if (hasForm) {
546
- // 校验必填字段 + 必备照片
547
- for (const k of APPLY_REQUIRED) {
565
+ // 经营场景 (非法值回落线下门店)
566
+ const scene = APPLY_SCENE_CFG[b.sales_scene] ? b.sales_scene : "SALES_SCENES_STORE";
567
+ const sceneCfg = APPLY_SCENE_CFG[scene];
568
+ // 校验必填字段 (中文提示 + field 名回传, 前端红卡 + 字段定位)
569
+ for (const k of APPLY_REQUIRED_COMMON.concat(sceneCfg.fields)) {
548
570
  if (!String(b[k] || "").trim()) {
549
- return res.status(400).json({ ok: false, error: "invalid_param", message: "缺少必填字段: " + k });
571
+ const label = APPLY_LABELS[k] || k;
572
+ return res.status(400).json({ ok: false, error: "invalid_param", field: k, message: `缺少必填字段: ${label} (${k})` });
550
573
  }
551
574
  }
552
- for (const [k, label] of APPLY_MEDIA) {
575
+ for (const k of APPLY_MEDIA_COMMON.concat(sceneCfg.media)) {
553
576
  if (!b[k]) {
554
- return res.status(400).json({ ok: false, error: "media_missing", message: label + " 未上传 (请先上传拿到 media_id)" });
577
+ const label = APPLY_LABELS[k] || k;
578
+ return res.status(400).json({ ok: false, error: "media_missing", field: k, message: `${label} 未上传 (请先上传拿到 media_id)` });
555
579
  }
556
580
  }
557
581
  // 本机加密 (SP 微信支付公钥; 私钥只在微信侧, 密文暂存/传输都安全)
@@ -566,6 +590,9 @@ CREATE TABLE IF NOT EXISTS onboard_applyments (
566
590
  for (const k of Object.keys(b)) {
567
591
  if (typeof b[k] === "string" && b[k].trim()) payload[k] = b[k].trim();
568
592
  }
593
+ payload.sales_scene = scene; // 场景必带 (SP 侧按它组装 biz_store_info/web_info/mp_info)
594
+ // 通用类目资质留空 → 默认 "无" (微信通用场景接受, 避免留空被 SP 拒)
595
+ if (!payload.qualification_type) payload.qualification_type = "无";
569
596
  for (const k of APPLY_SENSITIVE) payload[k] = spApplyment.encryptSensitive(payload[k], pem);
570
597
  // 法人姓名双轨: legal_person 要明文 (payload.legal_name 不动),
571
598
  // id_card_info.id_card_name 要密文 (单独字段, SP 侧 already_encrypted 时优先取它)
@@ -604,8 +631,8 @@ CREATE TABLE IF NOT EXISTS onboard_applyments (
604
631
  const msg = r.json.message || ("服务商返回 HTTP " + r.status);
605
632
  db.prepare("UPDATE onboard_applyments SET push_error=?, updated_at=? WHERE id=(SELECT id FROM onboard_applyments ORDER BY id DESC LIMIT 1)")
606
633
  .run(String(msg).slice(0, 300), now);
607
- // SP/微信业务错误 (如资料问题) 原样透传给卖家页面
608
- return res.status(502).json({ ok: false, error: "sp_error", sp_status: r.status, wx_code: r.json.wx_code || "", message: msg });
634
+ // SP/微信业务错误 (如资料问题) 原样透传给卖家页面 (含 field 供前端定位高亮)
635
+ return res.status(502).json({ ok: false, error: "sp_error", sp_status: r.status, wx_code: r.json.wx_code || "", field: r.json.field || undefined, message: msg });
609
636
  }
610
637
  db.prepare(`UPDATE onboard_applyments SET status='SUBMITTED', business_code=?, applyment_id=?, push_error='', updated_at=?
611
638
  WHERE id=(SELECT id FROM onboard_applyments ORDER BY id DESC LIMIT 1)`)
@@ -120,16 +120,55 @@ function reqStr(v, name, maxLen) {
120
120
  function buildApplymentBody(form, enc, opts) {
121
121
  const f = form || {};
122
122
  const sensLen = (opts && opts.alreadyEncrypted) ? 2048 : 0; // 0 = 用明文默认上限
123
- // 必备照片 media_id 校验 (缺哪张报哪张)
123
+ // 经营场景 (2026-10-03 xiaomu 拍板三选一): 线下门店(默认) / 互联网网站 / 公众号·小程序
124
+ const scene = (f.sales_scene === "SALES_SCENES_WEB" || f.sales_scene === "SALES_SCENES_MP") ? f.sales_scene : "SALES_SCENES_STORE";
125
+ // 必备照片 media_id 校验 (通用 3 张 + 场景照片, 缺哪张报哪张)
124
126
  const mediaNeed = [
125
127
  ["license_media_id", "营业执照照片"],
126
128
  ["legal_id_front_media_id", "身份证人像面照片"],
127
129
  ["legal_id_back_media_id", "身份证国徽面照片"],
128
- ["store_entrance_media_id", "门店门头照片"],
129
- ["store_indoor_media_id", "门店内部照片"],
130
130
  ];
131
+ if (scene === "SALES_SCENES_STORE") {
132
+ mediaNeed.push(["store_entrance_media_id", "门店门头照片"], ["store_indoor_media_id", "门店内部照片"]);
133
+ } else if (scene === "SALES_SCENES_WEB") {
134
+ mediaNeed.push(["web_home_media_id", "网站首页截图"], ["web_product_media_id", "商品/服务页截图"]);
135
+ } else {
136
+ mediaNeed.push(["mp_media_id", "公众号/小程序页面截图"]);
137
+ }
131
138
  for (const [k, label] of mediaNeed) {
132
- if (!f[k]) throw new HttpError(400, "media_missing", label + " 未上传 (请先拍照上传拿到 media_id)");
139
+ if (!f[k]) throw new HttpError(400, "media_missing", label + " 未上传 (请先拍照上传拿到 media_id)", k);
140
+ }
141
+ // 场景化经营场景结构 (微信官方: WEB→web_info / MP→mp_info(公众号) 或 mini_program_info(小程序))
142
+ const salesInfo = { sales_scenes_type: [scene] };
143
+ if (scene === "SALES_SCENES_STORE") {
144
+ salesInfo.biz_store_info = {
145
+ biz_store_name: reqStr(f.store_name, "门店名称", 50),
146
+ biz_address_code: reqStr(f.store_address_code, "门店省市编码", 16),
147
+ biz_store_address: reqStr(f.store_address, "门店详细地址", 512),
148
+ store_entrance_pic: [String(f.store_entrance_media_id)],
149
+ indoor_pic: [String(f.store_indoor_media_id)],
150
+ };
151
+ } else if (scene === "SALES_SCENES_WEB") {
152
+ const url = reqStr(f.website_url, "网站网址", 256);
153
+ if (!/^https?:\/\//i.test(url)) throw new HttpError(400, "invalid_param", "网站网址须以 http(s):// 开头 (需已上线且有 ICP 备案)", "website_url");
154
+ salesInfo.web_info = {
155
+ website_url: url,
156
+ web_pics: [String(f.web_home_media_id), String(f.web_product_media_id)],
157
+ };
158
+ } else {
159
+ // 公众号/小程序二选一: 用户只有小程序 appid 时用 mini_program_info
160
+ if (f.appid_type === "mini") {
161
+ salesInfo.sales_scenes_type = ["SALES_SCENES_MINI_PROGRAM"];
162
+ salesInfo.mini_program_info = {
163
+ mini_program_sub_appid: reqStr(f.appid, "小程序 AppID", 64),
164
+ mini_program_pics: [String(f.mp_media_id)],
165
+ };
166
+ } else {
167
+ salesInfo.mp_info = {
168
+ mp_sub_appid: reqStr(f.appid, "公众号 AppID", 64), // 商家自己的已认证公众号
169
+ mp_pics: [String(f.mp_media_id)],
170
+ };
171
+ }
133
172
  }
134
173
  const subjectType = f.subject_type === "SUBJECT_TYPE_ENTERPRISE" ? "SUBJECT_TYPE_ENTERPRISE" : "SUBJECT_TYPE_INDIVIDUAL";
135
174
  const body = {
@@ -165,20 +204,12 @@ function buildApplymentBody(form, enc, opts) {
165
204
  business_info: {
166
205
  merchant_shortname: reqStr(f.merchant_shortname, "商户简称", 64),
167
206
  service_phone: reqStr(f.service_phone, "客服电话", 32),
168
- sales_info: {
169
- sales_scenes_type: ["SALES_SCENES_STORE"],
170
- biz_store_info: {
171
- biz_store_name: reqStr(f.store_name, "门店名称", 50),
172
- biz_address_code: reqStr(f.store_address_code, "门店省市编码", 16),
173
- biz_store_address: reqStr(f.store_address, "门店详细地址", 512),
174
- store_entrance_pic: [String(f.store_entrance_media_id)],
175
- indoor_pic: [String(f.store_indoor_media_id)],
176
- },
177
- },
207
+ sales_info: salesInfo,
178
208
  },
179
209
  settlement_info: {
180
- settlement_id: reqStr(f.settlement_id, "经营类目编码(settlement_id)", 16),
181
- qualification_type: reqStr(f.qualification_type, "经营类目资质(qualification_type)", 128),
210
+ settlement_id: reqStr(f.settlement_id, "经营类目", 16),
211
+ // 通用类目 (716/719/727) 资质留空即可 — 留空默认 "无" (2026-10-03 xiaomu 实测秒拒 fix)
212
+ qualification_type: f.qualification_type ? reqStr(f.qualification_type, "类目资质描述", 128) : "无",
182
213
  },
183
214
  bank_account_info: {
184
215
  bank_account_type: f.account_type === "BANK_ACCOUNT_TYPE_PERSONAL" ? "BANK_ACCOUNT_TYPE_PERSONAL" : "BANK_ACCOUNT_TYPE_CORPORATE",
@@ -28,6 +28,7 @@ process.env.WXPAY_SP_PUBLIC_KEY_PATH = pubKeyPath;
28
28
  process.env.SP_MERCHANT_TOKEN = MERCHANT_TOKEN;
29
29
 
30
30
  const onboard = require("./routes/onboard");
31
+ const spApplymentLib = require("./routes/sp-applyment"); // buildApplymentBody 场景结构断言用
31
32
 
32
33
  // ---------- 假 SP 网关 (捕获请求体) ----------
33
34
  const spCapture = { uploads: [], submits: [], statusQueries: [] };
@@ -265,6 +266,126 @@ test("onboard 页面含进件表单 (btn-apply-submit) 且第③步状态机元
265
266
  assert.ok(html.includes("btn-apply-submit"), "进件提交按钮");
266
267
  assert.ok(html.includes("apply-status"), "状态框");
267
268
  assert.ok(html.includes("/api/onboard/applyment"), "页面打新端点");
269
+ // 2026-10-03: 三场景选择器 + 错误红卡
270
+ assert.ok(html.includes('name="sales_scene"'), "场景单选");
271
+ assert.ok(html.includes("SALES_SCENES_WEB") && html.includes("SALES_SCENES_MP"), "WEB/MP 场景");
272
+ assert.ok(html.includes("apply-err"), "错误红卡容器");
273
+ } finally { srv.close(); }
274
+ });
275
+
276
+ /** 场景表单变体: 去掉门店字段, 换场景专属字段+照片 */
277
+ async function fillSceneForm(base, scene) {
278
+ const form = await fillForm(base);
279
+ delete form.store_name; delete form.store_address_code; delete form.store_address;
280
+ delete form.store_entrance_media_id; delete form.store_indoor_media_id;
281
+ form.sales_scene = scene;
282
+ if (scene === "SALES_SCENES_WEB") {
283
+ form.website_url = "https://shop.example.com";
284
+ for (const k of ["web_home_media_id", "web_product_media_id"]) {
285
+ const r = await fetch(base + "/api/onboard/applyment/upload", {
286
+ method: "POST", headers: { "Content-Type": "application/json" },
287
+ body: JSON.stringify({ filename: k + ".jpg", content_b64: Buffer.from("fake-" + k).toString("base64") }),
288
+ });
289
+ form[k] = (await r.json()).media_id;
290
+ }
291
+ } else if (scene === "SALES_SCENES_MP") {
292
+ form.appid = "wx1234567890abcdef";
293
+ form.appid_type = "mp";
294
+ const r = await fetch(base + "/api/onboard/applyment/upload", {
295
+ method: "POST", headers: { "Content-Type": "application/json" },
296
+ body: JSON.stringify({ filename: "mp.jpg", content_b64: Buffer.from("fake-mp").toString("base64") }),
297
+ });
298
+ form.mp_media_id = (await r.json()).media_id;
299
+ }
300
+ return form;
301
+ }
302
+
303
+ test("错误提示: 缺 store_address → 中文 message + field 定位 (2026-10-03 实测 fix)", async () => {
304
+ const { srv, base } = await startSeller();
305
+ try {
306
+ const form = await fillForm(base);
307
+ delete form.store_address;
308
+ const r = await fetch(base + "/api/onboard/applyment", {
309
+ method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(form),
310
+ });
311
+ const j = await r.json();
312
+ assert.strictEqual(r.status, 400);
313
+ assert.strictEqual(j.error, "invalid_param");
314
+ assert.strictEqual(j.field, "store_address", "field 回传供前端高亮");
315
+ assert.ok(j.message.includes("门店详细地址"), "中文文案: " + j.message);
316
+ assert.ok(j.message.includes("(store_address)"), "带字段名: " + j.message);
317
+ } finally { srv.close(); }
318
+ });
319
+
320
+ test("qualification_type 留空可提交 (后端默认 '无', 2026-10-03 实测秒拒 fix)", async () => {
321
+ const { srv, base } = await startSeller();
322
+ try {
323
+ const form = await fillForm(base);
324
+ delete form.qualification_type;
325
+ const before = spCapture.submits.length;
326
+ const r = await fetch(base + "/api/onboard/applyment", {
327
+ method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(form),
328
+ });
329
+ const j = await r.json();
330
+ assert.strictEqual(r.status, 200, JSON.stringify(j));
331
+ assert.strictEqual(spCapture.submits[before].qualification_type, "无", "留空默认 '无'");
332
+ } finally { srv.close(); }
333
+ });
334
+
335
+ test("WEB 场景: 不带门店字段可提交, SP 侧组装 web_info", async () => {
336
+ const { srv, base } = await startSeller();
337
+ try {
338
+ const form = await fillSceneForm(base, "SALES_SCENES_WEB");
339
+ const before = spCapture.submits.length;
340
+ const r = await fetch(base + "/api/onboard/applyment", {
341
+ method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(form),
342
+ });
343
+ const j = await r.json();
344
+ assert.strictEqual(r.status, 200, JSON.stringify(j));
345
+ const fwd = spCapture.submits[before];
346
+ assert.strictEqual(fwd.sales_scene, "SALES_SCENES_WEB");
347
+ assert.strictEqual(fwd.website_url, "https://shop.example.com");
348
+ assert.ok(!fwd.store_name, "不带门店字段");
349
+ // SP 侧结构: sales_info.web_info
350
+ const wxBody = spApplymentLib.buildApplymentBody({ ...fwd, business_code: "TBTEST" }, (s) => s, { alreadyEncrypted: true });
351
+ assert.deepStrictEqual(wxBody.business_info.sales_info.sales_scenes_type, ["SALES_SCENES_WEB"]);
352
+ assert.strictEqual(wxBody.business_info.sales_info.web_info.website_url, "https://shop.example.com");
353
+ assert.strictEqual(wxBody.business_info.sales_info.web_info.web_pics.length, 2);
354
+ assert.ok(!wxBody.business_info.sales_info.biz_store_info, "无门店结构");
355
+ // 网址不带协议 → SP 拒
356
+ assert.throws(() => spApplymentLib.buildApplymentBody({ ...fwd, website_url: "shop.example.com" }, (s) => s, { alreadyEncrypted: true }), /http/);
357
+ } finally { srv.close(); }
358
+ });
359
+
360
+ test("MP 场景: mp_media_id 必传 + appid_type=mini 走 mini_program_info", async () => {
361
+ const { srv, base } = await startSeller();
362
+ try {
363
+ // 缺公众号截图 → 400 中文提示
364
+ const form = await fillSceneForm(base, "SALES_SCENES_MP");
365
+ delete form.mp_media_id;
366
+ let r = await fetch(base + "/api/onboard/applyment", {
367
+ method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(form),
368
+ });
369
+ let j = await r.json();
370
+ assert.strictEqual(r.status, 400);
371
+ assert.strictEqual(j.error, "media_missing");
372
+ assert.strictEqual(j.field, "mp_media_id");
373
+ assert.ok(j.message.includes("公众号/小程序页面截图"), j.message);
374
+ // 补齐 → 200; mini 类型 → mini_program_info
375
+ const form2 = await fillSceneForm(base, "SALES_SCENES_MP");
376
+ form2.appid_type = "mini";
377
+ r = await fetch(base + "/api/onboard/applyment", {
378
+ method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(form2),
379
+ });
380
+ j = await r.json();
381
+ assert.strictEqual(r.status, 200, JSON.stringify(j));
382
+ const fwd = spCapture.submits[spCapture.submits.length - 1];
383
+ const wxBody = spApplymentLib.buildApplymentBody({ ...fwd, business_code: "TBTEST" }, (s) => s, { alreadyEncrypted: true });
384
+ assert.deepStrictEqual(wxBody.business_info.sales_info.sales_scenes_type, ["SALES_SCENES_MINI_PROGRAM"]);
385
+ assert.strictEqual(wxBody.business_info.sales_info.mini_program_info.mini_program_sub_appid, "wx1234567890abcdef");
386
+ // 公众号类型 → mp_info.mp_sub_appid
387
+ const wxBody2 = spApplymentLib.buildApplymentBody({ ...fwd, appid_type: "mp", business_code: "TBTEST" }, (s) => s, { alreadyEncrypted: true });
388
+ assert.strictEqual(wxBody2.business_info.sales_info.mp_info.mp_sub_appid, "wx1234567890abcdef");
268
389
  } finally { srv.close(); }
269
390
  });
270
391