Files
NJUPT-Suan-API/njupt_api/zhengfang/sso.py
MangoFanFanw b284c3c260 Python 后端提交
Python 后端(FastAPI + FastMCP + ...)的初始版本号设定为 0.1.0,这是 uv 在 pypriject.toml
里给我自动设置的,我觉得有道理。
2026-04-21 13:38:46 +08:00

39 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from njupt_api.baselib import PlayContextManager, logger
class SSO(PlayContextManager):
def __init__(self) -> None:
super().__init__()
async def login(self, username: str, password: str) -> bool:
"""使用用户名和密码实现登录南邮统一身份验证。
Parameters:
username: 用户名,学号,一般为一位大写字母+八位数字
password: 密码
Returns:
bool表明判登录是否成功。
"""
await self.page.goto("http://i.njupt.edu.cn/")
await self.page.fill('input[name="username"]', username)
await self.page.fill('input[type="password"]', password)
await self.page.click('button[type="button"]')
await self.page.wait_for_load_state("networkidle")
if "user-login" in self.page.url:
logger.error(f"{username} | 登录失败,请检查学号和密码是否正确。")
return False
logger.info(f"{username} | 登录南邮统一身份认证成功。")
self.isLogin = True
return True
async def goto_zf(self) -> None:
sub_frame = self.page.frame_locator('iframe[name="iframe0"]')
async with self.context.expect_event("page") as new_page_event:
await sub_frame.locator('a[title="教务系统"]').click()
self.page = await new_page_event.value
return