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
+61 -9
View File
@@ -1,6 +1,6 @@
<script setup lang="ts">
import { computed, onMounted, ref } from "vue";
import { NAlert, NButton, NInput, NInputNumber, NSwitch } from "naive-ui";
import { NAlert, NButton, NInput, NInputNumber, NRadioButton, NRadioGroup, NSwitch } from "naive-ui";
const emit = defineEmits<{ saved: [] }>();
@@ -16,6 +16,10 @@ const attempts = ref<number | null>(null);
const username = ref("");
const password = ref("");
const confirmPassword = ref("");
const hbMode = ref("manual");
const hbVideo = ref<number | null>(30);
const hbDocument = ref<number | null>(120);
const hbScorm = ref<number | null>(120);
const preferenceLabel = computed(() =>
showManualJigsaw.value ? "默认勾选手动滑动验证码" : "先尝试自动滑动验证码",
@@ -58,6 +62,10 @@ onMounted(async () => {
showManualJigsaw.value = body.show_manual_jigsaw === true;
manualPreference.value = body.manual_preference === true;
username.value = body.admin_username;
hbMode.value = typeof body.heartbeat_interval_mode === "string" ? body.heartbeat_interval_mode : "manual";
hbVideo.value = readNumber(body.heartbeat_video_seconds) ?? 30;
hbDocument.value = readNumber(body.heartbeat_document_seconds) ?? 120;
hbScorm.value = readNumber(body.heartbeat_scorm_seconds) ?? 120;
loaded.value = true;
} catch {
errorText.value = "系统配置没有获取到。";
@@ -82,24 +90,35 @@ async function save() {
errorText.value = "自动滑动次数须为 1 到 10 的整数";
return;
}
if (hbMode.value === "manual") {
if (invalidInteger(hbVideo.value, 10, 300)) {
errorText.value = "视频心跳间隔须为 10 到 300 的整数";
return;
}
if (invalidInteger(hbDocument.value, 30, 600)) {
errorText.value = "文档心跳间隔须为 30 到 600 的整数";
return;
}
if (invalidInteger(hbScorm.value, 30, 600)) {
errorText.value = "Scorm 心跳间隔须为 30 到 600 的整数";
return;
}
}
const adminUsername = username.value.trim();
if (!adminUsername) {
errorText.value = "请填写管理员用户名";
return;
}
const body: {
refresh_interval_minutes: number;
show_manual_jigsaw: boolean;
manual_preference: boolean;
auto_jigsaw_attempts: number;
admin_username: string;
admin_password?: string;
} = {
const body: Record<string, unknown> = {
refresh_interval_minutes: refreshInterval.value as number,
show_manual_jigsaw: showManualJigsaw.value,
manual_preference: manualPreference.value,
auto_jigsaw_attempts: attempts.value as number,
admin_username: adminUsername,
heartbeat_interval_mode: hbMode.value,
heartbeat_video_seconds: hbVideo.value as number,
heartbeat_document_seconds: hbDocument.value as number,
heartbeat_scorm_seconds: hbScorm.value as number,
};
if (password.value !== "" || confirmPassword.value !== "") {
if (password.value !== confirmPassword.value) {
@@ -166,6 +185,39 @@ async function save() {
<span class="setting-unit">次</span>
</div>
</div>
<h3 class="form-section">自动学习心跳间隔</h3>
<div class="form-row">
<label>间隔来源</label>
<n-radio-group v-model:value="hbMode" size="small">
<n-radio-button value="auto">自动获取</n-radio-button>
<n-radio-button value="manual">手动设定</n-radio-button>
</n-radio-group>
</div>
<template v-if="hbMode === 'manual'">
<div class="form-row">
<label for="hb-video">视频</label>
<div class="setting-control">
<n-input-number id="hb-video" v-model:value="hbVideo" button-placement="both" :min="10" :max="300" :precision="0" />
<span class="setting-unit">秒</span>
</div>
</div>
<div class="form-row">
<label for="hb-document">文档</label>
<div class="setting-control">
<n-input-number id="hb-document" v-model:value="hbDocument" button-placement="both" :min="30" :max="600" :precision="0" />
<span class="setting-unit">秒</span>
</div>
</div>
<div class="form-row">
<label for="hb-scorm">Scorm</label>
<div class="setting-control">
<n-input-number id="hb-scorm" v-model:value="hbScorm" button-placement="both" :min="30" :max="600" :precision="0" />
<span class="setting-unit">秒</span>
</div>
</div>
</template>
<p v-else class="form-hint">自动获取:每次自动学习时从课程播放页在线读取间隔</p>
<h3 class="form-section">管理员</h3>
<div class="form-row">
<label for="admin-username">管理员用户名</label>
<n-input id="admin-username" v-model:value="username" autocomplete="username" />
+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>
+73
View File
@@ -405,6 +405,64 @@ select {
color: var(--muted);
}
.home-target {
display: flex;
align-items: center;
gap: 12px;
padding-top: 16px;
border-top: 1px solid var(--line);
}
.home-target label {
flex: none;
font-size: 13px;
color: var(--muted);
}
.target-control {
display: flex;
align-items: center;
gap: 6px;
}
.target-control .n-input-number {
width: 120px;
}
.target-unit {
color: var(--muted);
font-size: 13px;
}
.home-study-status {
display: flex;
align-items: center;
gap: 8px;
padding-top: 12px;
border-top: 1px solid var(--line);
}
.study-indicator {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--muted);
flex: none;
}
.study-indicator.active {
background: #3fb950;
box-shadow: 0 0 6px rgba(63, 185, 80, 0.5);
}
.study-text {
font-size: 13px;
}
.study-text.muted {
color: var(--muted);
}
.profile {
display: flex;
align-items: center;
@@ -959,6 +1017,21 @@ select {
color: var(--muted);
}
.form-section {
margin: 20px 0 8px;
font-size: 13px;
font-weight: 600;
color: var(--muted);
letter-spacing: 0.04em;
}
.form-hint {
margin: 0;
padding: 2px 0 0 160px;
font-size: 12px;
color: var(--muted);
}
.admin-table {
width: 100%;
min-width: 820px;