This commit is contained in:
yumoqing 2026-03-03 11:31:41 +08:00
parent 32c1f97a59
commit d764aea2a5

View File

@ -38,7 +38,7 @@ class WOAHandler:
if encoding_aes_key: if encoding_aes_key:
# 微信的 AES Key 是 base64 编码的 32 字节字符串 # 微信的 AES Key 是 base64 编码的 32 字节字符串
self.aes_key = base64.b64decode(encoding_aes_key + "=") self.aes_key = base64.b64decode(encoding_aes_key + "=")
self.encoding_aes_key = self.aes_key self.aes_key = None
self.media_manager = WeChatMediaManager(self.app_id, self.secret) self.media_manager = WeChatMediaManager(self.app_id, self.secret)
def _check_signature(self, signature: str, timestamp: str, nonce: str) -> bool: def _check_signature(self, signature: str, timestamp: str, nonce: str) -> bool:
@ -132,7 +132,7 @@ class WOAHandler:
# 如果开启加密echostr 也需要解密 # 如果开启加密echostr 也需要解密
debug(f'handle_get():{request.query=}') debug(f'handle_get():{request.query=}')
return web.Response(text=echostr) return web.Response(text=echostr)
if self.encoding_aes_key: if self.aes_key:
try: try:
echostr = self._decrypt_msg(echostr) echostr = self._decrypt_msg(echostr)
except Exception as e: except Exception as e:
@ -217,7 +217,7 @@ class WOAHandler:
xml_str = body.decode('utf-8') xml_str = body.decode('utf-8')
# 3. 如果开启加密,先解密 # 3. 如果开启加密,先解密
if self.encoding_aes_key: if self.aes_key:
root = ET.fromstring(xml_str) root = ET.fromstring(xml_str)
encrypt_node = root.find('Encrypt') encrypt_node = root.find('Encrypt')
if encrypt_node is not None and encrypt_node.text: if encrypt_node is not None and encrypt_node.text:
@ -237,7 +237,7 @@ class WOAHandler:
debug(f'收到微信消息:{msg_dict} 返回:{reply_xml}') debug(f'收到微信消息:{msg_dict} 返回:{reply_xml}')
# 5. 如果需要加密回复 # 5. 如果需要加密回复
if self.encoding_aes_key and reply_xml: if self.aes_key and reply_xml:
# 1. 生成新的时间戳和随机数 (不要用请求里的旧数据) # 1. 生成新的时间戳和随机数 (不要用请求里的旧数据)
timestamp = str(int(time.time())) timestamp = str(int(time.time()))
nonce = ''.join([str(random.randint(0, 9)) for _ in range(10)]) nonce = ''.join([str(random.randint(0, 9)) for _ in range(10)])