fix: 凭据有效改为当场取到个人信息和本月学时

This commit is contained in:
Nixevol
2026-11-21 15:01:54 +08:00
parent a6306e8339
commit a5539405ca
3 changed files with 85 additions and 26 deletions
+34 -23
View File
@@ -116,23 +116,34 @@ def _require(db: Session, employee_id: str) -> Account:
return account
def _checked_hours(client) -> tuple[str, str]:
def describe_credential(client) -> dict:
"""用当前凭据现查个人信息和本月学时。两项都取到才算有效,取不到的字段留空。"""
from admin.settings import source_for_employee
from euniv.service import _month_hours
try:
hours = _month_hours(client)
except Exception:
hours = ""
info_ok = bool(client.credential_alive())
profile = _checked_profile(client) if info_ok else {}
if not str(profile.get("name") or "").strip():
info_ok = False
profile = {}
hours = ""
official = ""
if hours and source_for_employee(str(getattr(client, "login_name", "") or "")) == "history":
reader = getattr(client, "month_hours", None)
if callable(reader):
try:
official = str(reader() or "").strip()
except Exception:
official = ""
return hours, official
if info_ok:
try:
hours = str(_month_hours(client) or "").strip()
except Exception:
hours = ""
source = source_for_employee(str(getattr(client, "login_name", "") or ""))
if source == "history":
reader = getattr(client, "month_hours", None)
if callable(reader):
try:
official = str(reader() or "").strip()
except Exception:
official = ""
else:
official = hours
return {"valid": info_ok and bool(hours), "profile": profile, "hours": hours, "official": official}
def _checked_profile(client) -> dict:
@@ -168,12 +179,12 @@ def check_employee(db: Session, employee_id: str) -> dict:
client = _open_client(employee_id, password, proxy)
try:
client.restore(credential)
alive = bool(client.credential_alive())
result = "有效" if alive else "已失效"
valid = alive
profile = _checked_profile(client) if alive else {}
if alive:
hours, official_hours = _checked_hours(client)
found = describe_credential(client)
result = "有效" if found["valid"] else "已失效"
valid = bool(found["valid"])
profile = found["profile"]
hours = found["hours"]
official_hours = found["official"]
break
except Exception as exc:
profile = {}
@@ -199,16 +210,16 @@ 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:
if profile:
from euniv.service import _remember_profile
_remember_profile(db, employee_id, profile)
if valid is True and hours:
if hours:
from euniv.study_control import stamp_hours
stamp_hours(account, hours)
if official_hours:
account.official_month_hours = official_hours[:32]
if official_hours:
account.official_month_hours = official_hours[:32]
db.commit()
payload = {"employee_id": employee_id, "result": result}
if result == "检查失败":