From 7dbee54fc438ea5c14bf5346b8211b8e0a440817 Mon Sep 17 00:00:00 2001 From: eric Date: Tue, 10 Mar 2026 04:53:08 -0500 Subject: [PATCH] 's' --- app/main.py | 12 +++++++++++- app/payment/zpay.py | 6 +++++- app/routers/nomadvip.py | 12 ++++++++++-- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/app/main.py b/app/main.py index 943e7e8..67a49f8 100755 --- a/app/main.py +++ b/app/main.py @@ -15,8 +15,9 @@ """ import logging -from fastapi import FastAPI +from fastapi import FastAPI, Request, HTTPException from fastapi.middleware.cors import CORSMiddleware +from fastapi.responses import JSONResponse from .routers import ( nomadvip_router, @@ -41,6 +42,15 @@ app.add_middleware( allow_headers=["*"], ) + +@app.exception_handler(Exception) +async def global_exception_handler(request: Request, exc: Exception): + """捕获未处理异常,记录日志后返回 500(HTTPException 交由 FastAPI 默认处理)""" + if isinstance(exc, HTTPException): + raise exc + logging.error(f"未处理异常: {request.url.path} - {exc}", exc_info=True) + return JSONResponse(status_code=500, content={"detail": str(exc)}) + # 日志 logging.basicConfig( level=logging.INFO, diff --git a/app/payment/zpay.py b/app/payment/zpay.py index e19811b..96aa3d0 100644 --- a/app/payment/zpay.py +++ b/app/payment/zpay.py @@ -121,7 +121,11 @@ class ZPayProvider(PaymentProvider): result = {} if not isinstance(result, dict): result = {} - if int(result.get("code", 0)) == 1: + try: + code = int(float(result.get("code", 0))) + except (TypeError, ValueError): + code = 0 + if code == 1: return { "status": "ok", "provider": self.name, diff --git a/app/routers/nomadvip.py b/app/routers/nomadvip.py index 7b17aa3..c836438 100644 --- a/app/routers/nomadvip.py +++ b/app/routers/nomadvip.py @@ -60,7 +60,12 @@ async def nomadvip_payh5(item: dict): @router.post("/payh5/redirect") async def nomadvip_payh5_redirect(item: dict, request: Request): """nomadvip 专用:ZPAY mapi 接口""" - total_fee = float(item.get("total_fee", 8800)) + logging.info(f"nomadvip payh5/redirect 请求: item={item}") + try: + total_fee = float(item.get("total_fee", 8800)) + except (TypeError, ValueError): + logging.warning(f"nomadvip payh5/redirect 无效 total_fee: {item.get('total_fee')}") + total_fee = 8800.0 total_fee_yuan = f"{total_fee / 100:.2f}" user_id = item.get("user_id", "unknown") pay_type = (item.get("type") or "vip").lower() @@ -88,7 +93,9 @@ async def nomadvip_payh5_redirect(item: dict, request: Request): device=device, ) if result.get("status") != "ok": - raise HTTPException(status_code=500, detail=result.get("msg", "ZPAY mapi 创建订单失败")) + err_msg = result.get("msg", "ZPAY mapi 创建订单失败") + logging.error(f"nomadvip payh5/redirect ZPAY 创建订单失败: {err_msg}, result={result}") + raise HTTPException(status_code=500, detail=err_msg) payurl = result.get("payurl") payurl2 = result.get("payurl2") img = result.get("img") @@ -104,6 +111,7 @@ async def nomadvip_payh5_redirect(item: dict, request: Request): return RedirectResponse(url=payurl2, status_code=302, headers=headers) if payurl: return RedirectResponse(url=payurl, status_code=302, headers=headers) + logging.error(f"nomadvip payh5/redirect ZPAY 未返回支付链接: device={device}, channel={channel}, result keys={list(result.keys())}") raise HTTPException(status_code=500, detail="ZPAY 未返回支付链接")