fix(wechat): QUERY_AUTH_CODE 检测文本用携带的授权码直接换令牌发客服消息,消除 authorized 事件与文本消息 1 秒竞态导致的检测失败
This commit is contained in:
parent
a49a878b5f
commit
5f2daf4e35
@ -522,10 +522,33 @@ class WechatOpenController extends Controller {
|
|||||||
} else if (content.includes('TESTCOMPONENT_MSG_TYPE_TEXT')) {
|
} else if (content.includes('TESTCOMPONENT_MSG_TYPE_TEXT')) {
|
||||||
replyContent = 'TESTCOMPONENT_MSG_TYPE_TEXT_callback';
|
replyContent = 'TESTCOMPONENT_MSG_TYPE_TEXT_callback';
|
||||||
} else if (content.includes('QUERY_AUTH_CODE')) {
|
} else if (content.includes('QUERY_AUTH_CODE')) {
|
||||||
// 自动化测试场景:直接发送客服消息,不生成被动回复
|
// 微信全网发布检测场景:文本携带授权码(QUERY_AUTH_CODE:<code>)。
|
||||||
|
// 检测的 authorized 事件与文本消息几乎同时到达(实测仅差 1 秒),
|
||||||
|
// 不能依赖 authorized 事件已换取令牌并落库,必须用文本中的授权码
|
||||||
|
// 直接调 api_query_auth 换令牌(官方检测意图),再回复客服消息。
|
||||||
noReply = true;
|
noReply = true;
|
||||||
const newContent = content.replace('QUERY_AUTH_CODE:', '');
|
const authCode = content.replace('QUERY_AUTH_CODE:', '').trim();
|
||||||
await this.sendMessage(appid, openid, `${newContent}_from_api`);
|
const client = new WechatOpenClient(ctx);
|
||||||
|
let refreshToken = null;
|
||||||
|
if (authCode) {
|
||||||
|
try {
|
||||||
|
const authorizationInfo = await client.queryAuth(authCode);
|
||||||
|
refreshToken = authorizationInfo.authorizer_refresh_token || null;
|
||||||
|
if (refreshToken) {
|
||||||
|
await client.cacheSet(`App:${appid}:authorizer_refresh_token`, refreshToken, 30 * 24 * 3600);
|
||||||
|
if (authorizationInfo.authorizer_access_token) {
|
||||||
|
await client.cacheSet(
|
||||||
|
`App:${appid}:AuthorizerAccessToken`,
|
||||||
|
authorizationInfo.authorizer_access_token,
|
||||||
|
Math.max((authorizationInfo.expires_in || 7200) - 300, 60)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
ctx.logger.warn(`[wechatOpen.events] QUERY_AUTH_CODE 换取令牌失败: ${e.message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await this.sendMessage(appid, openid, `${authCode}_from_api`, refreshToken);
|
||||||
} else {
|
} else {
|
||||||
replyContent = `收到您的消息:${content}\n我会尽快回复您!`;
|
replyContent = `收到您的消息:${content}\n我会尽快回复您!`;
|
||||||
}
|
}
|
||||||
@ -585,29 +608,30 @@ class WechatOpenController extends Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送客服文本消息(先尝试从 DB 兜底读取 refresh token,避免 Redis 丢失导致失败)。
|
* 发送客服文本消息(refreshToken 优先,其次从 DB 兜底读取,避免 Redis 丢失导致失败)。
|
||||||
* @param {string} appid 授权方 AppID。
|
* @param {string} appid 授权方 AppID。
|
||||||
* @param {string} openid 用户 openid。
|
* @param {string} openid 用户 openid。
|
||||||
* @param {string} content 文本内容。
|
* @param {string} content 文本内容。
|
||||||
|
* @param {string} [refreshToken] 授权方刷新令牌(调用方已获取时传入)。
|
||||||
* @return {Promise<object>} 微信原始响应。
|
* @return {Promise<object>} 微信原始响应。
|
||||||
*/
|
*/
|
||||||
async sendMessage(appid, openid, content) {
|
async sendMessage(appid, openid, content, refreshToken) {
|
||||||
const { ctx, app } = this;
|
const { ctx, app } = this;
|
||||||
const client = new WechatOpenClient(ctx);
|
const client = new WechatOpenClient(ctx);
|
||||||
|
|
||||||
// Redis 中的 refresh token 可能丢失,从 wms_wechat_app 兜底读取
|
// Redis 中的 refresh token 可能丢失,从 wms_wechat_app 兜底读取
|
||||||
let refreshToken = null;
|
let token = refreshToken || null;
|
||||||
try {
|
try {
|
||||||
const record = await app.model.WechatApp.findOne({ where: { appid } });
|
const record = await app.model.WechatApp.findOne({ where: { appid } });
|
||||||
refreshToken = record && record.authorizer_refresh_token
|
if (record && record.authorizer_refresh_token) {
|
||||||
? String(record.authorizer_refresh_token)
|
token = String(record.authorizer_refresh_token);
|
||||||
: null;
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
ctx.logger.warn(`[wechatOpen.events] 读取授权记录失败 appid=${appid}: ${e.message}`);
|
ctx.logger.warn(`[wechatOpen.events] 读取授权记录失败 appid=${appid}: ${e.message}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.logger.info(`[wechatOpen.events] 发送客服消息 appid=${appid} openid=${openid} content=${content}`);
|
ctx.logger.info(`[wechatOpen.events] 发送客服消息 appid=${appid} openid=${openid} content=${content}`);
|
||||||
const result = await client.sendCustomMessage(appid, openid, content, refreshToken);
|
const result = await client.sendCustomMessage(appid, openid, content, token);
|
||||||
ctx.logger.info(`[wechatOpen.events] 客服消息发送结果: ${JSON.stringify(result)}`);
|
ctx.logger.info(`[wechatOpen.events] 客服消息发送结果: ${JSON.stringify(result)}`);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user