This commit is contained in:
aaron 2025-02-27 18:18:27 +08:00
parent a9a615ea74
commit 61c63678e4

View File

@ -53,15 +53,10 @@ class WecomClient:
if not token:
return None
url = f"https://qyapi.weixin.qq.com/cgi-bin/miniprogram/jscode2session?access_token={token}"
data = {
"js_code": code,
"grant_type": "authorization_code"
}
url = f"https://qyapi.weixin.qq.com/cgi-bin/miniprogram/jscode2session?access_token={token}&js_code={code}&grant_type=authorization_code"
try:
response = requests.post(url, json=data)
response = requests.get(url)
result = response.json()
if result.get("errcode") == 0:
@ -70,61 +65,11 @@ class WecomClient:
"pending_id": result.get("pending_id")
}
else:
logging.error(f"小程序openid转换失败: {result}")
logging.error(f"小程序code转换失败: {result}")
return None
except Exception as e:
logging.exception(f"小程序openid转换异常: {str(e)}")
logging.exception(f"小程序code转换异常: {str(e)}")
return None
async def get_user_in_group_chat(self, userid: str) -> List[str]:
"""
获取用户所在的群聊列表
Args:
userid: 企业微信用户ID
Returns:
List[str]: 群聊ID列表
"""
token = await self.get_access_token()
if not token:
return []
url = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/list_group_chat?access_token={token}"
# 先获取所有群聊
try:
response = requests.post(url, json={})
result = response.json()
if result.get("errcode") != 0:
logging.error(f"获取群聊列表失败: {result}")
return []
chat_ids = []
for chat in result.get("group_chat_list", []):
chat_id = chat.get("chat_id")
# 获取群聊详情
detail_url = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/get?access_token={token}"
detail_response = requests.post(detail_url, json={"chat_id": chat_id})
detail_result = detail_response.json()
if detail_result.get("errcode") == 0:
group_chat = detail_result.get("group_chat", {})
members = group_chat.get("member_list", [])
# 检查用户是否在群内
for member in members:
if member.get("userid") == userid:
chat_ids.append(chat_id)
break
return chat_ids
except Exception as e:
logging.exception(f"获取用户群聊异常: {str(e)}")
return []
wecom_client = WecomClient()