From ad0b18ecfc9f77eb16636906c6f07a628b1df1bf Mon Sep 17 00:00:00 2001 From: Nixevol Date: Tue, 17 Nov 2026 12:07:45 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BB=BB=E5=8A=A1=E5=BC=80=E5=A7=8B?= =?UTF-8?q?=E5=90=8E=E6=98=BE=E7=A4=BA=E5=B7=B2=E5=BC=80=E5=90=AF=E5=92=8C?= =?UTF-8?q?=E5=AD=A6=E6=97=B6=E5=88=B7=E6=96=B0=E5=80=92=E8=AE=A1=E6=97=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/euniv/autostudy.py | 3 +++ backend/euniv/study_control.py | 7 +++++++ backend/tests/test_autostudy.py | 13 +++++++++++++ web/src/features/home/HomeView.vue | 24 ++++++++++++++++++++---- 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/backend/euniv/autostudy.py b/backend/euniv/autostudy.py index 35fffbf..0827667 100644 --- a/backend/euniv/autostudy.py +++ b/backend/euniv/autostudy.py @@ -131,6 +131,8 @@ def _set_user_status(employee_id: str, **kw: object) -> None: users = _status.setdefault("users", {}) info = users.setdefault(employee_id, {}) info.update(kw) + if kw.get("state") in ("已停止", "已学完", "学习失败", "会话失效", "已达标"): + info.pop("hours_refresh_at", None) info["month"] = str(kw["month"]) if "month" in kw else month_key() _status["updated_at"] = datetime.now(timezone.utc).isoformat() _notify(employee_id) @@ -762,6 +764,7 @@ class HoursWatcher: async def ticker(self, global_stop: asyncio.Event) -> None: while not global_stop.is_set() and not self.user_stop.is_set(): + _set_user_status(self.employee_id, hours_refresh_at=time.time() + self.interval) if await wait_stop(self.interval, global_stop, self.user_stop): return await self.refresh() diff --git a/backend/euniv/study_control.py b/backend/euniv/study_control.py index 6037b66..0f61482 100644 --- a/backend/euniv/study_control.py +++ b/backend/euniv/study_control.py @@ -2,8 +2,10 @@ from __future__ import annotations +import math import re import threading +import time from datetime import datetime from euniv.study_window import month_key, window_open @@ -148,6 +150,11 @@ def assemble(account, live: dict | None = None, now: datetime | None = None) -> if recorded_month and recorded_month != month_key(now): stored_hours = "" payload["month_hours"] = cached_hours or stored_hours + refresh_at = info.get("hours_refresh_at") + left = 0 + if running and isinstance(refresh_at, (int, float)) and not isinstance(refresh_at, bool): + left = max(0, int(math.ceil(float(refresh_at) - time.time()))) + payload["hours_refresh_in"] = left try: payload["monthly_target"] = int(getattr(account, "monthly_target", 0) or 0) except (TypeError, ValueError): diff --git a/backend/tests/test_autostudy.py b/backend/tests/test_autostudy.py index 03be600..8ea2935 100644 --- a/backend/tests/test_autostudy.py +++ b/backend/tests/test_autostudy.py @@ -469,6 +469,19 @@ def test_manual_start_expires_when_the_month_changes() -> None: assert study_eligible(account, datetime(2026, 12, 1, tzinfo=CN_TZ)) is False +def test_assemble_reports_hours_refresh_countdown(monkeypatch) -> None: + import time + + monkeypatch.setattr("euniv.autostudy.work_running", lambda _eid: True) + account = _Start(1, 0, 0, 0) + account.employee_id = "u-hours" + payload = assemble(account, {"hours_refresh_at": time.time() + 40}) + assert 1 <= payload["hours_refresh_in"] <= 40 + monkeypatch.setattr("euniv.autostudy.work_running", lambda _eid: False) + stopped = assemble(account, {"hours_refresh_at": time.time() + 40}) + assert stopped["hours_refresh_in"] == 0 + + def test_hours_met_clears_started_without_pause() -> None: account = _Start(1, 0, 0, 0) start_now(account) diff --git a/web/src/features/home/HomeView.vue b/web/src/features/home/HomeView.vue index a58ae87..baf72f2 100644 --- a/web/src/features/home/HomeView.vue +++ b/web/src/features/home/HomeView.vue @@ -74,6 +74,8 @@ const studyControl = ref({ workers: [] as StudyWorker[], }); const studyBusy = ref(false); +const hoursRefreshIn = ref(0); +const hoursRefreshSeen = ref(0); const studyError = ref(""); const studyingWorkers = computed(() => ( @@ -104,8 +106,7 @@ const studyLabel = computed(() => { if (studyFailed.value) return message || state; if (state === "排队中") return "排队中"; if (state === "准备") return "正在连接学习平台"; - if (studyingWorkers.value.length) return studyControl.value.course || "学习中"; - if (state === "学习中") return "正在领取课程"; + if (studyingWorkers.value.length || state === "学习中") return "任务已开启"; if (studyControl.value.active || studyControl.value.started) return "正在启动学习任务"; if (state === "已停止" || studyControl.value.paused) return "任务已停止"; if (state === "已学完") return "课程已学完"; @@ -223,6 +224,14 @@ function workerCount(row: StudyWorker) { return "—"; } +const showHoursRefresh = computed(() => studyLabel.value === "任务已开启" && hoursRefreshIn.value > 0); + +function hoursRefreshLeft() { + void nowTick.value; + const elapsed = (Date.now() - hoursRefreshSeen.value) / 1000; + return Math.max(0, Math.ceil(hoursRefreshIn.value - elapsed)); +} + function heartbeatLeft(row: StudyWorker) { void nowTick.value; if (row.heartbeat_in > 0) { @@ -564,6 +573,12 @@ function applyStudyControl(payload: Record) { workers: asWorkers(payload.workers), }; const studying = payload.active === true || payload.started === true; + if (studying && typeof payload.hours_refresh_in === "number" && payload.hours_refresh_in > 0) { + hoursRefreshIn.value = payload.hours_refresh_in; + hoursRefreshSeen.value = Date.now(); + } else if (!studying) { + hoursRefreshIn.value = 0; + } if (studying && typeof payload.month_hours === "string" && payload.month_hours) { progressHours.value = payload.month_hours; if (summary.value) summary.value = { ...summary.value, month_hours: payload.month_hours }; @@ -741,8 +756,8 @@ function onStudyVisible() { if (document.visibilityState === "visible") studyWake(); } -watch(countdownOn, (on) => { - if (on) startStudyTick(); +watch([countdownOn, showHoursRefresh], ([heart, hours]) => { + if (heart || hours) startStudyTick(); else stopStudyTick(); }); @@ -926,6 +941,7 @@ onUnmounted(() => { }" > {{ studyLabel }} + 刷新学时 {{ hoursRefreshLeft() }}s