diff --git a/backend/euniv/study_control.py b/backend/euniv/study_control.py index 1ffd9df..2af19bf 100644 --- a/backend/euniv/study_control.py +++ b/backend/euniv/study_control.py @@ -129,7 +129,11 @@ def assemble(account, live: dict | None = None, now: datetime | None = None) -> payload["active"] = running payload["started"] = running cached_hours = str(info.get("month_hours") or "") - payload["month_hours"] = cached_hours or str(getattr(account, "month_hours", "") or "") + stored_hours = str(getattr(account, "month_hours", "") or "") + recorded_month = str(getattr(account, "hours_month", "") or "") + if recorded_month and recorded_month != month_key(now): + stored_hours = "" + payload["month_hours"] = cached_hours or stored_hours 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 b435539..86ca765 100644 --- a/backend/tests/test_autostudy.py +++ b/backend/tests/test_autostudy.py @@ -507,6 +507,9 @@ def test_assemble_prefers_live_month_hours() -> None: account.month_hours = "10" assert assemble(account, {"month_hours": "68"})["month_hours"] == "68" assert assemble(account, {})["month_hours"] == "10" + account.hours_month = "2026-10" + account.month_hours = "88" + assert assemble(account, {}, datetime(2026, 11, 2, tzinfo=CN_TZ))["month_hours"] == "" def test_schedule_disabled_skips_window_until_started() -> None: diff --git a/web/src/features/home/HomeView.vue b/web/src/features/home/HomeView.vue index c1938d5..55f9c2a 100644 --- a/web/src/features/home/HomeView.vue +++ b/web/src/features/home/HomeView.vue @@ -472,7 +472,7 @@ function applyStudyControl(payload: Record) { message: typeof payload.message === "string" ? payload.message : "", workers: asWorkers(payload.workers), }; - if (typeof payload.month_hours === "string" && summary.value) { + if (typeof payload.month_hours === "string" && payload.month_hours && summary.value) { summary.value = { ...summary.value, month_hours: payload.month_hours }; } } @@ -480,6 +480,10 @@ function applyStudyControl(payload: Record) { async function loadStudyControl() { try { const res = await fetch("/api/settings/study-control"); + if (res.status === 401) { + stopStudySocket(); + return; + } const data = await res.json().catch(() => ({})); if (res.ok) applyStudyControl(data); } catch { /* 静默 */ } @@ -555,14 +559,18 @@ function connectStudySocket() { return; } studySocket = ws; + let welcomed = false; ws.onmessage = (event) => { + if (!welcomed) { + welcomed = true; + studyBackoff = 1000; + } try { const data = JSON.parse(String(event.data)); if (data && typeof data === "object") applyStudyControl(data as Record); } catch { /* ignore */ } }; ws.onopen = () => { - studyBackoff = 1000; stopStudyFallback(); void loadStudyControl(); if (studyPing) window.clearInterval(studyPing); @@ -570,12 +578,17 @@ function connectStudySocket() { if (ws.readyState === WebSocket.OPEN) ws.send("ping"); }, 8000); }; - ws.onclose = () => { + ws.onclose = (event) => { if (studyPing) { window.clearInterval(studyPing); studyPing = 0; } if (studySocket === ws) studySocket = null; + if (event.code === 4401 || event.code === 4403) { + studyClosed = true; + stopStudyFallback(); + return; + } if (!studyClosed) { startStudyFallback(); scheduleStudyReconnect();