Quellcode durchsuchen

登录接口

develop
H Vs vor 2 Monaten
Ursprung
Commit
8dfce88e2e
4 geänderte Dateien mit 75 neuen und 11 gelöschten Zeilen
  1. +2
    -1
      app.py
  2. +69
    -6
      resources/login_resources.py
  3. +1
    -1
      wechat/biz.py
  4. +3
    -3
      wechat/gewe_chat.py

+ 2
- 1
app.py Datei anzeigen

@@ -4,7 +4,7 @@ from resources.messages_resource import MessagesResource
from resources.contacts_resources import DeleteFriendResource,GetFriendsInfoResource from resources.contacts_resources import DeleteFriendResource,GetFriendsInfoResource
from resources.config_reources import GetWxchatConfigResource ,SaveWxchatConfigResource from resources.config_reources import GetWxchatConfigResource ,SaveWxchatConfigResource
from resources.groups_resources import GetGroupsInfoResource from resources.groups_resources import GetGroupsInfoResource
from resources.login_resources import GetLoginInfoResource,GetLoginWxQRCodeResource
from resources.login_resources import GetLoginInfoResource,GetLoginWxQRCodeResource,LoginWxCaptchCodeResource
from resources.sns_resources import SendSNSTextResource,SendSNSImageResource, SendSNSVideoResource from resources.sns_resources import SendSNSTextResource,SendSNSImageResource, SendSNSVideoResource
from common.log import logger, log_exception from common.log import logger, log_exception
from common.interceptors import before_request, after_request, handle_exception from common.interceptors import before_request, after_request, handle_exception
@@ -226,6 +226,7 @@ flask_api.add_resource(GetGroupsInfoResource, '/api/groups/getchatroominfo')


flask_api.add_resource(GetLoginInfoResource, '/api/agent/getlogin') flask_api.add_resource(GetLoginInfoResource, '/api/agent/getlogin')
flask_api.add_resource(GetLoginWxQRCodeResource, '/api/agent/getwxqrcode') flask_api.add_resource(GetLoginWxQRCodeResource, '/api/agent/getwxqrcode')
flask_api.add_resource(LoginWxCaptchCodeResource, '/api/agent/logincaptchcode')


flask_api.add_resource(SendSNSTextResource, '/api/sns/sendtext') flask_api.add_resource(SendSNSTextResource, '/api/sns/sendtext')
flask_api.add_resource(SendSNSImageResource, '/api/sns/sendimages') flask_api.add_resource(SendSNSImageResource, '/api/sns/sendimages')


+ 69
- 6
resources/login_resources.py Datei anzeigen

@@ -4,6 +4,9 @@ from common import redis_helper,utils
from wechat import gewe_chat,biz from wechat import gewe_chat,biz
from common.log import logger, log_exception from common.log import logger, log_exception
import time import time
import threading

from model import Models






@@ -52,9 +55,9 @@ class GetLoginWxQRCodeResource(Resource):
response=jsonify({'code': 501, 'message': msg}) response=jsonify({'code': 501, 'message': msg})
response.status_code = 501 response.status_code = 501
return response return response
expried_time=int(time.time())+800
flag=gewe_chat.wxchat.acquire_login_lock(token_id,800)
now=time.time()
expried_time=int(now)+150
flag=gewe_chat.wxchat.acquire_login_lock(token_id,150)
if not flag: if not flag:
msg=f'手机号{tel}, wx_token{token_id} 登录进行中,稍后再试' msg=f'手机号{tel}, wx_token{token_id} 登录进行中,稍后再试'
logger.info(msg) logger.info(msg)
@@ -63,7 +66,6 @@ class GetLoginWxQRCodeResource(Resource):
return response return response
app_id=loginfo.get('app_id','') app_id=loginfo.get('app_id','')

qr_code = gewe_chat.wxchat.get_login_qr_code(token_id, app_id,region_id) qr_code = gewe_chat.wxchat.get_login_qr_code(token_id, app_id,region_id)
base64_string = qr_code.get('qrImgBase64') base64_string = qr_code.get('qrImgBase64')
uuid = qr_code.get('uuid') uuid = qr_code.get('uuid')
@@ -75,7 +77,12 @@ class GetLoginWxQRCodeResource(Resource):
response.status_code = 501 response.status_code = 501
return response return response


gewe_chat.wxchat.qrCallback(uuid,base64_string)
hash_key = f"__AI_OPS_WX__:LOGININFO:{tel}"
thread = threading.Thread(target=waitting_login_result, args=(gewe_chat.wxchat,token_id, app_id,region_id, agent_token_id,hash_key, uuid,now))
thread.daemon = True
thread.start()

data={ data={
"tokenId": token_id, "tokenId": token_id,
"tel": tel, "tel": tel,
@@ -83,4 +90,60 @@ class GetLoginWxQRCodeResource(Resource):
"expiredTime": expried_time, "expiredTime": expried_time,
} }
return jsonify(data) return jsonify(data)




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', {})

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())})
else:
login_info.update({"modify_at":int(time.time())})
# if 'appId' in cache_login_info:
# login_info.update({"reg_time":datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3],"login_time":datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]})
# else:
# login_info.update({"login_time":datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]})
cleaned_login_info = {k: (v if v is not None else '') for k, v in login_info.items()}
redis_helper.redis_helper.set_hash(hash_key, cleaned_login_info)
wxchat.release_login_lock(token_id)
# 默认配置
config=Models.AgentConfig.model_validate({
"chatroomIdWhiteList": [],
"agentTokenId": agent_token_id,
"agentEnabled": True,
"addContactsFromChatroomIdWhiteList": [],
"chatWaitingMsgEnabled": True
})

config_dict=config.model_dump()
wxid=cleaned_login_info.get('wxid',agent_tel)
wxchat.save_wxchat_config(wxid,config_dict)
else:
logger.info(f"登录检查中: {ret}-{msg}-{res}")

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

+ 1
- 1
wechat/biz.py Datei anzeigen

@@ -319,7 +319,7 @@ def login_or_reconnect(wxchat:gewe_chat.GeWeChatCom, token_id, app_id, region_id
if now- start_time > 150: #150 秒 二维码失效 if now- start_time > 150: #150 秒 二维码失效
break break
logger.info(f"{token_id} 使用 {app_id},等待扫码登录,二维码有效时间 {150 - int(now - start_time)} 秒") logger.info(f"{token_id} 使用 {app_id},等待扫码登录,二维码有效时间 {150 - int(now - start_time)} 秒")
captch_code = wxchat.get_login_wx_captch_code_to_cache(token_id)
captch_code = wxchat.get_login_wx_captch_code_from_cache(token_id)
captch_code= captch_code if captch_code else '' captch_code= captch_code if captch_code else ''
ret,msg,res = wxchat.check_login(token_id, app_id, uuid,captch_code) ret,msg,res = wxchat.check_login(token_id, app_id, uuid,captch_code)
if ret == 200: if ret == 200:


+ 3
- 3
wechat/gewe_chat.py Datei anzeigen

@@ -91,7 +91,7 @@ class GeWeChatCom:
response = requests.post(url=api_url, headers=headers, data=json.dumps(data)) response = requests.post(url=api_url, headers=headers, data=json.dumps(data))
response_data = response.json() response_data = response.json()
data=json.dumps(response_data, separators=(',', ':'),ensure_ascii=False) data=json.dumps(response_data, separators=(',', ':'),ensure_ascii=False)
logger.info(f'{token_id} 的登录APP信息:{data}')
#logger.info(f'{token_id} 的登录APP信息:{data}')
return response_data.get('data') return response_data.get('data')
def qrCallback(self,uuid, base64_string): def qrCallback(self,uuid, base64_string):
@@ -964,9 +964,9 @@ class GeWeChatCom:


def save_login_wx_captch_code_to_cache(self,token_id,captch_code): def save_login_wx_captch_code_to_cache(self,token_id,captch_code):
hash_key = f"__AI_OPS_WX__:WXCAPTCHCODE:{token_id}" hash_key = f"__AI_OPS_WX__:WXCAPTCHCODE:{token_id}"
redis_helper.redis_helper.set_hash(hash_key,{"data":captch_code},15)
redis_helper.redis_helper.set_hash(hash_key,{"data":captch_code},30)


def get_login_wx_captch_code_to_cache(self,token_id)->str:
def get_login_wx_captch_code_from_cache(self,token_id)->str:
hash_key = f"__AI_OPS_WX__:WXCAPTCHCODE:{token_id}" hash_key = f"__AI_OPS_WX__:WXCAPTCHCODE:{token_id}"
r=redis_helper.redis_helper.get_hash_field(hash_key,"data") r=redis_helper.redis_helper.get_hash_field(hash_key,"data")
return r return r


Laden…
Abbrechen
Speichern