diff --git a/backend/admin/settings.py b/backend/admin/settings.py index 3713cf8..ac244a2 100644 --- a/backend/admin/settings.py +++ b/backend/admin/settings.py @@ -244,6 +244,7 @@ def save_settings(db: Session, payload: dict) -> SystemConfig: row = load_config(db) previous_interval = int(row.refresh_interval_minutes) previous_username = row.admin_username + previous_source = hours_source_of(row) row.refresh_interval_minutes = interval row.show_manual_jigsaw = bool(payload.get("show_manual_jigsaw")) row.manual_preference = bool(payload.get("manual_preference")) @@ -269,4 +270,8 @@ def save_settings(db: Session, payload: dict) -> SystemConfig: from euniv.service import notify_refresh_interval_changed notify_refresh_interval_changed() + if source != previous_source: + from euniv.service import notify_hours_source_changed + + notify_hours_source_changed() return row diff --git a/backend/euniv/autostudy.py b/backend/euniv/autostudy.py index 54a1165..4156dbf 100644 --- a/backend/euniv/autostudy.py +++ b/backend/euniv/autostudy.py @@ -695,7 +695,10 @@ class HoursWatcher: self.user_stop.set() return True try: - if self.source == "history": + from admin.settings import hours_source_of + + source = hours_source_of() + if source == "history": current = int(await self.session.month_history_minutes()) text = str(current) else: diff --git a/backend/euniv/client.py b/backend/euniv/client.py index 266f1f8..0391df7 100644 --- a/backend/euniv/client.py +++ b/backend/euniv/client.py @@ -608,10 +608,18 @@ def _exam_result(block: str, status: str) -> str: return "" -def _exam_detail_path(value: str) -> bool: +def exam_path_ok(value: str) -> bool: return bool(re.fullmatch(r"/exam/exampreview\.htm\?[A-Za-z0-9_=&%.\-]+", value)) and "xamArrangeID=" in value +def training_path_ok(value: str) -> bool: + return bool(re.fullmatch(r"/plan/[0-9a-fA-F]{32}\.html", value)) + + +def _exam_detail_path(value: str) -> bool: + return exam_path_ok(value) + + def _field(page: str, element_id: str) -> str: match = re.search(rf'id="{re.escape(element_id)}"[^>]*>([\s\S]*?)', page) return _plain(match.group(1)) if match else "" @@ -1155,6 +1163,23 @@ class EunivClient: def home_trainings(self) -> list[dict]: return _by_closeness(self.online_trainings(), "sort_days") + def hour_fields(self) -> dict: + """只取四个学时数字,不读个人资料。""" + periods = self._period_minutes() + score_resp = self._request("POST", f"{YXT_API}/v1/customapiforweb/getscoreorpointorhour", json={}) + score_payload = score_resp.json() + score = score_payload.get("data") or {} + if score_payload.get("code") not in (None, "0", 0): + raise RuntimeError(score_payload.get("message") or "学时学分查询失败") + today_resp = self._request("GET", f"{STUDY_API}/v1/studyapi/studyhour/todayscorehours") + today = today_resp.json() + return { + "year_hours": periods["year"], + "month_hours": periods["month"], + "today_hours": today.get("studyHours"), + "total_hours": score.get("actualStudyHour") or "", + } + def month_hours(self) -> str: return self._period_minutes().get("month", "") @@ -1331,7 +1356,7 @@ class EunivClient: return parse_exam_detail(self._site_page(path)) def training_detail(self, path: str) -> dict: - if not re.fullmatch(r"/plan/[0-9a-fA-F]{32}\.html", path): + if not training_path_ok(path): raise RuntimeError("培训明细无法打开") return parse_training_detail(self._site_page(path)) diff --git a/backend/euniv/service.py b/backend/euniv/service.py index ceb0c01..02f5fca 100644 --- a/backend/euniv/service.py +++ b/backend/euniv/service.py @@ -378,10 +378,18 @@ def fetch_home_trainings(db: Session, employee_id: str, client_factory=EunivClie def fetch_exam_detail(db: Session, employee_id: str, path: str, client_factory=EunivClient) -> dict: + from euniv.client import exam_path_ok + + if not exam_path_ok(path): + raise RuntimeError("考试明细无法打开") return _live(db, employee_id, client_factory, lambda client: client.exam_detail(path)) def fetch_training_detail(db: Session, employee_id: str, path: str, client_factory=EunivClient) -> dict: + from euniv.client import training_path_ok + + if not training_path_ok(path): + raise RuntimeError("培训明细无法打开") return _live(db, employee_id, client_factory, lambda client: client.training_detail(path)) @@ -420,7 +428,8 @@ def _api_hour_fields(client) -> dict: if hours_source_of() == "history": return {} - summary = client.home_summary() + reader = getattr(client, "hour_fields", None) + summary = reader() if reader is not None else client.home_summary() return { "year_hours": summary.get("year_hours"), "month_hours": summary.get("month_hours"), @@ -636,16 +645,22 @@ class _RefreshGate: self._reason = "wake" self._event.set() + def refresh_now(self) -> None: + with self._lock: + if self._reason != "stop": + self._reason = "now" + self._event.set() + def wait(self, seconds: float) -> str: self._event.wait(timeout=max(0.0, seconds)) with self._lock: reason = self._reason if reason == "stop": return "stop" - if reason == "wake": + if reason in {"wake", "now"}: self._reason = "" self._event.clear() - return "wake" + return reason return "timeout" @@ -664,6 +679,14 @@ def notify_refresh_interval_changed() -> None: _gate.wake() +def notify_hours_source_changed() -> None: + """学时依据变了,立刻再刷新一轮已保存的学时。""" + if _gate is not None: + _gate.refresh_now() + return + threading.Thread(target=refresh_stored_credentials, name="euniv-hours-source", daemon=True).start() + + def start_credential_refresh() -> _RefreshGate: global _gate gate = _RefreshGate()