This commit is contained in:
2026-01-15 18:08:08 +08:00
parent 5586e98e51
commit 4a51ec89cc
21 changed files with 184 additions and 72 deletions

View File

@@ -1,9 +1,4 @@
from .wx_com import wxcpt
from .wx_utils import get_request_params,decrypt_message,extract_message_content
from .wx_utils import decrypt_message, extract_message_content, get_request_params
__all__ = [
"wxcpt",
"get_request_params",
"decrypt_message",
"extract_message_content"
]
__all__ = ["wxcpt", "get_request_params", "decrypt_message", "extract_message_content"]

View File

@@ -1,6 +1,7 @@
from uvicorn.server import logger
from config import Settings
from utils.wxcom.WXBizMsgCrypt3 import WXBizMsgCrypt
from utils.wxcom.WXBizMsgCrypt3 import WXBizMsgCrypt
def get_wxcpt():
@@ -15,20 +16,20 @@ def get_wxcpt():
required_configs = [
Settings().WECOM_APP_TOKEN,
Settings().WECOM_APP_ENCODING_AES_KEY,
Settings().WECOM_CORPID
Settings().WECOM_CORPID,
]
if not all(required_configs):
raise ValueError("企业微信配置不完整")
return WXBizMsgCrypt(
Settings().WECOM_APP_TOKEN, # 设置的Token
Settings().WECOM_APP_ENCODING_AES_KEY, # 设置密钥
Settings().WECOM_CORPID # 企业ID
Settings().WECOM_CORPID, # 企业ID
)
except Exception as e:
logger.error(f"初始化WXBizMsgCrypt失败: {str(e)}")
raise
wxcpt = get_wxcpt()

View File

@@ -4,6 +4,7 @@ from typing import Dict, Tuple, Union
import xmltodict
from fastapi import HTTPException, Request
from uvicorn.server import logger
from .wx_com import wxcpt
@@ -23,7 +24,10 @@ async def get_request_params(request: Request) -> Tuple[bytes, str, str, str]:
return body, msg_signature, timestamp, nonce
def decrypt_message(body: bytes, msg_signature: str, timestamp: str, nonce: str) -> dict:
def decrypt_message(
body: bytes, msg_signature: str, timestamp: str, nonce: str
) -> dict:
"""解密消息"""
ret, sMsg = wxcpt.DecryptMsg(body, msg_signature, timestamp, nonce)
if ret != 0:
@@ -35,6 +39,7 @@ def decrypt_message(body: bytes, msg_signature: str, timestamp: str, nonce: str)
return xml_dict
def extract_message_content(
xml_dict: Dict,
) -> Tuple[str, str, str, str, Union[Dict[str, Union[str, None]], str, None], str, str]:
@@ -82,7 +87,6 @@ def extract_message_content(
message_data = xml_content.get("Content")
logger.info(f"收到未知类型消息: {message_data}")
return {
"ToUserName": to_user_name,
"FromUserName": from_user_name,
@@ -91,4 +95,4 @@ def extract_message_content(
"MsgId": msg_id,
"AgentID": agent_id,
**message_data,
}
}