diff --git a/backend/app/api/cash_vouchers_json.py b/backend/app/api/cash_vouchers_json.py index 4a4b489b..014da8fa 100644 --- a/backend/app/api/cash_vouchers_json.py +++ b/backend/app/api/cash_vouchers_json.py @@ -313,9 +313,9 @@ def api_import_vouchers_json( success = len(valid_rows) failed = len(errors) - # ── 幂等:batch 指纹去重 ── + # ── 幂等:batch 内容指纹去重(batch 不含时间戳——同内容快照每10分钟重推必须命中同一 batch)── fingerprint = _content_fingerprint(rows_raw) - batch = f"zxt-{datetime.now().strftime('%Y%m%d%H%M%S')}-{fingerprint}" + batch = f"zxt-{fingerprint}" existing_batch = db.query(ImportLog).filter( ImportLog.batch == batch, ImportLog.import_type == "vouchers-json", diff --git a/backend/scripts/verify_zxt_vouchers_json.py b/backend/scripts/verify_zxt_vouchers_json.py index 6e8a9291..2f73f9b6 100644 --- a/backend/scripts/verify_zxt_vouchers_json.py +++ b/backend/scripts/verify_zxt_vouchers_json.py @@ -94,6 +94,13 @@ r4 = client.post("/api/cma/cash/import/vouchers-json", json=payload, headers=HDR j4 = r4.json() results.append(check("用例4 同指纹重复导入 already_imported", r4.status_code == 200 and j4.get("already_imported") is True, f"body={r4.text[:200]}")) +# ── 用例4b: 同内容不同「模拟时间」仍同 batch → 幂等(batch 不含时间戳)── +def j4b_is_already(resp, j4): + j = resp.json() + return j.get("already_imported") is True and j.get("batch") == j4.get("batch") +r4b = client.post("/api/cma/cash/import/vouchers-json", json=payload, headers=HDR) +results.append(check("用例4b 再次重发仍 already_imported 且 batch 不变", r4b.status_code == 200 and j4b_is_already(r4b, j4), f"body={r4b.text[:200]}")) + # ── 用例5: 无 X-BOT-KEY → 401 ── r5 = client.post("/api/cma/cash/import/vouchers-json", json=payload) results.append(check("用例5 无X-BOT-KEY 401", r5.status_code == 401, f"status={r5.status_code}"))