# -*- coding: utf-8 -*- """设置类接口实时对比(配送模板 / 物流公司 / 支付设置 / 支付模板 / 打印机)。 用法:python compare_setting_batch.py [用例名...] """ import json import os import re import sys from pathlib import Path import requests PHP_BASE = os.environ.get("YOSHOP_PHP_BASE", "https://shop.qinys.cn/index.php?s=") JAVA_BASE = "http://127.0.0.1:18080" TOKEN = "testsession123" SESSION = requests.Session() SESSION.trust_env = False CASES = [ ("delivery_detail_10002", "/store/setting.delivery/detail&deliveryId=10002", "/store/setting.delivery/detail?deliveryId=10002"), ("delivery_detail_10001", "/store/setting.delivery/detail&deliveryId=10001", "/store/setting.delivery/detail?deliveryId=10001"), ("delivery_detail_missing", "/store/setting.delivery/detail&deliveryId=99999", "/store/setting.delivery/detail?deliveryId=99999"), ("delivery_all", "/store/setting.delivery/all", "/store/setting.delivery/all"), ("delivery_list", "/store/setting.delivery/list&page=1", "/store/setting.delivery/list?page=1"), ("express_all", "/store/setting.express/all", "/store/setting.express/all"), ("express_list", "/store/setting.express/list&page=1", "/store/setting.express/list?page=1"), ("printer_all", "/store/setting.printer/all", "/store/setting.printer/all"), ("printer_list", "/store/setting.printer/list&page=1", "/store/setting.printer/list?page=1"), ("payment_options", "/store/setting.payment/options", "/store/setting.payment/options"), ("payment_template_list", "/store/setting.payment.template/list", "/store/setting.payment.template/list"), ("payment_template_all", "/store/setting.payment.template/all", "/store/setting.payment.template/all"), ("payment_template_detail_missing", "/store/setting.payment.template/detail&templateId=99999", "/store/setting.payment.template/detail?templateId=99999"), ] def php_token() -> str: if os.environ.get("YOSHOP_TOKEN"): return os.environ["YOSHOP_TOKEN"] src = Path(r"F:\import goods\upload_goods.py").read_text(encoding="utf-8") m = re.search(r'TOKEN\s*=\s*"([^"]+)"', src) return m.group(1) if m else TOKEN def diff(a, b, path="$", out=None, limit=25): out = out if out is not None else [] if len(out) >= limit: return out if type(a) != type(b): if isinstance(a, (int, float)) and isinstance(b, (int, float)) \ and not isinstance(a, bool) and not isinstance(b, bool): if float(a) != float(b): out.append(f"{path}: php={a!r} java={b!r}") return out out.append(f"{path}: 类型不同 php={type(a).__name__} java={type(b).__name__} (php={a!r} java={b!r})") return out if isinstance(a, dict): for k in a: if k not in b: out.append(f"{path}.{k}: Java 缺失 (php={a[k]!r})") else: diff(a[k], b[k], f"{path}.{k}", out, limit) for k in b: if k not in a: out.append(f"{path}.{k}: Java 多出 (java={b[k]!r})") return out if isinstance(a, list): if len(a) != len(b): out.append(f"{path}: 长度不同 php={len(a)} java={len(b)}") for i in range(min(len(a), len(b))): diff(a[i], b[i], f"{path}[{i}]", out, limit) return out if a != b: out.append(f"{path}: php={a!r} java={b!r}") return out def main(): only = set(sys.argv[1:]) token = php_token() ok = fail = 0 for name, php_path, java_path in CASES: if only and name not in only: continue try: php = SESSION.get(PHP_BASE + php_path, headers={"Access-Token": token}, timeout=60).json() except Exception as e: print(f"[skip] {name} 拉取 PHP 失败: {e}") continue try: java = SESSION.get(JAVA_BASE + java_path, headers={"Access-Token": TOKEN}, timeout=60).json() except Exception as e: print(f"[FAIL] {name} 拉取 Java 失败: {e}") fail += 1 continue if php.get("status") != java.get("status"): print(f"[DIFF] {name} status php={php.get('status')} java={java.get('status')} " f"msg php={php.get('message')!r} java={java.get('message')!r}") fail += 1 continue diffs = diff(php.get("data"), java.get("data")) if diffs: fail += 1 print(f"[DIFF] {name} ({len(diffs)}+ 处)") for d in diffs: print(" ", d) else: ok += 1 print(f"[PASS] {name}") print(f"\n通过 {ok} / 差异 {fail}") if __name__ == "__main__": main()