fix: 检查凭据时同步姓名和公司

This commit is contained in:
Nixevol
2026-11-18 11:58:11 +08:00
parent 199960276a
commit 66b4d74ba8
4 changed files with 41 additions and 3 deletions
+17
View File
@@ -110,6 +110,16 @@ def _require(db: Session, employee_id: str) -> Account:
return 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: 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 import bind_url, is_proxy_failure, url_of, uses_pool
from euniv.proxy_pool import MAX_SWITCHES, retire_if_dead 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 client = None
result = "检查失败" result = "检查失败"
valid: bool | None = None valid: bool | None = None
profile: dict = {}
try: try:
for _ in range(MAX_SWITCHES + 1): for _ in range(MAX_SWITCHES + 1):
proxy = bind_url(account) if pooled else url_of(account) 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()) alive = bool(client.credential_alive())
result = "有效" if alive else "已失效" result = "有效" if alive else "已失效"
valid = alive valid = alive
profile = _checked_profile(client) if alive else {}
break break
except Exception as exc: except Exception as exc:
profile = {}
if pooled and proxy and is_proxy_failure(exc): if pooled and proxy and is_proxy_failure(exc):
retire_if_dead(employee_id, proxy, str(exc)[:200]) retire_if_dead(employee_id, proxy, str(exc)[:200])
client.close() client.close()
@@ -155,6 +168,10 @@ def check_employee(db: Session, employee_id: str) -> dict:
if account is None: if account is None:
raise HTTPException(status_code=404, detail="用户不存在") raise HTTPException(status_code=404, detail="用户不存在")
record_outcome(account, valid) record_outcome(account, valid)
if valid is True and profile:
from euniv.service import _remember_profile
_remember_profile(db, employee_id, profile)
db.commit() db.commit()
payload = {"employee_id": employee_id, "result": result} payload = {"employee_id": employee_id, "result": result}
if result == "检查失败": if result == "检查失败":
+14
View File
@@ -1424,6 +1424,7 @@ class EunivClient:
return response.text return response.text
def credential_alive(self) -> bool: def credential_alive(self) -> bool:
self._checked_profile = None
try: try:
response = self._request("POST", f"{YXT_API}/v1/customdiy/user/getuserinfobyid", content=b"") response = self._request("POST", f"{YXT_API}/v1/customdiy/user/getuserinfobyid", content=b"")
except SessionExpired: except SessionExpired:
@@ -1436,8 +1437,21 @@ class EunivClient:
raise RuntimeError("学习平台暂时不可用") from exc raise RuntimeError("学习平台暂时不可用") from exc
if not isinstance(payload, dict): if not isinstance(payload, dict):
raise RuntimeError("学习平台暂时不可用") raise RuntimeError("学习平台暂时不可用")
self._checked_profile = payload
return bool(payload.get("cnName")) 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]: def _pages(self, url: str, headers: dict, html: str, parse, identity: str) -> list[dict]:
items = parse(html) items = parse(html)
seen = {(item.get("name", ""), item.get(identity, "")) for item in items} seen = {(item.get("name", ""), item.get(identity, "")) for item in items}
+1 -1
View File
@@ -478,7 +478,7 @@ def _profile_text(value: object, limit: int) -> str:
def _remember_profile(db: Session, employee_id: str, payload: dict) -> None: def _remember_profile(db: Session, employee_id: str, payload: dict) -> None:
"""首页已经取到姓名和部门时,顺手写回用户列表。不记录职级。""" """首页或凭据检查已经取到姓名和部门时,顺手写回用户列表。不记录职级。"""
if not isinstance(payload, dict): if not isinstance(payload, dict):
return return
name = _profile_text(payload.get("name"), 64) name = _profile_text(payload.get("name"), 64)
+9 -2
View File
@@ -362,6 +362,9 @@ class _Alive:
def credential_alive(self) -> bool: def credential_alive(self) -> bool:
return True return True
def checked_profile(self) -> dict:
return {"name": "莫英达", "department": "广州瀚信广州处", "level": "Scientist"}
def month_hours(self) -> str: def month_hours(self) -> str:
return "660" 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["refresh_paused"] is False
assert saved["credential_valid"] is True assert saved["credential_valid"] is True
assert saved["month_hours"] == "" assert saved["month_hours"] == ""
assert saved["user_name"] == "" assert saved["user_name"] == "莫英达"
assert saved["company"] == "" assert saved["company"] == "广州瀚信广州处"
assert isinstance(saved["last_refresh_at"], str) assert isinstance(saved["last_refresh_at"], str)
calls.clear() calls.clear()
again = client.get("/api/admin/users") again = client.get("/api/admin/users")
@@ -563,9 +566,13 @@ def test_check_all_reports_each_saved_account(monkeypatch) -> None:
with SessionLocal() as db: with SessionLocal() as db:
failed = db.get(Account, "b-user") failed = db.get(Account, "b-user")
untouched = db.get(Account, "c-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 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 failed.last_refresh_at is not None
assert untouched is not None and untouched.last_refresh_at is 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: def test_credentials_and_delete_user(monkeypatch) -> None: