From a72366566ed37e3a36e1388cbdc6aacb642ab050 Mon Sep 17 00:00:00 2001 From: z8jd98 Date: Sat, 31 Oct 2026 01:48:15 +0000 Subject: [PATCH] =?UTF-8?q?feat(bypos-collector):=20=E8=B7=A8=E6=AC=A1?= =?UTF-8?q?=E5=8E=BB=E9=87=8D=E3=80=81=E9=87=8D=E8=AF=95=E3=80=81CSV=20?= =?UTF-8?q?=E8=A1=A5=E5=85=A8=E3=80=81=E7=AB=AF=E5=8F=A3=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E9=80=89=E6=8B=A9=E3=80=81Windows=20=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E5=92=8C=E4=B8=80=E9=94=AE=E5=AF=BC=E5=85=A5=EF=BC=88=E5=8F=96?= =?UTF-8?q?=E4=BB=A3=20#24=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- tools/bypos-collector/README.md | 7 +- tools/bypos-collector/collect.go | 144 +++++++++++++++++++------- tools/bypos-collector/collect_test.go | 113 ++++++++++++++++++++ tools/bypos-collector/main.go | 141 ++++++++++++++++++++++--- tools/bypos-collector/web/index.html | 70 ++++++++++++- 5 files changed, 418 insertions(+), 57 deletions(-) create mode 100644 tools/bypos-collector/collect_test.go diff --git a/tools/bypos-collector/README.md b/tools/bypos-collector/README.md index 6039816..34c5b8d 100644 --- a/tools/bypos-collector/README.md +++ b/tools/bypos-collector/README.md @@ -58,9 +58,10 @@ GET http://zc.bypos.net/byGoodsService/byMessage.asmx/GetGoodsinfo ## 配置 -- `sdogid`:中心库账号 id(本项目所属云店账号的授权 id)。默认值见 - `collect.go` 的 `defaultSdogID`,也可用 `-sdogid` 参数或控制台覆盖。 - **这是账号级凭证**——若本仓库对外公开,建议改为从环境变量/外部配置读取。 +- `sdogid`:中心库账号 id(本项目所属云店账号的授权 id)。 + 优先从环境变量 `BYPOS_SDOGID` 读取,其次是命令行 `-sdogid` 参数, + 也可以在 Web 控制台的输入框中填写。**三者都未设时启动会警告、开始采集时会报错。** +- 网络错误自动重试(最多 3 次,指数退避 500ms/1s/2s)。 ## 导入 goods/天工库 diff --git a/tools/bypos-collector/collect.go b/tools/bypos-collector/collect.go index ebe6672..3583f87 100644 --- a/tools/bypos-collector/collect.go +++ b/tools/bypos-collector/collect.go @@ -1,6 +1,7 @@ package main import ( + "bufio" "context" "crypto/md5" "encoding/hex" @@ -20,12 +21,21 @@ import ( ) // ---- upstream config ---- -// sdogid is the 云店 account license id observed in the live request. It is -// configurable so the tool is not tied to a single account. -const defaultSdogID = "137966" const endpoint = "http://zc.bypos.net/byGoodsService/byMessage.asmx/GetGoodsinfo" +// maxRetries is the number of retry attempts for transient network errors. +const maxRetries = 3 + +// resolveSdogID reads the account id from $BYPOS_SDOGID, falling back to the +// explicit argument (CLI flag or Web UI input). Returns empty if neither set. +func resolveSdogID(explicit string) string { + if v := os.Getenv("BYPOS_SDOGID"); v != "" { + return v + } + return explicit +} + var stringTagRe = regexp.MustCompile(`(?s)]*>(.*)`) // Product is the normalized record we persist (one JSON object per line). @@ -87,8 +97,8 @@ func ean13Check(body string) (string, bool) { return body + strconv.Itoa(chk), true } -// lookup queries the upstream central library for one barcode. -func (c *Collector) lookup(ctx context.Context, barcode string) (*Product, error) { +// lookupOnce performs a single HTTP request to the upstream central library. +func (c *Collector) lookupOnce(ctx context.Context, barcode string) (*Product, error) { tsMs := strconv.FormatInt(time.Now().Unix()*1000, 10) // always ends in 000 sparm1 := md5hex(c.sdogID) sparm2 := md5hex(barcode + tsMs) @@ -147,21 +157,48 @@ func (c *Collector) lookup(ctx context.Context, barcode string) (*Product, error return p, nil } +// lookup queries the upstream with up to maxRetries retries on transient errors. +func (c *Collector) lookup(ctx context.Context, barcode string) (*Product, error) { + var lastErr error + for attempt := 0; attempt <= maxRetries; attempt++ { + if ctx.Err() != nil { + return nil, ctx.Err() + } + p, err := c.lookupOnce(ctx, barcode) + if err == nil { + return p, nil + } + lastErr = err + if attempt < maxRetries { + backoff := time.Duration(1< @@ -79,6 +84,10 @@

② 采集参数

+
+ + +
@@ -93,11 +102,12 @@
-
速度越快越容易触发上游频控。建议并发 3、间隔 300ms 起步,稳定后再调。已采过的条码会自动跳过(断点续采)。
+
速度越快越容易触发上游频控。建议并发 3、间隔 300ms 起步,稳定后再调。已采过的条码会自动跳过(跨次去重)。

③ 运行

+
历史已采集: 0 个条码
@@ -112,13 +122,36 @@
0
未命中
0
无效
0
错误
-
0
跳过
+
0
跳过(已采)
-

④ 实时结果(最近 60 条)

+

④ 导入到 Goods 系统

+
采集完成后,将数据一键导入到 OpenGoods 商品档案系统。
+
+
+ + +
+
+ + +
+
+ + +
+
+
+ +
+
+
+ +
+

⑤ 实时结果(最近 60 条)

@@ -165,6 +198,7 @@ async function start(){ start_body: document.getElementById('start').value.trim(), end_body: document.getElementById('end').value.trim(), list: document.getElementById('list').value, + sdog_id: document.getElementById('sdogid').value.trim(), concurrency: parseInt(document.getElementById('concurrency').value)||3, delay_ms: parseInt(document.getElementById('delay').value)||0, log_miss: document.getElementById('logmiss').checked, @@ -191,6 +225,7 @@ async function poll(){ document.getElementById('s-skipped').textContent = s.skipped; let pct = s.total>0 ? Math.floor(s.done*100/s.total) : 0; document.getElementById('prog').style.width = pct + '%'; + document.getElementById('history-badge').textContent = '历史已采集: ' + (s.history_count||0) + ' 个条码'; document.getElementById('msg').textContent = (s.running? ('采集中… 当前 '+s.current) : (s.message||'空闲')); document.getElementById('btnStart').disabled = s.running; document.getElementById('btnStop').disabled = !s.running; @@ -201,6 +236,35 @@ async function poll(){ }catch(e){} } setInterval(poll, 1000); poll(); + +async function doImport(){ + let url = document.getElementById('import-url').value.trim(); + let user = document.getElementById('import-user').value.trim(); + let pass = document.getElementById('import-pass').value; + if(!url || !user || !pass){ alert('请填写完整的 Goods 系统地址、用户名和密码'); return; } + let btn = document.getElementById('btnImport'); + let res = document.getElementById('import-result'); + btn.disabled = true; + btn.textContent = '导入中...'; + res.innerHTML = ''; + try { + let r = await fetch('/api/import', { + method:'POST', + headers:{'Content-Type':'application/json'}, + body:JSON.stringify({api_url:url, username:user, password:pass}) + }); + let j = await r.json(); + if(j.error){ + res.innerHTML = '
导入失败: '+esc(j.error.message||j.error)+'
'; + } else { + res.innerHTML = '
导入完成: 成功 '+j.loaded+' 条, 跳过 '+j.skipped+' 条, 失败 '+j.errored+' 条
'; + } + } catch(e){ + res.innerHTML = '
导入失败: '+esc(e.message)+'
'; + } + btn.disabled = false; + btn.textContent = '导入到 Goods'; +}
条码品名规格单位产地进价零售价状态