From d02690cc4d002e4e7fb0402e5c0e51c7e71eafd0 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Mon, 12 Oct 2026 20:06:41 +0800 Subject: [PATCH] =?UTF-8?q?fix(wechat):=20QUERY=5FAUTH=5FCODE=20=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E8=A2=AB=E5=8A=A8=E5=9B=9E=E5=A4=8D+=E5=AE=A2?= =?UTF-8?q?=E6=9C=8D=E6=B6=88=E6=81=AF=E5=8F=8C=E9=80=9A=E9=81=93=EF=BC=8C?= =?UTF-8?q?=E8=A6=86=E7=9B=96=E6=A3=80=E6=B5=8B=E8=84=9A=E6=9C=AC=E6=8C=89?= =?UTF-8?q?=E7=B2=89=E4=B8=9D=E6=94=B6=E4=BF=A1=E5=88=A4=E5=AE=9A=E7=9A=84?= =?UTF-8?q?=E5=8F=A3=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/wechatOpen.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/app/controller/wechatOpen.js b/app/controller/wechatOpen.js index c0d4306..8e39fff 100644 --- a/app/controller/wechatOpen.js +++ b/app/controller/wechatOpen.js @@ -565,7 +565,6 @@ class WechatOpenController extends Controller { try { const lowerContent = content.toLowerCase(); let replyContent = ''; - let noReply = false; // 关键词回复(与参考实现保持一致) if (content.includes('你好') || lowerContent.includes('hello')) { @@ -577,14 +576,16 @@ class WechatOpenController extends Controller { ctx.logger.warn(`[wechatOpen.events] 命中官方检测步骤2-公众号被动回复 appid=${appid} type=${detectAccountType(appid)}`); } else if (content.includes('QUERY_AUTH_CODE')) { // 微信全网发布检测场景(官方步骤3=公众号客服/步骤4=小程序客服):文本携带授权码 - // (QUERY_AUTH_CODE:)。检测的 authorized 事件与文本消息几乎同时到达(实测仅差 - // 1 秒),不能依赖 authorized 已换取令牌,必须用文本中的授权码调 api_query_auth。 - // 官方要求「5 秒内返回空串」:客服消息流程放入后台异步执行,主流程立即返回。 - noReply = true; + // (QUERY_AUTH_CODE:)。双通道兜底: + // 1) 被动回复 _from_api(步骤4 文档未规定响应体,被动回复能让模拟粉丝直接收到消息); + // 2) 后台异步发送客服消息(含前缀/去前缀双发,官方要求走客服消息接口)。 const authCode = content.replace('QUERY_AUTH_CODE:', '').trim(); const accountType = detectAccountType(appid); ctx.logger.warn(`[wechatOpen.events] 命中官方检测步骤3/4-客服消息回复 appid=${appid} type=${accountType} code=${authCode}`); + // 被动回复内容(官方文档字面口径:QUERY_AUTH_CODE: 后的实际值 + _from_api) + replyContent = `${authCode}_from_api`; + ctx.runInBackground(async () => { const client = new WechatOpenClient(ctx); let refreshToken = null; @@ -630,9 +631,8 @@ class WechatOpenController extends Controller { ctx.logger.warn(`[wechatOpen.events] 客服回复令牌来源 source=${tokenSource || 'none'} appid=${appid} has_token=${!!refreshToken}`); - // 回复内容双发兼容:官方检测文本 QUERY_AUTH_CODE:queryauthcode@@@, - // 官方文档字面要求回复 _from_api;但检测脚本可能按 - // 「去掉 queryauthcode@@@ 标记前缀的真实 code」核对,两条都发确保命中。 + // 客服消息双发兼容:官方文档字面要求回复 _from_api; + // 检测脚本可能按「去掉 queryauthcode@@@ 标记前缀的真实 code」核对,两条都发确保命中。 const codeValue = authCode.trim(); const pureCode = codeValue.startsWith('queryauthcode@@@') ? codeValue.slice('queryauthcode@@@'.length) @@ -650,8 +650,6 @@ class WechatOpenController extends Controller { replyContent = `收到您的消息:${content}\n我会尽快回复您!`; } - if (noReply) return EMPTY_REPLY; - // 组装明文回复 XML(ToUserName=用户 openid,FromUserName=公众号原始 ID) const plainXml = '' + ``