diff --git a/.env.example b/.env.example index 82c5bd1..c469c8c 100644 --- a/.env.example +++ b/.env.example @@ -40,6 +40,9 @@ MT_CPS_APP_KEY= MT_CPS_APP_SECRET= # 默认渠道追踪标识(sid),用于区分不同 app 的 CPS 数据 MT_CPS_DEFAULT_SID=sgbjia +# 美团调用走的代理。⚠️ 本机/内网开发直连美团会 SSL EOF,必须填本地代理(如 http://127.0.0.1:7897); +# 线上国内服务器留空(=直连)。留空且本机直连失败时 /feed、/coupons、/top-sales 会返回空。 +MT_CPS_PROXY= # ===== Pricebot 上游 (领券业务透传目标) ===== # 客户端调本服务的 /api/v1/coupon/step,我们透传到 pricebot-backend 的 /api/coupon/step。 diff --git a/app/core/config.py b/app/core/config.py index 9f86f76..1d4474c 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -81,6 +81,9 @@ class Settings(BaseSettings): MT_CPS_HOST: str = "https://media.meituan.com" MT_CPS_TIMEOUT_SEC: int = 15 MT_CPS_DEFAULT_SID: str = "sgbjia" + # 美团调用走的代理。本机开发直连美团会 SSL EOF,需填 http://127.0.0.1:7897; + # 线上国内服务器留空(=直连)。见 .env.example 与 integrations/meituan.py。 + MT_CPS_PROXY: str = "" @property def mt_cps_configured(self) -> bool: diff --git a/app/integrations/meituan.py b/app/integrations/meituan.py index 4bb7188..6d1b10a 100644 --- a/app/integrations/meituan.py +++ b/app/integrations/meituan.py @@ -62,8 +62,12 @@ def _call(path: str, body_obj: dict[str, Any]) -> dict[str, Any]: } url = f"{settings.MT_CPS_HOST}{path}" + # 美团调用走 MT_CPS_PROXY(本机开发直连会 SSL EOF,必须走代理;线上留空=直连)。 + # trust_env=False:不读进程环境的 HTTP_PROXY,只认配置,避免被错误/失效代理误导。 + proxy = settings.MT_CPS_PROXY or None try: - resp = httpx.post(url, content=body, headers=headers, timeout=settings.MT_CPS_TIMEOUT_SEC) + with httpx.Client(proxy=proxy, trust_env=False, timeout=settings.MT_CPS_TIMEOUT_SEC) as client: + resp = client.post(url, content=body, headers=headers) except httpx.HTTPError as e: logger.exception("[MT] http error calling %s", url) raise MeituanCpsError(f"meituan http error: {e}") from e