您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

146 行
5.5KB

  1. from flask_restful import Resource, reqparse
  2. from flask import jsonify,request,json
  3. from common import redis_helper,utils
  4. from wechat import gewe_chat,biz
  5. from common.log import logger, log_exception
  6. import time
  7. import threading
  8. from model import Models
  9. class GetLoginInfoResource(Resource):
  10. def __init__(self):
  11. self.parser = reqparse.RequestParser()
  12. self.wxchat = gewe_chat.wxchat
  13. def post(self):
  14. req = request.get_json()
  15. tel = req.get("tel")
  16. config=self.wxchat.get_login_info_from_cache(tel)
  17. return jsonify(config)
  18. class LoginWxCaptchCodeResource(Resource):
  19. def __init__(self):
  20. self.parser = reqparse.RequestParser()
  21. self.wxchat = gewe_chat.wxchat
  22. def post(self):
  23. req = request.get_json()
  24. token_id = req.get("token_id")
  25. captch_code= req.get("captch_code")
  26. res=self.wxchat.save_login_wx_captch_code_to_cache(token_id,captch_code)
  27. return jsonify(res)
  28. class GetLoginWxQRCodeResource(Resource):
  29. def __init__(self):
  30. self.parser = reqparse.RequestParser()
  31. self.wxchat = gewe_chat.wxchat
  32. def post(self):
  33. req = request.get_json()
  34. token_id = req.get("tokenId")
  35. tel= req.get("tel")
  36. region_id= req.get("regionId")
  37. agent_token_id= req.get("agentTokenId")
  38. loginfo=gewe_chat.wxchat.get_login_info_from_cache(tel)
  39. status=loginfo.get('status','0')
  40. if status=='1':
  41. msg=f'手机号{tel},wx_token{token_id} 已经微信登录,终止登录流程'
  42. logger.info(msg)
  43. response=jsonify({'code': 501, 'message': msg})
  44. response.status_code = 501
  45. return response
  46. now=time.time()
  47. expried_time=int(now)+150
  48. flag=gewe_chat.wxchat.acquire_login_lock(token_id,150)
  49. if not flag:
  50. msg=f'手机号{tel}, wx_token{token_id} 登录进行中,稍后再试'
  51. logger.info(msg)
  52. response=jsonify({'code': 501, 'message': msg})
  53. response.status_code = 501
  54. return response
  55. app_id=loginfo.get('app_id','')
  56. qr_code = gewe_chat.wxchat.get_login_qr_code(token_id, app_id,region_id)
  57. base64_string = qr_code.get('qrImgBase64')
  58. uuid = qr_code.get('uuid')
  59. if not uuid:
  60. msg=f"uuid获取二维码失败,uuid: {uuid}"
  61. gewe_chat.wxchat.release_login_lock(token_id)
  62. logger.info(msg)
  63. response=jsonify({'code': 501, 'message': msg})
  64. response.status_code = 501
  65. return response
  66. gewe_chat.wxchat.qrCallback(uuid,base64_string)
  67. hash_key = f"__AI_OPS_WX__:LOGININFO:{tel}"
  68. thread = threading.Thread(target=waitting_login_result, args=(gewe_chat.wxchat,token_id, app_id,region_id, agent_token_id,hash_key, uuid,now))
  69. thread.daemon = True
  70. thread.start()
  71. data={
  72. "tokenId": token_id,
  73. "tel": tel,
  74. "base64Img": base64_string,
  75. "expiredTime": expried_time,
  76. }
  77. return jsonify(data)
  78. def waitting_login_result(wxchat:gewe_chat.GeWeChatCom, token_id, app_id,region_id, agent_token_id,hash_key, uuid,start_time):
  79. agent_tel=hash_key.split(":")[-1]
  80. while True:
  81. now = time.time()
  82. if now - start_time > 150:
  83. logger.info(f'{token_id} 使用 {app_id} 扫二维码登录超时')
  84. break
  85. logger.info(f"{token_id} 使用 {app_id},等待扫码登录,二维码有效时间 {150 - int(now - start_time)} 秒")
  86. captch_code = wxchat.get_login_wx_captch_code_from_cache(token_id)
  87. captch_code= captch_code if captch_code else ''
  88. logger.info(f"{token_id} 使用 {app_id} 的验证码 {captch_code}")
  89. ret,msg,res = wxchat.check_login(token_id, app_id, uuid,captch_code)
  90. if ret == 200:
  91. flag = res.get('status')
  92. if flag == 2:
  93. logger.info(f"登录成功: {res}")
  94. head_img_url=res.get('headImgUrl','')
  95. login_info = res.get('loginInfo', {})
  96. login_info.update({'appId': app_id, 'uuid': uuid, 'tokenId': token_id,'status': 1,'headImgUrl':head_img_url,'regionId':region_id})
  97. cache_login_info=redis_helper.redis_helper.get_hash(hash_key)
  98. if 'appId' not in cache_login_info:
  99. login_info.update({"create_at":int(time.time()),"modify_at":int(time.time())})
  100. else:
  101. login_info.update({"modify_at":int(time.time())})
  102. cleaned_login_info = {k: (v if v is not None else '') for k, v in login_info.items()}
  103. redis_helper.redis_helper.set_hash(hash_key, cleaned_login_info)
  104. wxchat.release_login_lock(token_id)
  105. # 默认配置
  106. config=Models.AgentConfig.model_validate({
  107. "chatroomIdWhiteList": [],
  108. "agentTokenId": agent_token_id,
  109. "agentEnabled": True,
  110. "addContactsFromChatroomIdWhiteList": [],
  111. "chatWaitingMsgEnabled": True
  112. })
  113. config_dict=config.model_dump()
  114. wxid=cleaned_login_info.get('wxid',agent_tel)
  115. wxchat.save_wxchat_config(wxid,config_dict)
  116. else:
  117. logger.info(f"登录检查中: {ret}-{msg}-{res}")
  118. time.sleep(5)
  119. wxchat.release_login_lock(token_id)