diff --git a/backend/admin/users.py b/backend/admin/users.py index f087824..d2eaf28 100644 --- a/backend/admin/users.py +++ b/backend/admin/users.py @@ -110,6 +110,16 @@ def _require(db: Session, employee_id: str) -> Account: return account +def _checked_profile(client) -> dict: + reader = getattr(client, "checked_profile", None) + if not callable(reader): + return {} + payload = reader() + if not isinstance(payload, dict): + return {} + return payload + + def check_employee(db: Session, employee_id: str) -> dict: from euniv.proxy import bind_url, is_proxy_failure, url_of, uses_pool from euniv.proxy_pool import MAX_SWITCHES, retire_if_dead @@ -124,6 +134,7 @@ def check_employee(db: Session, employee_id: str) -> dict: client = None result = "检查失败" valid: bool | None = None + profile: dict = {} try: for _ in range(MAX_SWITCHES + 1): proxy = bind_url(account) if pooled else url_of(account) @@ -133,8 +144,10 @@ def check_employee(db: Session, employee_id: str) -> dict: alive = bool(client.credential_alive()) result = "有效" if alive else "已失效" valid = alive + profile = _checked_profile(client) if alive else {} break except Exception as exc: + profile = {} if pooled and proxy and is_proxy_failure(exc): retire_if_dead(employee_id, proxy, str(exc)[:200]) client.close() @@ -155,6 +168,10 @@ def check_employee(db: Session, employee_id: str) -> dict: if account is None: raise HTTPException(status_code=404, detail="用户不存在") record_outcome(account, valid) + if valid is True and profile: + from euniv.service import _remember_profile + + _remember_profile(db, employee_id, profile) db.commit() payload = {"employee_id": employee_id, "result": result} if result == "检查失败": diff --git a/backend/euniv/client.py b/backend/euniv/client.py index 4597503..753ebf2 100644 --- a/backend/euniv/client.py +++ b/backend/euniv/client.py @@ -1424,6 +1424,7 @@ class EunivClient: return response.text def credential_alive(self) -> bool: + self._checked_profile = None try: response = self._request("POST", f"{YXT_API}/v1/customdiy/user/getuserinfobyid", content=b"") except SessionExpired: @@ -1436,8 +1437,21 @@ class EunivClient: raise RuntimeError("学习平台暂时不可用") from exc if not isinstance(payload, dict): raise RuntimeError("学习平台暂时不可用") + self._checked_profile = payload return bool(payload.get("cnName")) + def checked_profile(self) -> dict: + """最近一次凭据检查里的姓名和部门。职级不取出。""" + payload = getattr(self, "_checked_profile", None) + if not isinstance(payload, dict): + return {} + name = payload.get("cnName") + company = payload.get("departmentName") + return { + "name": name if isinstance(name, str) else "", + "department": company if isinstance(company, str) else "", + } + def _pages(self, url: str, headers: dict, html: str, parse, identity: str) -> list[dict]: items = parse(html) seen = {(item.get("name", ""), item.get(identity, "")) for item in items} diff --git a/backend/euniv/service.py b/backend/euniv/service.py index 76b664a..caed425 100644 --- a/backend/euniv/service.py +++ b/backend/euniv/service.py @@ -478,7 +478,7 @@ def _profile_text(value: object, limit: int) -> str: def _remember_profile(db: Session, employee_id: str, payload: dict) -> None: - """首页已经取到姓名和部门时,顺手写回用户列表。不记录职级。""" + """首页或凭据检查已经取到姓名和部门时,顺手写回用户列表。不记录职级。""" if not isinstance(payload, dict): return name = _profile_text(payload.get("name"), 64) diff --git a/backend/tests/test_admin.py b/backend/tests/test_admin.py index 57cc87f..9a110a9 100644 --- a/backend/tests/test_admin.py +++ b/backend/tests/test_admin.py @@ -362,6 +362,9 @@ class _Alive: def credential_alive(self) -> bool: return True + def checked_profile(self) -> dict: + return {"name": "莫英达", "department": "广州瀚信广州处", "level": "Scientist"} + def month_hours(self) -> str: return "660" @@ -526,8 +529,8 @@ def test_check_skips_empty_credential_and_updates_saved_one(monkeypatch) -> None assert saved["refresh_paused"] is False assert saved["credential_valid"] is True assert saved["month_hours"] == "" - assert saved["user_name"] == "" - assert saved["company"] == "" + assert saved["user_name"] == "莫英达" + assert saved["company"] == "广州瀚信广州处" assert isinstance(saved["last_refresh_at"], str) calls.clear() again = client.get("/api/admin/users") @@ -563,9 +566,13 @@ def test_check_all_reports_each_saved_account(monkeypatch) -> None: with SessionLocal() as db: failed = db.get(Account, "b-user") untouched = db.get(Account, "c-user") + checked = db.get(Account, "a-user") + assert checked is not None and checked.user_name == "莫英达" and checked.company == "广州瀚信广州处" assert failed is not None and failed.credential_valid is None + assert failed.user_name == "" and failed.company == "" assert failed.last_refresh_at is not None assert untouched is not None and untouched.last_refresh_at is None + assert untouched.user_name == "" and untouched.company == "" def test_credentials_and_delete_user(monkeypatch) -> None: