466 lines
16 KiB
Vue
466 lines
16 KiB
Vue
<script setup lang="ts">
|
||
import { computed, onMounted, ref, watch } from "vue";
|
||
import { NAlert, NButton, NIcon, NInput, NInputNumber, NSelect, NTooltip } from "naive-ui";
|
||
import { HelpCircleOutline, RefreshOutline } from "@vicons/ionicons5";
|
||
|
||
interface HomeSummary {
|
||
name: string;
|
||
avatar: string;
|
||
department: string;
|
||
level: string;
|
||
year_hours: string;
|
||
month_hours: string;
|
||
today_hours: string | number | null;
|
||
total_hours: string;
|
||
total_score: string;
|
||
points: string | number | null;
|
||
}
|
||
|
||
type SectionState = "loading" | "ready" | "error";
|
||
|
||
const props = defineProps<{ employeeId: string }>();
|
||
const summary = ref<HomeSummary | null>(null);
|
||
const summaryState = ref<SectionState>("loading");
|
||
const summaryError = ref("");
|
||
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;
|
||
|
||
interface StudyUserState {
|
||
state?: string;
|
||
course?: string;
|
||
}
|
||
|
||
const studyStatus = ref<{
|
||
running: boolean;
|
||
users?: Record<string, StudyUserState>;
|
||
message: string;
|
||
} | null>(null);
|
||
|
||
const myStudy = computed(() => {
|
||
const users = studyStatus.value?.users;
|
||
if (!users) return null;
|
||
return users[props.employeeId] ?? null;
|
||
});
|
||
|
||
function amount(value: string | number | null | undefined) {
|
||
if (value === null || value === undefined || value === "") return "—";
|
||
const numeric = Number(value);
|
||
if (!Number.isFinite(numeric)) return String(value);
|
||
if (Number.isInteger(numeric)) return String(numeric);
|
||
return String(value);
|
||
}
|
||
|
||
const facts = computed(() => {
|
||
const current = summary.value;
|
||
return [
|
||
{ label: "本年学时", value: current ? amount(current.year_hours) : "" },
|
||
{ label: "本月学时", value: current ? amount(current.month_hours) : "" },
|
||
{ label: "今日学时", value: current ? amount(current.today_hours) : "" },
|
||
{ label: "累计学时", value: current ? amount(current.total_hours) : "" },
|
||
{ label: "累计学分", value: current ? amount(current.total_score) : "" },
|
||
{ label: "可用积分", value: current ? amount(current.points) : "" },
|
||
];
|
||
});
|
||
|
||
function failureText(payload: { detail?: unknown }, fallback: string) {
|
||
return typeof payload.detail === "string" ? payload.detail : fallback;
|
||
}
|
||
|
||
async function loadSummary() {
|
||
if (busy) return;
|
||
busy = true;
|
||
const hadData = summary.value !== null;
|
||
if (!hadData) summaryState.value = "loading";
|
||
try {
|
||
const response = await fetch("/api/home/summary");
|
||
const payload = await response.json().catch(() => ({}));
|
||
if (!response.ok) {
|
||
if (!hadData) {
|
||
summaryError.value = failureText(payload, "个人学习信息没有获取到。");
|
||
summaryState.value = "error";
|
||
}
|
||
return;
|
||
}
|
||
summary.value = payload;
|
||
summaryError.value = "";
|
||
summaryState.value = "ready";
|
||
} catch {
|
||
if (!hadData) {
|
||
summaryError.value = "个人学习信息没有获取到。";
|
||
summaryState.value = "error";
|
||
}
|
||
} finally {
|
||
busy = false;
|
||
}
|
||
}
|
||
|
||
function refresh() {
|
||
if (refreshRunning) return;
|
||
refreshRunning = true;
|
||
refreshing.value = true;
|
||
void loadSummary().finally(() => {
|
||
refreshRunning = false;
|
||
refreshing.value = false;
|
||
});
|
||
}
|
||
|
||
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); });
|
||
|
||
const startLoaded = ref(false);
|
||
const startDay = ref<number | null>(1);
|
||
const startHour = ref<number | null>(0);
|
||
const startMinute = ref<number | null>(0);
|
||
const startSecond = ref<number | null>(0);
|
||
let startSaveTimer: ReturnType<typeof setTimeout> | undefined;
|
||
|
||
async function loadStart() {
|
||
try {
|
||
const res = await fetch("/api/settings/study-start");
|
||
if (res.ok) {
|
||
const data = await res.json();
|
||
startDay.value = typeof data.day === "number" ? data.day : 1;
|
||
startHour.value = typeof data.hour === "number" ? data.hour : 0;
|
||
startMinute.value = typeof data.minute === "number" ? data.minute : 0;
|
||
startSecond.value = typeof data.second === "number" ? data.second : 0;
|
||
startLoaded.value = true;
|
||
}
|
||
} catch { /* 静默 */ }
|
||
}
|
||
|
||
function saveStart() {
|
||
const day = startDay.value;
|
||
const hour = startHour.value;
|
||
const minute = startMinute.value;
|
||
const second = startSecond.value;
|
||
if (
|
||
day == null || hour == null || minute == null || second == null
|
||
|| !Number.isInteger(day) || !Number.isInteger(hour) || !Number.isInteger(minute) || !Number.isInteger(second)
|
||
) return;
|
||
clearTimeout(startSaveTimer);
|
||
startSaveTimer = setTimeout(async () => {
|
||
try {
|
||
await fetch("/api/settings/study-start", {
|
||
method: "PUT",
|
||
headers: { "Content-Type": "application/json" },
|
||
body: JSON.stringify({ day, hour, minute, second }),
|
||
});
|
||
} catch { /* 静默 */ }
|
||
}, 600);
|
||
}
|
||
|
||
watch([startDay, startHour, startMinute, startSecond], () => { if (startLoaded.value) saveStart(); });
|
||
|
||
const workersLoaded = ref(false);
|
||
const courseWorkers = ref<number | null>(1);
|
||
let workersSaveTimer: ReturnType<typeof setTimeout> | undefined;
|
||
|
||
async function loadWorkers() {
|
||
try {
|
||
const res = await fetch("/api/settings/course-workers");
|
||
if (res.ok) {
|
||
const data = await res.json();
|
||
courseWorkers.value = typeof data.workers === "number" ? data.workers : 1;
|
||
workersLoaded.value = true;
|
||
}
|
||
} catch { /* 静默 */ }
|
||
}
|
||
|
||
function saveWorkers(value: number | null) {
|
||
if (value == null || !Number.isInteger(value) || value < 1 || value > 8) return;
|
||
clearTimeout(workersSaveTimer);
|
||
workersSaveTimer = setTimeout(async () => {
|
||
try {
|
||
await fetch("/api/settings/course-workers", {
|
||
method: "PUT",
|
||
headers: { "Content-Type": "application/json" },
|
||
body: JSON.stringify({ workers: value }),
|
||
});
|
||
} catch { /* 静默 */ }
|
||
}, 600);
|
||
}
|
||
|
||
watch(courseWorkers, (v) => { if (workersLoaded.value) saveWorkers(v); });
|
||
|
||
const proxyLoaded = ref(false);
|
||
const proxySaving = ref(false);
|
||
const proxyError = ref("");
|
||
const proxyScheme = ref("off");
|
||
const proxyHost = ref("");
|
||
const proxyPort = ref<number | null>(null);
|
||
const proxyUser = ref("");
|
||
const proxyPassword = ref("");
|
||
const proxyHasPassword = ref(false);
|
||
const proxyOff = computed(() => proxyScheme.value === "off");
|
||
const proxyOptions = [
|
||
{ label: "关闭", value: "off" },
|
||
{ label: "HTTP", value: "http" },
|
||
{ label: "HTTPS", value: "https" },
|
||
{ label: "SOCKS4", value: "socks4" },
|
||
{ label: "SOCKS5", value: "socks5" },
|
||
];
|
||
|
||
async function loadProxy() {
|
||
try {
|
||
const res = await fetch("/api/settings/proxy");
|
||
const data = await res.json().catch(() => ({}));
|
||
if (!res.ok) return;
|
||
proxyScheme.value = typeof data.scheme === "string" && data.scheme ? data.scheme : "off";
|
||
proxyHost.value = typeof data.host === "string" ? data.host : "";
|
||
proxyPort.value = typeof data.port === "number" ? data.port : null;
|
||
proxyUser.value = typeof data.username === "string" ? data.username : "";
|
||
proxyPassword.value = "";
|
||
proxyHasPassword.value = data.has_password === true;
|
||
proxyLoaded.value = true;
|
||
} catch { /* 静默 */ }
|
||
}
|
||
|
||
function proxyPasswordPayload() {
|
||
if (proxyPassword.value !== "") return proxyPassword.value;
|
||
if (!proxyUser.value.trim()) return "";
|
||
return proxyHasPassword.value ? null : "";
|
||
}
|
||
|
||
async function saveProxy() {
|
||
if (proxySaving.value) return;
|
||
proxyError.value = "";
|
||
if (!proxyOff.value) {
|
||
if (!proxyHost.value.trim()) {
|
||
proxyError.value = "请填写代理地址";
|
||
return;
|
||
}
|
||
if (proxyPort.value == null || !Number.isInteger(proxyPort.value)) {
|
||
proxyError.value = "请填写代理端口";
|
||
return;
|
||
}
|
||
}
|
||
proxySaving.value = true;
|
||
try {
|
||
const res = await fetch("/api/settings/proxy", {
|
||
method: "PUT",
|
||
headers: { "Content-Type": "application/json" },
|
||
body: JSON.stringify(
|
||
proxyOff.value
|
||
? { scheme: "off" }
|
||
: {
|
||
scheme: proxyScheme.value,
|
||
host: proxyHost.value.trim(),
|
||
port: proxyPort.value,
|
||
username: proxyUser.value.trim(),
|
||
password: proxyPasswordPayload(),
|
||
},
|
||
),
|
||
});
|
||
const data = await res.json().catch(() => ({}));
|
||
if (!res.ok) {
|
||
proxyError.value = failureText(data, "没有保存。");
|
||
return;
|
||
}
|
||
proxyPassword.value = "";
|
||
proxyHasPassword.value = data.has_password === true;
|
||
if (proxyOff.value) {
|
||
proxyHost.value = "";
|
||
proxyPort.value = null;
|
||
proxyUser.value = "";
|
||
proxyHasPassword.value = false;
|
||
}
|
||
} catch {
|
||
proxyError.value = "没有保存。";
|
||
} finally {
|
||
proxySaving.value = false;
|
||
}
|
||
}
|
||
|
||
async function loadStudyStatus() {
|
||
try {
|
||
const res = await fetch("/api/autostudy/status");
|
||
if (res.ok) studyStatus.value = await res.json();
|
||
} catch { /* 静默 */ }
|
||
}
|
||
|
||
onMounted(() => { refresh(); loadTarget(); loadStart(); loadWorkers(); loadProxy(); loadStudyStatus(); });
|
||
</script>
|
||
|
||
<template>
|
||
<section class="page home-page">
|
||
<div class="home">
|
||
<div class="home-summary" :aria-busy="summaryState === 'loading'">
|
||
<div class="home-top">
|
||
<div class="home-top-main">
|
||
<n-alert v-if="summaryState === 'error'" class="summary-error" type="error">{{ summaryError }}</n-alert>
|
||
<div v-else class="profile">
|
||
<template v-if="summary">
|
||
<img v-if="summary.avatar" class="avatar" :src="summary.avatar" alt="" referrerpolicy="no-referrer" />
|
||
<div v-else class="brand-mark avatar-fallback" aria-hidden="true">{{ summary.name.slice(0, 1) || "E" }}</div>
|
||
<div>
|
||
<strong>{{ summary.name || props.employeeId }}</strong>
|
||
<span>{{ summary.department }}<template v-if="summary.level"> · {{ summary.level }}</template></span>
|
||
</div>
|
||
</template>
|
||
<template v-else>
|
||
<div class="bone bone-avatar"></div>
|
||
<div class="bone-copy">
|
||
<div class="bone bone-name"></div>
|
||
<div class="bone bone-meta"></div>
|
||
</div>
|
||
<div class="spin" role="status" aria-label="正在获取"></div>
|
||
</template>
|
||
</div>
|
||
</div>
|
||
<n-tooltip placement="bottom">
|
||
<template #trigger>
|
||
<n-button
|
||
class="home-refresh"
|
||
quaternary
|
||
circle
|
||
size="small"
|
||
aria-label="刷新数据"
|
||
:loading="refreshing"
|
||
@click="refresh"
|
||
>
|
||
<template #icon><n-icon><RefreshOutline /></n-icon></template>
|
||
</n-button>
|
||
</template>
|
||
刷新数据
|
||
</n-tooltip>
|
||
</div>
|
||
<dl class="facts">
|
||
<div v-for="item in facts" :key="item.label">
|
||
<dt>{{ item.label }}</dt>
|
||
<dd>
|
||
<span v-if="summaryState === 'loading'" class="bone bone-value"></span>
|
||
<template v-else>{{ item.value || "—" }}</template>
|
||
</dd>
|
||
</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="startLoaded" class="home-target home-start">
|
||
<label for="study-start-day">每月开始学习</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-input-number v-model:value="startHour" button-placement="both" :min="0" :max="23" :precision="0" size="small" />
|
||
<span class="target-unit">:</span>
|
||
<n-input-number v-model:value="startMinute" button-placement="both" :min="0" :max="59" :precision="0" size="small" />
|
||
<span class="target-unit">:</span>
|
||
<n-input-number v-model:value="startSecond" button-placement="both" :min="0" :max="59" :precision="0" size="small" />
|
||
</div>
|
||
</div>
|
||
<div v-if="workersLoaded" class="home-target">
|
||
<label class="field-title" for="course-workers">
|
||
<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>
|
||
</label>
|
||
<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>
|
||
</div>
|
||
<form v-if="proxyLoaded" class="home-proxy" @submit.prevent="saveProxy">
|
||
<n-alert v-if="proxyError" class="summary-error" type="error">{{ proxyError }}</n-alert>
|
||
<div class="form-row">
|
||
<label for="proxy-scheme">代理类型</label>
|
||
<n-select id="proxy-scheme" v-model:value="proxyScheme" size="small" :options="proxyOptions" />
|
||
</div>
|
||
<div class="form-row">
|
||
<label for="proxy-host">代理地址</label>
|
||
<n-input id="proxy-host" v-model:value="proxyHost" size="small" placeholder="例如 127.0.0.1" :disabled="proxyOff" />
|
||
</div>
|
||
<div class="form-row">
|
||
<label for="proxy-port">代理端口</label>
|
||
<n-input-number id="proxy-port" v-model:value="proxyPort" size="small" button-placement="both" :min="1" :max="65535" :precision="0" :disabled="proxyOff" />
|
||
</div>
|
||
<div class="form-row">
|
||
<label for="proxy-user">代理账号</label>
|
||
<n-input id="proxy-user" v-model:value="proxyUser" size="small" placeholder="可不填" autocomplete="off" :disabled="proxyOff" />
|
||
</div>
|
||
<div class="form-row">
|
||
<label for="proxy-password">代理密码</label>
|
||
<n-input
|
||
id="proxy-password"
|
||
v-model:value="proxyPassword"
|
||
size="small"
|
||
type="password"
|
||
show-password-on="click"
|
||
:placeholder="proxyHasPassword ? '已保存,留空则保持' : '可不填'"
|
||
autocomplete="new-password"
|
||
:disabled="proxyOff"
|
||
/>
|
||
</div>
|
||
<div class="home-proxy-actions">
|
||
<n-button type="primary" size="small" attr-type="submit" :loading="proxySaving">保存</n-button>
|
||
</div>
|
||
</form>
|
||
<div v-if="studyStatus" class="home-study-status">
|
||
<span class="study-indicator" :class="{ active: Boolean(myStudy) || studyStatus.running }"></span>
|
||
<span v-if="myStudy" class="study-text">
|
||
{{ myStudy.state === "已达标" ? "本月目标已达到" : `正在学习:${myStudy.course || myStudy.state || "准备中"}` }}
|
||
</span>
|
||
<span v-else-if="studyStatus.running" class="study-text muted">自动学习进行中</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>
|