Commit 496df7be by baiquan

fix(login): 修复登录流程并优化监听逻辑

- 在启动浏览器后添加了对 '/loginv1/callback' 路径的监听
- 实现了 wait_login_callback 函数以等待登录回调并获取 cookie
- 优化了 listen_check_login 函数的逻辑,提高了登录成功判断的准确性
parent 0095c18e
...@@ -52,9 +52,7 @@ async def page_login(browser_id): ...@@ -52,9 +52,7 @@ async def page_login(browser_id):
raise AppError(f"{browser_id}-->启动浏览器失败") raise AppError(f"{browser_id}-->启动浏览器失败")
tab.get('https://fxg.jinritemai.com/login/common') tab.get('https://fxg.jinritemai.com/login/common')
tab.listen.start(["account_login/v2/", "captcha/verify", "/aff/check_login", "/loginv1/callback"])
tab.listen.start(["account_login/v2/", "captcha/verify", "/aff/check_login"])
login_res = listen_check_login(tab) login_res = listen_check_login(tab)
logger.info(f"{browser_id}当前页面是否登录-->{login_res}") logger.info(f"{browser_id}当前页面是否登录-->{login_res}")
if not login_res: if not login_res:
...@@ -92,6 +90,7 @@ async def page_login(browser_id): ...@@ -92,6 +90,7 @@ async def page_login(browser_id):
click_button(tab, ".auxo-checkbox") # 点击勾选框 click_button(tab, ".auxo-checkbox") # 点击勾选框
click_button(tab, ".account-center-submit") # 点击登录按钮 click_button(tab, ".account-center-submit") # 点击登录按钮
listen_login(tab) listen_login(tab)
wait_login_callback(tab)
break break
except ElementNotFoundError as e: except ElementNotFoundError as e:
if tab.title == "首页": if tab.title == "首页":
...@@ -128,6 +127,17 @@ def set_token(tab): ...@@ -128,6 +127,17 @@ def set_token(tab):
else: else:
raise AppError("未找到 token") raise AppError("未找到 token")
def wait_login_callback(tab):
for packet in tab.listen.steps(timeout=10):
if '/loginv1/callback' in packet.url:
headers = packet.response.headers
set_cookie = headers['set-cookie']
if "PHPSESSID" in set_cookie:
logger.info(f"响应的cookie --> {set_cookie}")
return
raise AppError("登录失败 --> 未获取到回调的cookie")
def listen_login(tab): def listen_login(tab):
for packet in tab.listen.steps(timeout=10): for packet in tab.listen.steps(timeout=10):
if 'account_login/v2/' in packet.url: if 'account_login/v2/' in packet.url:
...@@ -139,19 +149,20 @@ def listen_login(tab): ...@@ -139,19 +149,20 @@ def listen_login(tab):
verify_captcha(tab) verify_captcha(tab)
elif login_res['description'] == '': elif login_res['description'] == '':
logger.info("登录成功") logger.info("登录成功")
break return
else: else:
raise AppError(f"登录失败 {login_res['description']}") raise AppError(f"登录失败 {login_res['description']}")
raise AppError("登录失败-->未获取到登录数据") raise AppError("登录失败-->未获取到登录数据")
def listen_check_login(tab): def listen_check_login(tab):
check_result = True check_result = False
for packet in tab.listen.steps(timeout=5): for packet in tab.listen.steps(timeout=5):
if '/aff/check_login' in packet.url and packet.response.body: if '/aff/check_login' in packet.url and packet.response.body:
login_res = packet.response.body login_res = packet.response.body
logger.info(f'获取确认登录数据:{login_res}') logger.info(f'获取确认登录数据:{login_res}')
if login_res['description'] != '' or not login_res['deputy_has_login'] or login_res['error_code'] != 0 or not login_res['subject_has_login']: if (login_res['deputy_has_login'] and login_res['subject_has_login'] and login_res['error_code'] == 0
check_result = False and login_res.get('sec_subject_uid') and login_res.get('sec_user_id')):
check_result = True
return check_result return check_result
def verify_captcha(tab): def verify_captcha(tab):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment