This commit is contained in:
eric
2026-03-31 11:27:39 -05:00
parent d16f188845
commit 80a4b2d0f9
5 changed files with 33 additions and 16 deletions

View File

@@ -597,10 +597,15 @@ async def _handle_wechat_request(request: Request, app_id: Optional[str], backgr
logger.info("回包明文(加密前): %s", resp_json_str)
# 8. 加密响应
# XML 回调场景使用当前会话 appid(公众号 appid加密避免与开放 API appid 不一致导致平台解密失败。
response_app_id = APP_ID
# - /?app_id=... 的第三方服务接口请求通常使用纯AES明文加密不拼接 appid
# - XML 回调场景使用会话 appid 封装加密
# - 其余 JSON 场景默认使用纯AES提升兼容性
response_app_id: Optional[str] = None
if str(data.get("_payload_format", "")) == "xml":
response_app_id = str(data.get("ChannelId", "") or data.get("appid", "")).strip() or APP_ID
elif app_id:
response_app_id = None
logger.info("响应加密模式: %s", "envelope" if response_app_id else "plain")
try:
encrypted_resp = aes_encrypt_base64(resp_json_str, ENCODING_AES_KEY, response_app_id)
except Exception as e: