From 06a2126e2e0aeb33fb63ff4730d7f27a7ddf44f1 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Mon, 12 Oct 2026 18:47:25 +0800 Subject: [PATCH] =?UTF-8?q?fix(wechat):=20authorized=20=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E6=8D=A2=E5=8F=96=E4=BB=A4=E7=89=8C=E5=A4=B1=E8=B4=A5=E4=B8=8D?= =?UTF-8?q?=E5=86=8D=E6=8A=9B=E9=94=99=E9=87=8D=E8=AF=95=EF=BC=8C=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E4=B8=8E=20QUERY=5FAUTH=5FCODE=20=E6=96=87=E6=9C=AC?= =?UTF-8?q?=E7=AB=9E=E4=BA=89=E6=B6=88=E8=B4=B9=E6=8E=88=E6=9D=83=E7=A0=81?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E3=80=8C=E6=8E=88=E6=9D=83=E6=88=90=E5=8A=9F?= =?UTF-8?q?=E3=80=8D=E6=A3=80=E6=B5=8B=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/wechatOpen.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/app/controller/wechatOpen.js b/app/controller/wechatOpen.js index f38605a..dc15817 100644 --- a/app/controller/wechatOpen.js +++ b/app/controller/wechatOpen.js @@ -232,8 +232,18 @@ class WechatOpenController extends Controller { // 暂存授权码(供后续换取令牌使用) await client.saveAuthorizationCode(authorizerAppid, authorizationCode); - // 用授权码换取授权方令牌与权限集合 - const authorizationInfo = await client.queryAuth(authorizationCode); + // 用授权码换取授权方令牌与权限集合。 + // 注意:同一 authorization_code 只能 queryAuth 成功一次。微信全网发布检测时, + // authorized 事件与 QUERY_AUTH_CODE 文本消息几乎同时推送(实测仅差 1 秒), + // 文本分支也会用同一 code 调 queryAuth——谁先到谁消费。此处失败不抛错重试, + // 令牌可由文本分支/授权回跳链路换得,保证「授权成功」检测项始终响应 success。 + let authorizationInfo = null; + try { + authorizationInfo = await client.queryAuth(authorizationCode); + } catch (e) { + ctx.logger.warn(`[wechatOpen] ${infoType} 换取授权方令牌失败(可能已被 QUERY_AUTH_CODE 文本消费): ${e.message}`); + return; + } const nowSec = Math.floor(Date.now() / 1000); const fields = { authorizer_refresh_token: authorizationInfo.authorizer_refresh_token || null,