feat: 自动拼图失败三次后改为手动拖动

This commit is contained in:
2026-10-27 04:28:25 +08:00
parent 858d777588
commit afda5a883f
6 changed files with 446 additions and 62 deletions
+83 -14
View File
@@ -3,35 +3,98 @@
from __future__ import annotations
import json
import secrets
import time
from dataclasses import dataclass
from sqlalchemy.orm import Session
from euniv.client import EunivClient, SessionExpired
from euniv.client import EunivClient, JigsawRejected, ManualJigsaw, SessionExpired
from euniv.crypto import decrypt_secret, encrypt_secret
from euniv.models import Account
_PENDING_SECONDS = 180
_pending: dict[str, "PendingLogin"] = {}
@dataclass
class PendingLogin:
client: EunivClient
employee_id: str
password: str
created: float
def _dump(credential: dict) -> str:
return json.dumps(credential, ensure_ascii=False, sort_keys=True)
def login_and_store(db: Session, employee_id: str, password: str, client_factory=EunivClient) -> Account:
client = client_factory(employee_id, password)
try:
profile = client.login()
client.authorize(profile)
credential = client.export_credential()
finally:
client.close()
def _purge_pending() -> None:
now = time.time()
for attempt_id, pending in list(_pending.items()):
if now - pending.created > _PENDING_SECONDS:
pending.client.close()
_pending.pop(attempt_id, None)
def _drop_employee(employee_id: str) -> None:
for attempt_id, pending in list(_pending.items()):
if pending.employee_id == employee_id:
pending.client.close()
_pending.pop(attempt_id, None)
def _persist(db: Session, employee_id: str, password: str, client: EunivClient, profile: dict) -> None:
client.authorize(profile)
account = db.get(Account, employee_id)
if account is None:
account = Account(employee_id=employee_id, password_cipher="", credential_cipher="")
db.add(account)
account.password_cipher = encrypt_secret(password)
account.credential_cipher = encrypt_secret(_dump(credential))
account.credential_cipher = encrypt_secret(_dump(client.export_credential()))
db.commit()
db.refresh(account)
return account
def login_and_store(db: Session, employee_id: str, password: str, client_factory=EunivClient) -> dict:
_purge_pending()
_drop_employee(employee_id)
client = client_factory(employee_id, password)
held = False
try:
try:
profile = client.login()
except ManualJigsaw as exc:
attempt_id = secrets.token_urlsafe(18)
_pending[attempt_id] = PendingLogin(client, employee_id, password, time.time())
held = True
return {"attempt_id": attempt_id, "jigsaw": exc.puzzle}
_persist(db, employee_id, password, client, profile)
return {"employee_id": employee_id}
finally:
if not held:
client.close()
def submit_jigsaw(db: Session, attempt_id: str, offset: int) -> dict:
_purge_pending()
pending = _pending.get(attempt_id)
if pending is None:
raise RuntimeError("请重新登录")
try:
profile = pending.client.submit_jigsaw(offset)
except JigsawRejected:
pending.created = time.time()
return {"attempt_id": attempt_id, "jigsaw": pending.client.next_jigsaw()}
except Exception:
_pending.pop(attempt_id, None)
pending.client.close()
raise
_pending.pop(attempt_id, None)
try:
_persist(db, pending.employee_id, pending.password, pending.client, profile)
return {"employee_id": pending.employee_id}
finally:
pending.client.close()
def fetch_home(db: Session, employee_id: str, client_factory=EunivClient) -> dict:
@@ -45,11 +108,17 @@ def fetch_home(db: Session, employee_id: str, client_factory=EunivClient) -> dic
if credential.get("yxt_token"):
client.restore(credential)
else:
client.authorize(client.login())
try:
client.authorize(client.login())
except ManualJigsaw:
raise RuntimeError("登录需要重新验证,请重新登录") from None
try:
data = client.home_snapshot()
except SessionExpired:
client.authorize(client.login())
try:
client.authorize(client.login())
except ManualJigsaw:
raise RuntimeError("登录需要重新验证,请重新登录") from None
data = client.home_snapshot()
updated = client.export_credential()
if _dump(updated) != _dump(credential):