H Vs 2 місяці тому
джерело
коміт
d7ffe98744
2 змінених файлів з 82 додано та 58 видалено
  1. +63
    -58
      resources/login_resources.py
  2. +19
    -0
      resources/messages_resource.py

+ 63
- 58
resources/login_resources.py Переглянути файл

@@ -65,7 +65,7 @@ class GetLoginWxQRCodeResource(Resource):
response.status_code = 501
return response
app_id=loginfo.get('app_id','')
app_id=loginfo.get('appId','')
qr_code = gewe_chat.wxchat.get_login_qr_code(token_id, app_id,region_id)
base64_string = qr_code.get('qrImgBase64')
uuid = qr_code.get('uuid')
@@ -77,8 +77,10 @@ class GetLoginWxQRCodeResource(Resource):
response.status_code = 501
return response
if app_id=='':
app_id=qr_code.get('appId')
# if app_id=='':
# app_id=qr_code.get('appId')

app_id = app_id or qr_code.get('appId')

gewe_chat.wxchat.qrCallback(uuid,base64_string)
hash_key = f"__AI_OPS_WX__:LOGININFO:{tel}"
@@ -99,58 +101,61 @@ class GetLoginWxQRCodeResource(Resource):

def waitting_login_result(wxchat:gewe_chat.GeWeChatCom, token_id, app_id,region_id, agent_token_id,hash_key, uuid,start_time):
agent_tel=hash_key.split(":")[-1]
while True:
now = time.time()
if now - start_time > 150:
logger.info(f'{token_id} 使用 {app_id} 扫二维码登录超时')
break

logger.info(f"{token_id} 使用 {app_id},等待扫码登录,二维码有效时间 {150 - int(now - start_time)} 秒")
captch_code = wxchat.get_login_wx_captch_code_from_cache(token_id)
captch_code= captch_code if captch_code else ''
logger.info(f"{token_id} 使用 {app_id} 的验证码 {captch_code}")
ret,msg,res = wxchat.check_login(token_id, app_id, uuid,captch_code)

if ret == 200:
flag = res.get('status')
if flag == 2:
logger.info(f"登录成功: {res}")
head_img_url=res.get('headImgUrl','')
login_info = res.get('loginInfo', {})

wxid=login_info.get('wxid',agent_tel)

login_info.update({'appId': app_id, 'uuid': uuid, 'tokenId': token_id,'status': 1,'headImgUrl':head_img_url,'regionId':region_id})
cache_login_info=redis_helper.redis_helper.get_hash(hash_key)
if 'appId' not in cache_login_info:
login_info.update({"create_at":int(time.time()),"modify_at":int(time.time())})
# 默认配置
config=Models.AgentConfig.model_validate({
"chatroomIdWhiteList": [],
"agentTokenId": agent_token_id,
"agentEnabled": False,
"addContactsFromChatroomIdWhiteList": [],
"chatWaitingMsgEnabled": True
})
else:
login_info.update({"modify_at":int(time.time())})
# 已有配置
config_cache=wxchat.get_wxchat_config_from_cache(wxid)
config=Models.AgentConfig.model_validate(config_cache)
cleaned_login_info = {k: (v if v is not None else '') for k, v in login_info.items()}
#wxid=cleaned_login_info.get('wxid',agent_tel)
# 保存配置信息
config_dict=config.model_dump()
wxchat.save_wxchat_config(wxid,config_dict)

# 保存登录信息
redis_helper.redis_helper.set_hash(hash_key, cleaned_login_info)
wxchat.release_login_lock(token_id)
break
else:
logger.info(f"登录检查中: {ret}-{msg}-{res}")

time.sleep(5)
wxchat.release_login_lock(token_id)
try:
while True:
now = time.time()
if now - start_time > 150:
logger.info(f'{token_id} 使用 {app_id} 扫二维码登录超时')
break

logger.info(f"{token_id} 使用 {app_id},等待扫码登录,二维码有效时间 {150 - int(now - start_time)} 秒")
captch_code = wxchat.get_login_wx_captch_code_from_cache(token_id)
captch_code= captch_code if captch_code else ''
logger.info(f"{token_id} 使用 {app_id} 的验证码 {captch_code}")
ret,msg,res = wxchat.check_login(token_id, app_id, uuid,captch_code)

if ret == 200:
flag = res.get('status')
if flag == 2:
logger.info(f"登录成功: {res}")
head_img_url=res.get('headImgUrl','')
login_info = res.get('loginInfo', {})

wxid=login_info.get('wxid',agent_tel)

login_info.update({'appId': app_id, 'uuid': uuid, 'tokenId': token_id,'status': 1,'headImgUrl':head_img_url,'regionId':region_id})
cache_login_info=redis_helper.redis_helper.get_hash(hash_key)
if 'appId' not in cache_login_info:
login_info.update({"create_at":int(time.time()),"modify_at":int(time.time())})
# 默认配置
config=Models.AgentConfig.model_validate({
"chatroomIdWhiteList": [],
"agentTokenId": agent_token_id,
"agentEnabled": False,
"addContactsFromChatroomIdWhiteList": [],
"chatWaitingMsgEnabled": True
})
else:
login_info.update({"modify_at":int(time.time())})
# 已有配置
config_cache=wxchat.get_wxchat_config_from_cache(wxid)
config=Models.AgentConfig.model_validate(config_cache)
cleaned_login_info = {k: (v if v is not None else '') for k, v in login_info.items()}
#wxid=cleaned_login_info.get('wxid',agent_tel)
# 保存配置信息
config_dict=config.model_dump()
wxchat.save_wxchat_config(wxid,config_dict)

# 保存登录信息
redis_helper.redis_helper.set_hash(hash_key, cleaned_login_info)
wxchat.release_login_lock(token_id)
break
else:
logger.info(f"登录检查中: {ret}-{msg}-{res}")

time.sleep(5)
finally:
wxchat.release_login_lock(token_id)


+ 19
- 0
resources/messages_resource.py Переглянути файл

@@ -71,6 +71,7 @@ class MessagesResource(Resource):
1: handle_text,
3: handle_image,
34: handle_voice,
42: handle_name_card,
49: handle_xml,
37: handle_add_friend_notice,
10002: handle_10002_msg
@@ -133,7 +134,25 @@ class MessagesResource(Resource):

return jsonify({"message": "收到微信回调消息"})

def handle_name_card(token_id,app_id, wxid,msg_data,from_wxid, to_wxid):
logger.info('名片消息')
msg_content_xml=msg_data["Content"]["string"]

# 解析XML字符串
root = ET.fromstring(msg_content_xml)

# 提取alias属性
alias_value = root.get("alias")

def handle_self_cmd(wxid,msg):
'''
个人微信命令处理
如果是个人微信的指令,wxid == from_wxid
commands = {
'启用托管': True,
'关闭托管': False
}
'''
msg_data=msg.get("Data")
from_wxid=msg_data["FromUserName"]["string"]
msg_content=msg_data["Content"]["string"]


Завантаження…
Відмінити
Зберегти