feat: 学习进度轮询、学时依据与首页代理弹窗
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, ref, watch } from "vue";
|
||||
import { NAlert, NButton, NForm, NFormItem, NIcon, NInput, NInputNumber, NSelect, NTimePicker, NTooltip } from "naive-ui";
|
||||
import { HelpCircleOutline, PlayOutline, RefreshOutline, StopOutline } from "@vicons/ionicons5";
|
||||
import { computed, onMounted, onUnmounted, ref, watch } from "vue";
|
||||
import { NAlert, NButton, NForm, NFormItem, NIcon, NInput, NInputNumber, NModal, NProgress, NSelect, NTimePicker, NTooltip } from "naive-ui";
|
||||
import { GlobeOutline, PlayOutline, RefreshOutline, StopOutline, WarningOutline } from "@vicons/ionicons5";
|
||||
import { dayOptions, workerOptions } from "../../shared/studyFields";
|
||||
|
||||
interface HomeSummary {
|
||||
name: string;
|
||||
@@ -16,6 +17,15 @@ interface HomeSummary {
|
||||
points: string | number | null;
|
||||
}
|
||||
|
||||
interface StudyWorker {
|
||||
id: number;
|
||||
course: string;
|
||||
progress: number;
|
||||
interval: number;
|
||||
heartbeat_at: number;
|
||||
ok: boolean;
|
||||
}
|
||||
|
||||
type SectionState = "loading" | "ready" | "error";
|
||||
|
||||
const props = defineProps<{ employeeId: string }>();
|
||||
@@ -25,22 +35,13 @@ const summaryError = ref("");
|
||||
const refreshing = ref(false);
|
||||
let refreshRunning = false;
|
||||
let busy = false;
|
||||
let studyPoll = 0;
|
||||
const nowTick = ref(Date.now());
|
||||
|
||||
const monthlyTarget = ref<number | null>(null);
|
||||
const targetLoaded = ref(false);
|
||||
let targetSaveTimer: ReturnType<typeof setTimeout> | undefined;
|
||||
|
||||
interface StudyUserState {
|
||||
state?: string;
|
||||
course?: string;
|
||||
}
|
||||
|
||||
const studyStatus = ref<{
|
||||
running: boolean;
|
||||
users?: Record<string, StudyUserState>;
|
||||
message: string;
|
||||
} | null>(null);
|
||||
|
||||
const studyControl = ref({
|
||||
started: false,
|
||||
paused: false,
|
||||
@@ -49,28 +50,22 @@ const studyControl = ref({
|
||||
active: false,
|
||||
state: "",
|
||||
course: "",
|
||||
message: "",
|
||||
workers: [] as StudyWorker[],
|
||||
});
|
||||
const studyBusy = ref(false);
|
||||
const studyError = ref("");
|
||||
|
||||
const myStudy = computed(() => {
|
||||
const users = studyStatus.value?.users;
|
||||
if (!users) return null;
|
||||
return users[props.employeeId] ?? null;
|
||||
});
|
||||
|
||||
const studyLabel = computed(() => {
|
||||
const live = myStudy.value;
|
||||
const state = live?.state || studyControl.value.state;
|
||||
const course = live?.course || studyControl.value.course;
|
||||
const state = studyControl.value.state;
|
||||
const course = studyControl.value.course;
|
||||
if (state === "已达标") return "本月目标已达到";
|
||||
if (state === "已停止" || studyControl.value.paused) return "任务已停止";
|
||||
if (studyControl.value.active || live) {
|
||||
if (studyControl.value.active) {
|
||||
return `正在学习:${course || state || "准备中"}`;
|
||||
}
|
||||
if (studyControl.value.started) return "任务已开始,等待执行";
|
||||
if (studyStatus.value?.message) return studyStatus.value.message;
|
||||
if (studyStatus.value?.running) return "自动学习进行中";
|
||||
if (studyControl.value.message) return studyControl.value.message;
|
||||
return "未到开始时间";
|
||||
});
|
||||
|
||||
@@ -102,6 +97,41 @@ function pad(value: number) {
|
||||
return String(value).padStart(2, "0");
|
||||
}
|
||||
|
||||
function asWorkers(value: unknown): StudyWorker[] {
|
||||
if (!Array.isArray(value)) return [];
|
||||
const rows: StudyWorker[] = [];
|
||||
for (const item of value) {
|
||||
if (!item || typeof item !== "object") continue;
|
||||
const row = item as Record<string, unknown>;
|
||||
rows.push({
|
||||
id: typeof row.id === "number" ? row.id : rows.length + 1,
|
||||
course: typeof row.course === "string" ? row.course : "",
|
||||
progress: typeof row.progress === "number" ? row.progress : 0,
|
||||
interval: typeof row.interval === "number" ? row.interval : 0,
|
||||
heartbeat_at: typeof row.heartbeat_at === "number" ? row.heartbeat_at : 0,
|
||||
ok: row.ok !== false,
|
||||
});
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
||||
function workerPercent(row: StudyWorker) {
|
||||
return Math.min(100, Math.max(0, Math.round(row.progress)));
|
||||
}
|
||||
|
||||
function workerTitle(row: StudyWorker) {
|
||||
if (row.course) return row.course;
|
||||
if (workerPercent(row) >= 100) return "空闲";
|
||||
return "准备";
|
||||
}
|
||||
|
||||
function heartbeatLeft(row: StudyWorker) {
|
||||
void nowTick.value;
|
||||
if (!row.heartbeat_at) return "—";
|
||||
const left = Math.max(0, Math.ceil(row.heartbeat_at - Date.now() / 1000));
|
||||
return `${left}s`;
|
||||
}
|
||||
|
||||
async function loadSummary() {
|
||||
if (busy) return;
|
||||
busy = true;
|
||||
@@ -134,7 +164,7 @@ function refresh() {
|
||||
if (refreshRunning) return;
|
||||
refreshRunning = true;
|
||||
refreshing.value = true;
|
||||
void Promise.all([loadSummary(), loadStudyStatus(), loadStudyControl()]).finally(() => {
|
||||
void Promise.all([loadSummary(), loadStudyControl()]).finally(() => {
|
||||
refreshRunning = false;
|
||||
refreshing.value = false;
|
||||
});
|
||||
@@ -256,6 +286,7 @@ function saveWorkers(value: number | null) {
|
||||
|
||||
watch(courseWorkers, (v) => { if (workersLoaded.value) saveWorkers(v); });
|
||||
|
||||
const proxyOpen = ref(false);
|
||||
const proxyLoaded = ref(false);
|
||||
const proxySaving = ref(false);
|
||||
const proxyError = ref("");
|
||||
@@ -266,6 +297,31 @@ const proxyUser = ref("");
|
||||
const proxyPassword = ref("");
|
||||
const proxyHasPassword = ref(false);
|
||||
const proxyManual = computed(() => proxyScheme.value !== "off" && proxyScheme.value !== "pool");
|
||||
const savedProxy = {
|
||||
scheme: "off",
|
||||
host: "",
|
||||
port: null as number | null,
|
||||
username: "",
|
||||
hasPassword: false,
|
||||
};
|
||||
|
||||
function rememberProxy() {
|
||||
savedProxy.scheme = proxyScheme.value;
|
||||
savedProxy.host = proxyHost.value;
|
||||
savedProxy.port = proxyPort.value;
|
||||
savedProxy.username = proxyUser.value;
|
||||
savedProxy.hasPassword = proxyHasPassword.value;
|
||||
}
|
||||
|
||||
function restoreProxy() {
|
||||
proxyScheme.value = savedProxy.scheme;
|
||||
proxyHost.value = savedProxy.host;
|
||||
proxyPort.value = savedProxy.port;
|
||||
proxyUser.value = savedProxy.username;
|
||||
proxyPassword.value = "";
|
||||
proxyHasPassword.value = savedProxy.hasPassword;
|
||||
proxyError.value = "";
|
||||
}
|
||||
const proxyOptions = [
|
||||
{ label: "关闭", value: "off" },
|
||||
{ label: "自动", value: "pool" },
|
||||
@@ -287,6 +343,7 @@ async function loadProxy() {
|
||||
proxyPassword.value = "";
|
||||
proxyHasPassword.value = data.has_password === true;
|
||||
proxyLoaded.value = true;
|
||||
rememberProxy();
|
||||
} catch { /* 静默 */ }
|
||||
}
|
||||
|
||||
@@ -339,6 +396,8 @@ async function saveProxy() {
|
||||
proxyUser.value = "";
|
||||
proxyHasPassword.value = false;
|
||||
}
|
||||
rememberProxy();
|
||||
proxyOpen.value = false;
|
||||
} catch {
|
||||
proxyError.value = "没有保存。";
|
||||
} finally {
|
||||
@@ -346,13 +405,6 @@ async function saveProxy() {
|
||||
}
|
||||
}
|
||||
|
||||
async function loadStudyStatus() {
|
||||
try {
|
||||
const res = await fetch("/api/autostudy/status");
|
||||
if (res.ok) studyStatus.value = await res.json();
|
||||
} catch { /* 静默 */ }
|
||||
}
|
||||
|
||||
function applyStudyControl(payload: Record<string, unknown>) {
|
||||
studyControl.value = {
|
||||
started: payload.started === true,
|
||||
@@ -362,6 +414,8 @@ function applyStudyControl(payload: Record<string, unknown>) {
|
||||
active: payload.active === true,
|
||||
state: typeof payload.state === "string" ? payload.state : "",
|
||||
course: typeof payload.course === "string" ? payload.course : "",
|
||||
message: typeof payload.message === "string" ? payload.message : "",
|
||||
workers: asWorkers(payload.workers),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -373,6 +427,33 @@ async function loadStudyControl() {
|
||||
} catch { /* 静默 */ }
|
||||
}
|
||||
|
||||
function startStudyPoll() {
|
||||
if (studyPoll) return;
|
||||
studyPoll = window.setInterval(() => {
|
||||
nowTick.value = Date.now();
|
||||
void loadStudyControl();
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
function stopStudyPoll() {
|
||||
if (studyPoll) {
|
||||
window.clearInterval(studyPoll);
|
||||
studyPoll = 0;
|
||||
}
|
||||
}
|
||||
|
||||
watch(proxyOpen, (open, wasOpen) => {
|
||||
if (wasOpen && !open) restoreProxy();
|
||||
});
|
||||
|
||||
watch(
|
||||
() => studyControl.value.started,
|
||||
(started) => {
|
||||
if (started) startStudyPoll();
|
||||
else stopStudyPoll();
|
||||
},
|
||||
);
|
||||
|
||||
async function toggleStudy() {
|
||||
if (studyBusy.value) return;
|
||||
studyBusy.value = true;
|
||||
@@ -390,7 +471,6 @@ async function toggleStudy() {
|
||||
return;
|
||||
}
|
||||
applyStudyControl(data);
|
||||
await loadStudyStatus();
|
||||
} catch {
|
||||
studyError.value = action === "start" ? "没有开始。" : "没有停止。";
|
||||
} finally {
|
||||
@@ -405,6 +485,13 @@ onMounted(() => {
|
||||
loadWorkers();
|
||||
loadProxy();
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
stopStudyPoll();
|
||||
clearTimeout(targetSaveTimer);
|
||||
clearTimeout(startSaveTimer);
|
||||
clearTimeout(workersSaveTimer);
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -461,7 +548,7 @@ onMounted(() => {
|
||||
</dl>
|
||||
</section>
|
||||
|
||||
<section class="panel-card">
|
||||
<section class="panel-card study-card">
|
||||
<header class="panel-card-head">
|
||||
<h3>自动学习</h3>
|
||||
<n-button
|
||||
@@ -477,7 +564,7 @@ onMounted(() => {
|
||||
</n-button>
|
||||
</header>
|
||||
<n-alert v-if="studyError" class="summary-error" type="error">{{ studyError }}</n-alert>
|
||||
<n-form class="compact-nform" label-placement="left" label-align="left" :label-width="88" :show-feedback="false" size="small">
|
||||
<n-form class="compact-nform" label-placement="left" label-align="left" :label-width="96" :show-feedback="false" size="small">
|
||||
<n-form-item v-if="targetLoaded" label="每月目标">
|
||||
<div class="target-control">
|
||||
<n-input-number
|
||||
@@ -492,66 +579,68 @@ onMounted(() => {
|
||||
<span class="target-unit">分钟</span>
|
||||
</div>
|
||||
</n-form-item>
|
||||
<n-form-item v-if="startLoaded" label="每月开始">
|
||||
<n-form-item v-if="startLoaded" label="定时启动">
|
||||
<div class="target-control start-control">
|
||||
<n-input-number id="study-start-day" v-model:value="startDay" button-placement="both" :min="1" :max="31" :precision="0" size="small" />
|
||||
<span class="target-unit">日</span>
|
||||
<n-select id="study-start-day" v-model:value="startDay" size="small" :options="dayOptions" class="day-select" />
|
||||
<n-tooltip v-if="startDay === 1" placement="top">
|
||||
<template #trigger>
|
||||
<n-icon class="day-warn"><WarningOutline /></n-icon>
|
||||
</template>
|
||||
不推荐使用
|
||||
</n-tooltip>
|
||||
<n-time-picker v-model:formatted-value="startTime" value-format="HH:mm:ss" size="small" :actions="null" />
|
||||
</div>
|
||||
</n-form-item>
|
||||
<n-form-item v-if="workersLoaded">
|
||||
<template #label>
|
||||
<span class="field-title">
|
||||
<span>同时课程</span>
|
||||
<n-tooltip placement="top">
|
||||
<template #trigger>
|
||||
<button type="button" class="hint-icon" aria-label="说明">
|
||||
<n-icon><HelpCircleOutline /></n-icon>
|
||||
</button>
|
||||
</template>
|
||||
速度较慢,但是稳定且不容易触发风控,最大可设置8
|
||||
</n-tooltip>
|
||||
</span>
|
||||
</template>
|
||||
<div class="target-control">
|
||||
<n-input-number
|
||||
id="course-workers"
|
||||
v-model:value="courseWorkers"
|
||||
button-placement="both"
|
||||
:min="1"
|
||||
:max="8"
|
||||
:precision="0"
|
||||
size="small"
|
||||
/>
|
||||
<span class="target-unit">门</span>
|
||||
<span v-if="courseWorkers === 1" class="recommend-mark">推荐</span>
|
||||
</div>
|
||||
<n-form-item v-if="workersLoaded" label="线程数量">
|
||||
<n-select id="course-workers" v-model:value="courseWorkers" size="small" :options="workerOptions" />
|
||||
</n-form-item>
|
||||
</n-form>
|
||||
<div class="home-study-status">
|
||||
<span class="study-indicator" :class="{ active: studyControl.started || studyControl.active }"></span>
|
||||
<span class="study-text" :class="{ muted: !studyControl.active }">{{ studyLabel }}</span>
|
||||
</div>
|
||||
<ul v-if="studyControl.started && studyControl.workers.length" class="study-workers">
|
||||
<li v-for="row in studyControl.workers" :key="row.id" class="study-worker">
|
||||
<div class="study-worker-main">
|
||||
<span class="study-worker-title">{{ workerTitle(row) }}</span>
|
||||
<n-progress type="line" :percentage="workerPercent(row)" :show-indicator="false" :height="8" />
|
||||
</div>
|
||||
<div class="study-worker-meta">
|
||||
<span class="study-worker-pct">{{ workerPercent(row) }}%</span>
|
||||
<span class="study-heartbeat">{{ heartbeatLeft(row) }}</span>
|
||||
<span class="study-dot" :class="row.ok ? 'up' : 'down'"></span>
|
||||
</div>
|
||||
</li>
|
||||
</ul>
|
||||
<div class="study-card-foot">
|
||||
<n-button v-if="proxyLoaded" size="small" quaternary @click="proxyOpen = true">
|
||||
<template #icon><n-icon><GlobeOutline /></n-icon></template>
|
||||
代理
|
||||
</n-button>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<form v-if="proxyLoaded" class="panel-card" @submit.prevent="saveProxy">
|
||||
<header class="panel-card-head">
|
||||
<h3>代理</h3>
|
||||
<n-button type="primary" size="small" attr-type="submit" :loading="proxySaving">保存</n-button>
|
||||
</header>
|
||||
<n-alert v-if="proxyError" class="summary-error" type="error">{{ proxyError }}</n-alert>
|
||||
<n-form class="compact-nform" label-placement="left" label-align="left" :label-width="88" :show-feedback="false" size="small">
|
||||
<n-form-item label="类型">
|
||||
<n-select v-model:value="proxyScheme" size="small" :options="proxyOptions" />
|
||||
</n-form-item>
|
||||
</div>
|
||||
<n-modal
|
||||
:show="proxyOpen"
|
||||
preset="card"
|
||||
title="代理"
|
||||
:style="{ width: 'min(420px, calc(100vw - 32px))' }"
|
||||
@update:show="(show) => (proxyOpen = show)"
|
||||
>
|
||||
<n-alert v-if="proxyError" class="summary-error" type="error">{{ proxyError }}</n-alert>
|
||||
<n-form class="compact-nform" label-placement="left" label-align="left" :label-width="72" :show-feedback="false" size="small">
|
||||
<n-form-item label="类型">
|
||||
<n-select v-model:value="proxyScheme" size="small" :options="proxyOptions" />
|
||||
</n-form-item>
|
||||
<template v-if="proxyManual">
|
||||
<n-form-item label="地址">
|
||||
<n-input v-model:value="proxyHost" size="small" placeholder="例如 127.0.0.1" :disabled="!proxyManual" />
|
||||
<n-input v-model:value="proxyHost" size="small" placeholder="例如 127.0.0.1" />
|
||||
</n-form-item>
|
||||
<n-form-item label="端口">
|
||||
<n-input-number v-model:value="proxyPort" size="small" button-placement="both" :min="1" :max="65535" :precision="0" :disabled="!proxyManual" placeholder="请输入" />
|
||||
<n-input-number v-model:value="proxyPort" size="small" button-placement="both" :min="1" :max="65535" :precision="0" placeholder="请输入" />
|
||||
</n-form-item>
|
||||
<n-form-item label="账号">
|
||||
<n-input v-model:value="proxyUser" size="small" placeholder="可不填" autocomplete="off" :disabled="!proxyManual" />
|
||||
<n-input v-model:value="proxyUser" size="small" placeholder="可不填" autocomplete="off" />
|
||||
</n-form-item>
|
||||
<n-form-item label="密码">
|
||||
<n-input
|
||||
@@ -561,11 +650,16 @@ onMounted(() => {
|
||||
show-password-on="click"
|
||||
:placeholder="proxyHasPassword ? '已保存,留空则保持' : '可不填'"
|
||||
autocomplete="new-password"
|
||||
:disabled="!proxyManual"
|
||||
/>
|
||||
</n-form-item>
|
||||
</n-form>
|
||||
</form>
|
||||
</div>
|
||||
</template>
|
||||
</n-form>
|
||||
<template #footer>
|
||||
<div class="modal-actions">
|
||||
<n-button @click="proxyOpen = false">取消</n-button>
|
||||
<n-button type="primary" :loading="proxySaving" @click="saveProxy">保存</n-button>
|
||||
</div>
|
||||
</template>
|
||||
</n-modal>
|
||||
</section>
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user