From abd8eabf9b566adcb57dd59694001c89b200ad02 Mon Sep 17 00:00:00 2001 From: marco Date: Thu, 28 May 2026 15:54:51 +0800 Subject: [PATCH] =?UTF-8?q?feat(meituan):=20=E6=9C=AA=E9=85=8D=E7=BD=AE=20?= =?UTF-8?q?MT=5FCPS=5FAPP=5FKEY=20=E6=97=B6=E6=8E=A5=E5=8F=A3=E9=99=8D?= =?UTF-8?q?=E7=BA=A7=E8=BF=94=E7=A9=BA,=E4=B8=8D=E5=86=8D=20502?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 之前未配美团 CPS 凭证时,/coupons 和 /referral-link 调用会抛 MeituanCpsError 被翻成 502,前端首页一进就拉 /feed → 看到错误(虽然 /feed 因 _fetch_topic 吞异常已经返空)。新开发机没填美团 key 时体验差。 本次让美团 3 个接口都在入口处早返空响应: - Settings 新增 mt_cps_configured property(同 wxpay_configured 套路) - /coupons / /feed / /referral-link 未配置时分别返空 list / 空 link - 客户端首页 feed 看到空列表,其他业务(登录/领券/广告/提现)完全不受影响 副作用:/feed 返空现在同时表示"配置缺失"和"美团真无券"两种状态,排查时 看 settings.mt_cps_configured 区分。 Co-Authored-By: Claude Opus 4.7 (1M context) --- app/api/v1/meituan.py | 7 +++++++ app/core/config.py | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/app/api/v1/meituan.py b/app/api/v1/meituan.py index 4d56b73..7e3e2bc 100644 --- a/app/api/v1/meituan.py +++ b/app/api/v1/meituan.py @@ -9,6 +9,7 @@ from concurrent.futures import ThreadPoolExecutor, as_completed from fastapi import APIRouter, HTTPException +from app.core.config import settings from app.integrations.meituan import MeituanCpsError, get_referral_link, query_coupon from app.schemas.meituan import ( CouponCard, @@ -27,6 +28,8 @@ router = APIRouter(prefix="/api/v1/meituan", tags=["meituan-cps"]) @router.post("/coupons", response_model=CouponListResponse, summary="券列表(为您推荐)") def list_coupons(req: CouponListRequest) -> CouponListResponse: + if not settings.mt_cps_configured: + return CouponListResponse(items=[], has_next=False, search_id=None) logger.info("[coupons] lon=%.6f lat=%.6f topic=%s", req.longitude, req.latitude, req.list_topic_id) try: raw = query_coupon( @@ -78,6 +81,8 @@ def _interleave(waimai: list[dict], daodian: list[dict]) -> list[CouponCard]: @router.post("/feed", response_model=FeedResponse, summary="混合feed(外卖+到店交叉, 无限流)") def feed(req: FeedRequest) -> FeedResponse: + if not settings.mt_cps_configured: + return FeedResponse(items=[], has_next=False, page=req.page) page_idx = req.page - 1 lon, lat = req.longitude, req.latitude logger.info("[feed] page=%s lon=%.6f lat=%.6f", req.page, lon, lat) @@ -108,6 +113,8 @@ def feed(req: FeedRequest) -> FeedResponse: @router.post("/referral-link", response_model=ReferralLinkResponse, summary="换取推广链接(点抢时调)") def referral_link(req: ReferralLinkRequest) -> ReferralLinkResponse: + if not settings.mt_cps_configured: + return ReferralLinkResponse(link="", link_map={}) try: raw = get_referral_link( product_view_sign=req.product_view_sign, diff --git a/app/core/config.py b/app/core/config.py index 24b049e..59d2b96 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -53,11 +53,18 @@ class Settings(BaseSettings): SMS_SEND_INTERVAL_SEC: int = 60 # ===== 美团联盟 CPS ===== + # 未配置时所有 /api/v1/meituan/* 接口 200 返空(优雅降级),不影响登录/领券等其他业务。 MT_CPS_APP_KEY: str = "" MT_CPS_APP_SECRET: str = "" MT_CPS_HOST: str = "https://media.meituan.com" MT_CPS_TIMEOUT_SEC: int = 15 MT_CPS_DEFAULT_SID: str = "sgbjia" + + @property + def mt_cps_configured(self) -> bool: + """美团 CPS 凭证齐全(缺则接口返空,而非 502)。""" + return bool(self.MT_CPS_APP_KEY and self.MT_CPS_APP_SECRET) + # ===== 微信支付(商家转账到零钱 / 提现)===== # 真实凭证放 .env(已 gitignore),证书 .pem 放 secrets/。WECHAT_APP_ID 同时用于 # 微信登录(code 换 openid)与转账,必须与 App 端开放平台 appid 一致。