Commit 6001cdb3 by baiquan

refactor(login): 重构登录验证逻辑

- 优化 is_logged_in 函数,通过监听网络请求判断登录状态
- 修改登录流程,确保仅在登录成功后设置认证令牌
parent b669d184
......@@ -30,11 +30,18 @@ def click_button(tab, loc, retry_limit=5, delay=2):
def is_logged_in(tab):
"""检查是否已登录的多种方式"""
if tab.title == "首页":
logger.info("已处于登录状态")
return True
cookies = tab.cookies().as_dict()
return bool(cookies.get('PHPSESSID'))
check_result = False
for packet in tab.listen.steps(timeout=5):
if '/aff/check_login' in packet.url and packet.response.body:
login_res = packet.response.body
logger.info(f'获取确认登录数据:{login_res}')
if (login_res['deputy_has_login'] and login_res['subject_has_login'] and login_res['error_code'] == 0
and login_res.get('sec_subject_uid') and login_res.get('sec_user_id')):
check_result = True
if check_result:
if tab.title != "首页":
return False
return check_result
def set_auth_token(tab):
......@@ -143,7 +150,7 @@ async def page_login(browser_id, max_retries=3):
co = ChromiumOptions().set_local_port(port)
browser = Chromium(co)
tab = browser.latest_tab
tab.set.cookies.clear()
# tab.set.cookies.clear()
tab.set.load_mode.eager()
tab.listen.start(["account_login/v2/", "captcha/verify", "/aff/check_login", "/loginv1/callback"])
# 访问登录页
......@@ -187,7 +194,7 @@ async def page_login(browser_id, max_retries=3):
# 验证登录结果
for _ in range(20): # 最多等待30秒
logger.debug(f"等待登录结果...")
if is_logged_in(tab) and set_auth_token(tab):
if tab.title == "首页" and set_auth_token(tab):
cookies = tab.cookies().as_dict()
PHPSESSID = cookies.get('PHPSESSID', '')
return PHPSESSID
......
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