Files
NJUPT-Suan-API/njupt_api/zhengfang/lib.py
MangoFanFanw 16bd8e9f9a jwxt()
1. 将 jwxt() 移动到 njupt_api 下,实现根据设置选择教务系统登录方式。
2. 将 api_router.py 和 mcp_router.py 中的对 ZhengFang() 的调用全部替换为对 jwxt() 的调用。
2026-04-21 17:42:59 +08:00

41 lines
1.0 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 contextlib import asynccontextmanager
from typing import AsyncGenerator
from ..baselib import config
from .exc import LoginError
from .sso import SSO
from .zhengfang import ZhengFang
@asynccontextmanager
async def jwxt(username: str, password: str) -> AsyncGenerator[ZhengFang, None]:
"""
根据设置,选择 SSO 登录教务系统或直接登录教务系统。
Args:
username: 用户名str
password: 密码str
Yields:
zf: ZhengFang
Raises:
LoginError: 登录失败,包含下层返回的提示信息。
"""
try:
if config.get("schedule", "jwxt_login_method", "sso"):
sso = SSO()
await sso.start()
await sso.login(username, password)
zf = await ZhengFang.init_from_sso(sso)
else:
zf = ZhengFang()
await zf.start()
await zf.login(username, password)
except LoginError as e:
raise e
else:
yield zf
await zf.close()
return