feat: 自动学习功能,后台定时发送心跳积累学时

This commit is contained in:
2026-11-15 01:01:45 +08:00
parent 2acf310e0c
commit ffa563d97c
13 changed files with 959 additions and 16 deletions
+66 -3
View File
@@ -1,6 +1,6 @@
<script setup lang="ts">
import { computed, onMounted, ref } from "vue";
import { NAlert, NButton, NIcon, NTooltip } from "naive-ui";
import { computed, onMounted, ref, watch } from "vue";
import { NAlert, NButton, NIcon, NInputNumber, NTooltip } from "naive-ui";
import { RefreshOutline } from "@vicons/ionicons5";
interface HomeSummary {
@@ -26,6 +26,12 @@ const refreshing = ref(false);
let refreshRunning = false;
let busy = false;
const monthlyTarget = ref<number | null>(null);
const targetLoaded = ref(false);
let targetSaveTimer: ReturnType<typeof setTimeout> | undefined;
const studyStatus = ref<{ running: boolean; user: string; course: string; message: string } | null>(null);
function amount(value: string | number | null | undefined) {
if (value === null || value === undefined || value === "") return "—";
const numeric = Number(value);
@@ -88,7 +94,41 @@ function refresh() {
});
}
onMounted(refresh);
async function loadTarget() {
try {
const res = await fetch("/api/settings/monthly-target");
if (res.ok) {
const data = await res.json();
monthlyTarget.value = data.monthly_target ?? 600;
targetLoaded.value = true;
}
} catch { /* 静默 */ }
}
function saveTarget(value: number | null) {
if (value == null || !Number.isInteger(value) || value < 0 || value > 9999) return;
clearTimeout(targetSaveTimer);
targetSaveTimer = setTimeout(async () => {
try {
await fetch("/api/settings/monthly-target", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ monthly_target: value }),
});
} catch { /* 静默 */ }
}, 600);
}
watch(monthlyTarget, (v) => { if (targetLoaded.value) saveTarget(v); });
async function loadStudyStatus() {
try {
const res = await fetch("/api/autostudy/status");
if (res.ok) studyStatus.value = await res.json();
} catch { /* 静默 */ }
}
onMounted(() => { refresh(); loadTarget(); loadStudyStatus(); });
</script>
<template>
@@ -144,6 +184,29 @@ onMounted(refresh);
</div>
</dl>
</div>
<div v-if="targetLoaded" class="home-target">
<label for="monthly-target">每月自动学习目标</label>
<div class="target-control">
<n-input-number
id="monthly-target"
v-model:value="monthlyTarget"
button-placement="both"
:min="0"
:max="9999"
:precision="0"
size="small"
/>
<span class="target-unit">分钟</span>
</div>
</div>
<div v-if="studyStatus" class="home-study-status">
<span class="study-indicator" :class="{ active: studyStatus.running }"></span>
<span v-if="studyStatus.running" class="study-text">
正在学习:{{ studyStatus.course || "准备中" }}
</span>
<span v-else-if="studyStatus.message" class="study-text muted">{{ studyStatus.message }}</span>
<span v-else class="study-text muted">自动学习未运行</span>
</div>
</div>
</section>
</template>