This commit is contained in:
yumoqing 2026-03-03 16:14:08 +08:00
parent 14c85ca342
commit 9ede3ae93d

View File

@ -178,27 +178,27 @@ class WOAHandler:
rzt_dic.media_id = await self.media_manager.upload_media(rzt_dic.msgtype, rzt_dic.media_filepath) rzt_dic.media_id = await self.media_manager.upload_media(rzt_dic.msgtype, rzt_dic.media_filepath)
return rzt_dic return rzt_dic
async def build_reply(self, msg:DictObject, rzt_msg:DictObject): async def build_reply(self, msg:DictObject, rzt_msg:DictObject, timestamp):
""" """
ImageReply, VoiceReply, VideoReply, ImageReply, VoiceReply, VideoReply,
ArticlesReply, MusicReply ArticlesReply, MusicReply
""" """
if rzt_msg.msgtype == 'text': if rzt_msg.msgtype == 'text':
reply = ReplyBuilder.text(msg, content=rzt_msg.content) reply = ReplyBuilder.text(msg, content=rzt_msg.content, create_time=timestamp)
return reply return reply
if rzt_msg.msgtype in ['image', 'video', 'voice']: if rzt_msg.msgtype in ['image', 'video', 'voice']:
rzt_msg.media_id = await self.upload_media(rzt_msg.msgtype, rzt_msg.media_id = await self.upload_media(rzt_msg.msgtype,
rzt_msg.media_file) rzt_msg.media_file)
if rzt_msg.msgtype == 'image': if rzt_msg.msgtype == 'image':
reply = ReplyBuilder.image(msg, media_id=rzt_msg.media_id) reply = ReplyBuilder.image(msg, media_id=rzt_msg.media_id, create_time=timestamp)
return reply return reply
if rzt_msg.msgtype == 'voice': if rzt_msg.msgtype == 'voice':
reply = ReplyBuilder.voice(msg, media_id=rzt_msg.media_id) reply = ReplyBuilder.voice(msg, media_id=rzt_msg.media_id, create_time=timestamp)
return reply return reply
if rzt_msg.msgtype == 'video': if rzt_msg.msgtype == 'video':
reply = ReplyBuilder.video(msg, title=rzt_msg.title, reply = ReplyBuilder.video(msg, title=rzt_msg.title,
media_id=rzt_msg.media_id, media_id=rzt_msg.media_id,
description=rzt_msg.description) description=rzt_msg.description, create_time=timestamp)
return reply return reply
if rzt_msg.msgtype == 'music': if rzt_msg.msgtype == 'music':
reply = ReplyBuilder.music(msg, reply = ReplyBuilder.music(msg,
@ -207,10 +207,10 @@ class WOAHandler:
music_url=rzt_msg.music_url, # 核心:播放链接 music_url=rzt_msg.music_url, # 核心:播放链接
hq_music_url=rzt_msg.hq_music_url, # 核心:高质量链接 hq_music_url=rzt_msg.hq_music_url, # 核心:高质量链接
thumb_media_id=media_id # 核心:封面图 media_id thumb_media_id=media_id # 核心:封面图 media_id
) , create_time=timestamp)
return reply return reply
if rzt_msg.msgtype == 'news': if rzt_msg.msgtype == 'news':
reply = ReplyBuilder.news(msg, rzt_msg.articles) reply = ReplyBuilder.news(msg, rzt_msg.articles, create_time=timestamp)
return reply return reply
async def handle_post(self, request: web.Request) -> web.Response: async def handle_post(self, request: web.Request) -> web.Response:
@ -250,14 +250,14 @@ class WOAHandler:
# --- 业务逻辑 --- # --- 业务逻辑 ---
reply_dic = await self.msghandle(msg_dict) reply_dic = await self.msghandle(msg_dict)
debug(f'返回的消息:{reply_dic}') debug(f'返回的消息:{reply_dic}')
reply_xml = await self.build_reply(msg_dict, reply_dic) timestamp = str(int(time.time()))
nonce = ''.join([str(random.randint(0, 9)) for _ in range(10)])
reply_xml = await self.build_reply(msg_dict, reply_dic, timestamp)
debug(f'收到微信消息:{msg_dict} 返回:{reply_xml}') debug(f'收到微信消息:{msg_dict} 返回:{reply_xml}')
# 5. 如果需要加密回复 # 5. 如果需要加密回复
if self.aes_key and reply_xml: if self.aes_key and reply_xml:
# 1. 生成新的时间戳和随机数 (不要用请求里的旧数据) # 1. 生成新的时间戳和随机数 (不要用请求里的旧数据)
timestamp = str(int(time.time()))
nonce = ''.join([str(random.randint(0, 9)) for _ in range(10)])
encrypted_xml_str = self._encrypt_msg(reply_xml, nonce, timestamp) encrypted_xml_str = self._encrypt_msg(reply_xml, nonce, timestamp)
try: try:
decode_data = self._decrypt_msg(encrypted_xml_str) decode_data = self._decrypt_msg(encrypted_xml_str)