# -*- coding: utf-8 -*- """商品接口参数组合回归:PHP 线上 vs Java 本地,逐字段对比""" import json import re import sys from pathlib import Path import requests UPLOAD = Path(r"F:\import goods\upload_goods.py") token = re.search(r'TOKEN\s*=\s*"([^"]+)"', UPLOAD.read_text(encoding="utf-8")).group(1) PHP = "https://shop.qinys.cn/index.php?s=" JAVA = "http://localhost:18080" LIST_CASES = [ {"page": 1, "listRows": 15}, {"page": 1, "goodsName": "榴莲"}, {"page": 1, "status": 10}, {"page": 1, "listType": "on_sale"}, {"page": 1, "listType": "off_sale"}, {"page": 1, "listType": "sold_out"}, {"page": 1, "sortType": "sales"}, {"page": 2}, {"page": 1, "categoryId": 10001}, {"page": 1, "goodsNo": "DL-TEST"}, {"page": 1, "sortType": "price"}, {"page": 1, "sortType": "price", "sortPrice": "true"}, ] DETAIL_CASES = [{"goodsId": 10010}, {"goodsId": 10003}, {"goodsId": 999999}] def php(path, data): return requests.post(PHP + path, headers={"Access-Token": token}, data=data, timeout=30).json() def java(path, data): return requests.post(JAVA + path, data=data, timeout=30).json() def diff(a, b, path="", out=None): out = [] if out is None else out if type(a) != type(b): out.append(f"{path}: 类型 PHP={type(a).__name__}({str(a)[:60]}) JAVA={type(b).__name__}({str(b)[:60]})") return out if isinstance(a, dict): for k in sorted(set(a) | set(b)): if k not in a: out.append(f"{path}.{k}: Java 多出") elif k not in b: out.append(f"{path}.{k}: Java 缺失") else: diff(a[k], b[k], f"{path}.{k}", out) elif 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) elif a != b: out.append(f"{path}: PHP={str(a)[:60]!r} JAVA={str(b)[:60]!r}") return out total_bad = 0 for case in LIST_CASES: p = php("/store/goods/list", case) j = java("/store/goods/list", case) d = diff(p, j) total_bad += len(d) print(("PASS " if not d else "DIFF ") + "list " + json.dumps(case, ensure_ascii=False), "" if not d else f"-> {len(d)} 处,例如 {d[0][:110]}") for case in DETAIL_CASES: p = php("/store/goods/detail", case) j = java("/store/goods/detail", case) d = diff(p, j) total_bad += len(d) print(("PASS " if not d else "DIFF ") + "detail " + json.dumps(case, ensure_ascii=False), "" if not d else f"-> {len(d)} 处,例如 {d[0][:110]}") print("\n总差异:", total_bad) sys.exit(0 if total_bad == 0 else 1)