feat: 首页拆分独立加载、骨架动画和手动刷新
This commit is contained in:
@@ -17,6 +17,17 @@ from euniv.models import Account
|
||||
|
||||
_PENDING_SECONDS = 180
|
||||
_pending: dict[str, "PendingLogin"] = {}
|
||||
_credential_guard = threading.Lock()
|
||||
_credential_locks: dict[str, threading.Lock] = {}
|
||||
|
||||
|
||||
def _credential_lock(employee_id: str) -> threading.Lock:
|
||||
with _credential_guard:
|
||||
lock = _credential_locks.get(employee_id)
|
||||
if lock is None:
|
||||
lock = threading.Lock()
|
||||
_credential_locks[employee_id] = lock
|
||||
return lock
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -69,6 +80,7 @@ def login_and_store(
|
||||
account = db.get(Account, employee_id)
|
||||
if account and account.password_cipher and account.credential_cipher:
|
||||
if decrypt_secret(account.password_cipher) == password and _credential_still_valid(account, client_factory):
|
||||
_request_course_sync(employee_id)
|
||||
return {"employee_id": employee_id}
|
||||
_drop_employee(employee_id)
|
||||
client = client_factory(employee_id, password)
|
||||
@@ -82,6 +94,7 @@ def login_and_store(
|
||||
held = True
|
||||
return {"attempt_id": attempt_id, "jigsaw": exc.puzzle}
|
||||
_persist(db, employee_id, password, client, profile)
|
||||
_request_course_sync(employee_id)
|
||||
return {"employee_id": employee_id}
|
||||
finally:
|
||||
if not held:
|
||||
@@ -116,6 +129,7 @@ def submit_jigsaw(db: Session, attempt_id: str, offset: int) -> dict:
|
||||
_pending.pop(attempt_id, None)
|
||||
try:
|
||||
_persist(db, pending.employee_id, pending.password, pending.client, profile)
|
||||
_request_course_sync(pending.employee_id)
|
||||
return {"employee_id": pending.employee_id}
|
||||
finally:
|
||||
pending.client.close()
|
||||
@@ -126,6 +140,18 @@ def fetch_home(db: Session, employee_id: str, client_factory=EunivClient) -> dic
|
||||
return _live(db, employee_id, client_factory, lambda client: client.home_snapshot())
|
||||
|
||||
|
||||
def fetch_home_summary(db: Session, employee_id: str, client_factory=EunivClient) -> dict:
|
||||
return _live(db, employee_id, client_factory, lambda client: client.home_summary())
|
||||
|
||||
|
||||
def fetch_home_exams(db: Session, employee_id: str, client_factory=EunivClient) -> dict:
|
||||
return {"exams": _live(db, employee_id, client_factory, lambda client: client.home_exams())}
|
||||
|
||||
|
||||
def fetch_home_trainings(db: Session, employee_id: str, client_factory=EunivClient) -> dict:
|
||||
return {"trainings": _live(db, employee_id, client_factory, lambda client: client.home_trainings())}
|
||||
|
||||
|
||||
def fetch_exam_detail(db: Session, employee_id: str, path: str, client_factory=EunivClient) -> dict:
|
||||
return _live(db, employee_id, client_factory, lambda client: client.exam_detail(path))
|
||||
|
||||
@@ -164,9 +190,13 @@ def _live(db: Session, employee_id: str, client_factory, read):
|
||||
raise RuntimeError("登录需要重新验证,请重新登录") from None
|
||||
data = read(client)
|
||||
updated = client.export_credential()
|
||||
if _dump(updated) != _dump(credential):
|
||||
account.credential_cipher = encrypt_secret(_dump(updated))
|
||||
db.commit()
|
||||
with _credential_lock(employee_id):
|
||||
db.refresh(account)
|
||||
current_raw = account.credential_cipher
|
||||
current = json.loads(decrypt_secret(current_raw)) if current_raw else {}
|
||||
if _dump(updated) != _dump(current) and _dump(current) == _dump(credential):
|
||||
account.credential_cipher = encrypt_secret(_dump(updated))
|
||||
db.commit()
|
||||
return data
|
||||
finally:
|
||||
client.close()
|
||||
@@ -202,6 +232,8 @@ def _refresh_one(employee_id: str, attempts: int, client_factory) -> None:
|
||||
return
|
||||
client = None
|
||||
valid = False
|
||||
checked_alive = False
|
||||
hours = ""
|
||||
try:
|
||||
password = decrypt_secret(account.password_cipher)
|
||||
previous = json.loads(decrypt_secret(account.credential_cipher))
|
||||
@@ -212,10 +244,13 @@ def _refresh_one(employee_id: str, attempts: int, client_factory) -> None:
|
||||
if _dump(updated) != _dump(previous):
|
||||
account.credential_cipher = encrypt_secret(_dump(updated))
|
||||
valid = True
|
||||
checked_alive = True
|
||||
else:
|
||||
profile = client.login(manual=False, auto_attempts=attempts)
|
||||
_persist(db, employee_id, password, client, profile)
|
||||
valid = True
|
||||
if valid:
|
||||
hours = _month_hours(client)
|
||||
except Exception:
|
||||
db.rollback()
|
||||
valid = False
|
||||
@@ -229,7 +264,11 @@ def _refresh_one(employee_id: str, attempts: int, client_factory) -> None:
|
||||
except Exception:
|
||||
pass
|
||||
record_outcome(account, valid)
|
||||
if hours:
|
||||
account.month_hours = hours
|
||||
db.commit()
|
||||
if checked_alive:
|
||||
_request_course_sync(employee_id)
|
||||
|
||||
|
||||
class _RefreshGate:
|
||||
@@ -301,5 +340,22 @@ def start_credential_refresh() -> _RefreshGate:
|
||||
return gate
|
||||
|
||||
|
||||
def _month_hours(client) -> str:
|
||||
reader = getattr(client, "month_hours", None)
|
||||
if reader is None:
|
||||
return ""
|
||||
try:
|
||||
value = reader()
|
||||
except Exception:
|
||||
return ""
|
||||
return value if isinstance(value, str) else ""
|
||||
|
||||
|
||||
def _request_course_sync(employee_id: str) -> None:
|
||||
from euniv.courses import request_course_sync
|
||||
|
||||
request_course_sync(employee_id)
|
||||
|
||||
|
||||
def stored_password(account: Account) -> str:
|
||||
return decrypt_secret(account.password_cipher)
|
||||
|
||||
Reference in New Issue
Block a user