From cae96824ecb58a200638e587ae3f699105ee62c1 Mon Sep 17 00:00:00 2001 From: Pine Date: Wed, 2 Sep 2026 11:03:53 +0800 Subject: [PATCH] =?UTF-8?q?fix(auth):=20mp-qr=E5=A4=B1=E8=B4=A5=E6=97=B6?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E8=AF=A6=E7=BB=86=E9=94=99=E8=AF=AF=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=EF=BC=8C=E4=BE=BF=E4=BA=8E=E6=8E=92=E6=9F=A5=E5=B0=8F?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E7=A0=81=E7=94=9F=E6=88=90=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - /auth/mp-qr/start 和 /auth/mp-qr/bind-start 在 get_wxacode 失败时 返回 error 字段,包含具体失败原因(缺少 WECHAT_APPID/SECRET 或微信API调用失败) - 之前只返回 mp_enabled:false + qr_url,前端无法区分是配置问题还是API问题 --- app/api/routers/auth.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/api/routers/auth.py b/app/api/routers/auth.py index 7fbc107..4ef7a74 100644 --- a/app/api/routers/auth.py +++ b/app/api/routers/auth.py @@ -743,10 +743,11 @@ async def mp_qr_start(request: Request, db: Database = Depends(get_db)): png = await wechat.get_wxacode(scene, page="pages/scan-login/index") b64 = base64.b64encode(png).decode("ascii") return {"scene": scene, "qr_image": f"data:image/png;base64,{b64}", "mp_enabled": True, "expires_in": wx_qr_store.TTL_SECONDS} - except wechat.WechatError: + except wechat.WechatError as exc: # 无凭据/失败兜底:返回可扫描的 URL,前端按 qr_url 渲染普通二维码 + # 同时返回错误详情,便于排查(服务器缺少 WECHAT_APPID/SECRET 或微信API调用失败) qr_url = f"https://opc.pinesound.cn/mp/login?scene={scene}" - return {"scene": scene, "qr_url": qr_url, "mp_enabled": False, "expires_in": wx_qr_store.TTL_SECONDS} + return {"scene": scene, "qr_url": qr_url, "mp_enabled": False, "error": str(exc), "expires_in": wx_qr_store.TTL_SECONDS} @router.get("/mp-qr/poll", summary="轮询小程序扫码登录状态") @@ -773,9 +774,9 @@ async def mp_qr_bind_start(request: Request, db: Database = Depends(get_db), use png = await wechat.get_wxacode(scene, page="pages/scan-login/index") b64 = base64.b64encode(png).decode("ascii") return {"scene": scene, "qr_image": f"data:image/png;base64,{b64}", "mp_enabled": True, "expires_in": wx_qr_store.TTL_SECONDS} - except wechat.WechatError: + except wechat.WechatError as exc: qr_url = f"https://opc.pinesound.cn/mp/bind?scene={scene}" - return {"scene": scene, "qr_url": qr_url, "mp_enabled": False, "expires_in": wx_qr_store.TTL_SECONDS} + return {"scene": scene, "qr_url": qr_url, "mp_enabled": False, "error": str(exc), "expires_in": wx_qr_store.TTL_SECONDS} @router.post("/mp-qr/confirm", summary="小程序内确认扫码登录/绑定")