feat: 首页拆分独立加载、骨架动画和手动刷新

This commit is contained in:
2026-11-15 00:48:26 +08:00
parent 9a6f134f56
commit fa6811dab7
9 changed files with 657 additions and 282 deletions
+91 -252
View File
@@ -1,72 +1,9 @@
<script setup lang="ts">
import { computed, onMounted, onUnmounted, ref } from "vue";
import { NAlert, NModal } from "naive-ui";
import { computed, onMounted, ref } from "vue";
import { NAlert, NButton, NIcon, NTooltip } from "naive-ui";
import { RefreshOutline } from "@vicons/ionicons5";
interface ExamItem {
name: string;
status: string;
result: string;
start: string;
detail: string;
}
interface TrainingItem {
name: string;
status: string;
progress: number;
assigned: string;
credits: string;
detail: string;
}
interface ExamAttempt {
submitted: string;
score: string;
result: string;
}
interface ExamDetail {
total_score: string;
pass_score: string;
questions: string;
window: string;
times: string;
limit: string;
examinee: string;
view_policy: string;
notice: string;
attempts: ExamAttempt[];
}
interface TrainingTask {
name: string;
progress: string;
score: string;
hours: string;
}
interface TrainingPhase {
name: string;
hours: string;
credits: string;
tasks: TrainingTask[];
}
interface TrainingDetail {
owner: string;
earned_hours: string;
actual_hours: string;
reward_hours: string;
earned_credits: string;
actual_credits: string;
reward_credits: string;
rank: string;
progress: string;
deadline: string;
phases: TrainingPhase[];
}
interface HomeProfile {
interface HomeSummary {
name: string;
avatar: string;
department: string;
@@ -77,24 +14,17 @@ interface HomeProfile {
total_hours: string;
total_score: string;
points: string | number | null;
exams: ExamItem[];
trainings: TrainingItem[];
}
type SectionState = "loading" | "ready" | "error";
const props = defineProps<{ employeeId: string }>();
const profile = ref<HomeProfile | null>(null);
const errorText = ref("");
const loading = ref(false);
const dialogOpen = ref(false);
const dialogTitle = ref("");
const dialogKind = ref<"exam" | "training">("exam");
const examDetail = ref<ExamDetail | null>(null);
const trainingDetail = ref<TrainingDetail | null>(null);
const detailError = ref("");
const detailLoading = ref(false);
let timer = 0;
let requestId = 0;
let detailRequest = 0;
const summary = ref<HomeSummary | null>(null);
const summaryState = ref<SectionState>("loading");
const summaryError = ref("");
const refreshing = ref(false);
let refreshRunning = false;
let busy = false;
function amount(value: string | number | null | undefined) {
if (value === null || value === undefined || value === "") return "—";
@@ -105,206 +35,115 @@ function amount(value: string | number | null | undefined) {
}
const facts = computed(() => {
const current = profile.value;
if (!current) return [];
const current = summary.value;
return [
{ label: "本年学时", value: amount(current.year_hours) },
{ label: "本月学时", value: amount(current.month_hours) },
{ label: "今日学时", value: amount(current.today_hours) },
{ label: "累计学时", value: amount(current.total_hours) },
{ label: "累计学分", value: amount(current.total_score) },
{ label: "可用积分", value: amount(current.points) },
{ 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 trainingMeta(item: TrainingItem) {
const parts = [item.status, `${item.progress}%`];
if (item.credits) parts.push(`${item.credits} 学分`);
return parts.join(" · ");
function failureText(payload: { detail?: unknown }, fallback: string) {
return typeof payload.detail === "string" ? payload.detail : fallback;
}
async function openDetail(kind: "exam" | "training", item: { name: string; detail: string }) {
const current = ++detailRequest;
dialogKind.value = kind;
dialogTitle.value = item.name;
examDetail.value = null;
trainingDetail.value = null;
detailError.value = "";
dialogOpen.value = true;
if (!item.detail) {
detailError.value = "明细没有获取到。";
detailLoading.value = false;
return;
}
detailLoading.value = true;
async function loadSummary() {
if (busy) return;
busy = true;
const hadData = summary.value !== null;
if (!hadData) summaryState.value = "loading";
try {
const route = kind === "exam" ? "/api/exam" : "/api/training";
const response = await fetch(`${route}?${new URLSearchParams({ path: item.detail })}`);
const response = await fetch("/api/home/summary");
const payload = await response.json().catch(() => ({}));
if (current !== detailRequest) return;
if (!response.ok) {
detailError.value = typeof payload.detail === "string" ? payload.detail : "明细没有获取到。";
if (!hadData) {
summaryError.value = failureText(payload, "个人学习信息没有获取到。");
summaryState.value = "error";
}
return;
}
if (kind === "exam") examDetail.value = payload;
else trainingDetail.value = payload;
summary.value = payload;
summaryError.value = "";
summaryState.value = "ready";
} catch {
if (current === detailRequest) detailError.value = "明细没有获取到。";
} finally {
if (current === detailRequest) detailLoading.value = false;
}
}
async function load() {
const current = ++requestId;
loading.value = profile.value === null;
try {
const response = await fetch("/api/home");
const payload = await response.json().catch(() => ({}));
if (current !== requestId) return;
if (!response.ok) {
errorText.value = typeof payload.detail === "string" ? payload.detail : "首页信息没有获取到。";
return;
if (!hadData) {
summaryError.value = "个人学习信息没有获取到。";
summaryState.value = "error";
}
errorText.value = "";
profile.value = payload;
} catch {
if (current === requestId) errorText.value = "首页信息没有获取到。";
} finally {
if (current === requestId) loading.value = false;
busy = false;
}
}
onMounted(() => {
void load();
timer = window.setInterval(() => void load(), 30000);
});
function refresh() {
if (refreshRunning) return;
refreshRunning = true;
refreshing.value = true;
void loadSummary().finally(() => {
refreshRunning = false;
refreshing.value = false;
});
}
onUnmounted(() => window.clearInterval(timer));
onMounted(refresh);
</script>
<template>
<section class="page home-page">
<header class="page-head">
<h2>首页</h2>
</header>
<n-alert v-if="errorText && !profile" type="error">{{ errorText }}</n-alert>
<p v-else-if="loading" class="home-status">正在获取</p>
<div v-if="profile" class="home">
<n-alert v-if="errorText" type="error">{{ errorText }}</n-alert>
<div class="home-summary">
<div class="profile">
<img v-if="profile.avatar" class="avatar" :src="profile.avatar" alt="" referrerpolicy="no-referrer" />
<div v-else class="brand-mark avatar-fallback" aria-hidden="true">{{ profile.name.slice(0, 1) || "E" }}</div>
<div>
<strong>{{ profile.name || props.employeeId }}</strong>
<span>{{ profile.department }}<template v-if="profile.level"> · {{ profile.level }}</template></span>
<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>{{ item.value }}</dd>
<dd>
<span v-if="summaryState === 'loading'" class="bone bone-value"></span>
<template v-else>{{ item.value || "—" }}</template>
</dd>
</div>
</dl>
</div>
<div class="boards">
<section class="board" aria-labelledby="exam-heading">
<h3 id="exam-heading">我的考试</h3>
<div class="rail" tabindex="0">
<p v-if="!(profile.exams || []).length" class="rail-empty">没有考试</p>
<ol v-else>
<li v-for="item in profile.exams" :key="`${item.start}-${item.name}`" class="event">
<span class="mark" aria-hidden="true"></span>
<button type="button" class="event-hit" @click="openDetail('exam', item)">
<time :datetime="item.start">{{ item.start }}</time>
<strong>{{ item.name }}</strong>
<p class="meta">
<span>{{ item.status }}</span>
<span v-if="item.result" :class="item.result === '已通过' ? 'ok' : 'late'">{{ item.result }}</span>
</p>
</button>
</li>
</ol>
</div>
</section>
<section class="board" aria-labelledby="training-heading">
<h3 id="training-heading">线上培训</h3>
<div class="rail" tabindex="0">
<p v-if="!(profile.trainings || []).length" class="rail-empty">没有线上培训</p>
<ol v-else>
<li v-for="item in profile.trainings" :key="`${item.assigned}-${item.name}`" class="event">
<span class="mark" aria-hidden="true"></span>
<button type="button" class="event-hit" @click="openDetail('training', item)">
<time>{{ item.assigned }}</time>
<strong>{{ item.name }}</strong>
<p class="meta"><span>{{ trainingMeta(item) }}</span></p>
</button>
</li>
</ol>
</div>
</section>
</div>
</div>
<n-modal
v-model:show="dialogOpen"
preset="card"
:title="dialogTitle"
:style="{ width: 'min(760px, calc(100vw - 32px))' }"
:content-style="{ maxHeight: '70vh', overflow: 'auto' }"
>
<p v-if="detailLoading" class="home-status">正在获取</p>
<n-alert v-else-if="detailError" type="error">{{ detailError }}</n-alert>
<div v-else-if="dialogKind === 'exam' && examDetail" class="detail">
<p v-if="examDetail.notice" class="detail-notice">{{ examDetail.notice }}</p>
<dl class="detail-facts">
<div><dt>试卷总分</dt><dd>{{ examDetail.total_score }}</dd></div>
<div><dt>合格分数</dt><dd>{{ examDetail.pass_score }}</dd></div>
<div><dt>试卷题数</dt><dd>{{ examDetail.questions }}</dd></div>
<div><dt>考试次数</dt><dd>{{ examDetail.times }}</dd></div>
<div><dt>开考时间</dt><dd>{{ examDetail.window }}</dd></div>
<div><dt>考试时限</dt><dd>{{ examDetail.limit }}</dd></div>
<div><dt>姓名</dt><dd>{{ examDetail.examinee }}</dd></div>
<div><dt>查看答卷</dt><dd>{{ examDetail.view_policy }}</dd></div>
</dl>
<h4>考试历史</h4>
<p v-if="!examDetail.attempts.length" class="home-status">没有考试记录</p>
<table v-else class="detail-table">
<thead>
<tr><th>交卷时间</th><th>考试成绩</th><th>考试结果</th></tr>
</thead>
<tbody>
<tr v-for="attempt in examDetail.attempts" :key="attempt.submitted">
<td>{{ attempt.submitted }}</td>
<td>{{ attempt.score }}</td>
<td :class="attempt.result === '通过' ? 'ok' : 'late'">{{ attempt.result }}</td>
</tr>
</tbody>
</table>
</div>
<div v-else-if="trainingDetail" class="detail">
<dl class="detail-facts">
<div><dt>负责人</dt><dd>{{ trainingDetail.owner || "—" }}</dd></div>
<div><dt>进度</dt><dd>{{ trainingDetail.progress }}</dd></div>
<div><dt>进度排名</dt><dd>{{ trainingDetail.rank }}</dd></div>
<div><dt>要求完成日期</dt><dd>{{ trainingDetail.deadline }}</dd></div>
<div><dt>已获总学时</dt><dd>{{ trainingDetail.earned_hours }}</dd></div>
<div><dt>实际学时</dt><dd>{{ trainingDetail.actual_hours }}</dd></div>
<div><dt>奖励学时</dt><dd>{{ trainingDetail.reward_hours }}</dd></div>
<div><dt>已获总学分</dt><dd>{{ trainingDetail.earned_credits }}</dd></div>
<div><dt>实际学分</dt><dd>{{ trainingDetail.actual_credits }}</dd></div>
<div><dt>奖励学分</dt><dd>{{ trainingDetail.reward_credits }}</dd></div>
</dl>
<section v-for="phase in trainingDetail.phases" :key="phase.name" class="phase">
<h4>{{ phase.name }}<span>{{ phase.hours }} 分钟 · {{ phase.credits }} 学分</span></h4>
<ul>
<li v-for="task in phase.tasks" :key="task.name">
<strong>{{ task.name }}</strong>
<span>{{ task.progress }}<template v-if="task.score"> · {{ task.score }} 学分</template><template v-if="task.hours"> · {{ task.hours }} 分钟</template></span>
</li>
</ul>
</section>
</div>
</n-modal>
</section>
</template>
+24
View File
@@ -106,6 +106,30 @@ export const themeOverrides = computed<GlobalThemeOverrides>(() => {
itemColorHover: current.vars["--elevated"],
borderRadius: "4px",
},
Pagination: {
itemTextColor: current.vars["--ink"],
itemTextColorHover: current.vars["--accent"],
itemTextColorPressed: current.vars["--accent"],
itemTextColorActive: current.vars["--accent"],
itemTextColorDisabled: current.vars["--muted"],
itemColor: "transparent",
itemColorHover: current.vars["--accent-soft"],
itemColorPressed: "transparent",
itemColorActive: "transparent",
itemColorActiveHover: "transparent",
itemBorder: `1px solid ${current.vars["--line"]}`,
itemBorderHover: `1px solid ${current.vars["--accent"]}`,
itemBorderPressed: `1px solid ${current.vars["--accent"]}`,
itemBorderActive: `1px solid ${current.vars["--accent"]}`,
itemBorderDisabled: `1px solid ${current.vars["--line"]}`,
buttonBorder: `1px solid ${current.vars["--line"]}`,
buttonBorderHover: `1px solid ${current.vars["--accent"]}`,
buttonBorderPressed: `1px solid ${current.vars["--accent"]}`,
buttonIconColor: current.vars["--ink"],
buttonIconColorHover: current.vars["--accent"],
buttonIconColorPressed: current.vars["--accent"],
jumperTextColor: current.vars["--muted"],
},
DataTable: {
thColor: "transparent",
tdColor: "transparent",
+211 -14
View File
@@ -351,35 +351,60 @@ select {
overflow: hidden;
}
.home-page {
.home-page,
.list-page {
overflow: hidden;
}
.home-page .page-head {
flex: none;
margin-bottom: 16px;
}
.home-page .page-head h2 {
margin: 0;
}
.home {
flex: 1;
min-height: 0;
overflow: auto;
}
.list-page {
display: flex;
flex-direction: column;
gap: 16px;
overflow: hidden;
}
.list-page > .board {
flex: 1;
min-height: 0;
}
.home-summary {
flex: none;
display: flex;
flex-direction: column;
gap: 16px;
padding: 16px;
background: var(--panel);
border: 1px solid var(--line);
border-radius: 4px;
}
.home-summary .facts {
margin: 0;
padding-top: 16px;
border-top: 1px solid var(--line);
}
.home-top {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
}
.home-top-main {
min-width: 0;
flex: 1;
}
.home-refresh.n-button {
flex: none;
color: var(--muted);
}
.profile {
display: flex;
align-items: center;
@@ -460,13 +485,23 @@ select {
.board h3 {
flex: none;
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin: 0;
padding: 12px 16px;
padding: 8px 12px 8px 16px;
border-bottom: 1px solid var(--line);
font-size: 14px;
font-weight: 650;
}
.board-tools {
display: flex;
align-items: center;
gap: 8px;
}
.rail {
flex: 1;
min-height: 0;
@@ -678,6 +713,109 @@ select {
color: var(--muted);
}
.spin {
width: 14px;
height: 14px;
flex: none;
box-sizing: border-box;
border: 1.5px solid var(--line);
border-top-color: var(--accent);
border-radius: 50%;
animation: home-spin 0.8s linear infinite;
}
.profile .spin {
margin-left: auto;
}
.detail-loading {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 16px;
}
.detail-loading .spin {
width: 18px;
height: 18px;
}
.bone {
display: block;
background: var(--line);
border-radius: 4px;
}
.bone-copy {
display: flex;
flex-direction: column;
gap: 8px;
min-width: 0;
}
.bone-avatar {
width: 48px;
height: 48px;
flex: none;
}
.bone-name {
width: 96px;
height: 14px;
}
.bone-meta {
width: 168px;
height: 12px;
}
.bone-value {
width: 52px;
height: 18px;
}
.bone-list {
list-style: none;
margin: 0;
padding: 0;
}
.bone-event {
display: grid;
gap: 8px;
padding: 14px 0 14px 24px;
}
.bone-time {
width: 140px;
height: 12px;
}
.bone-title {
width: min(240px, 86%);
height: 14px;
}
.section-error {
margin: 12px 0 0;
}
.summary-error {
margin: 0;
}
@keyframes home-spin {
to {
transform: rotate(360deg);
}
}
@media (prefers-reduced-motion: reduce) {
.spin {
animation: none;
}
}
@media (max-width: 800px) {
.page {
padding: 16px 12px;
@@ -710,6 +848,65 @@ select {
margin: 0;
}
.admin-page .page-head p {
margin: 4px 0 0;
color: var(--muted);
font-size: 12px;
}
.course-search {
display: flex;
gap: 8px;
align-items: center;
width: min(560px, 100%);
}
.course-kinds {
flex: none;
display: flex;
flex-wrap: wrap;
gap: 8px;
margin: 0 0 12px;
}
.kind-chip {
border: 1px solid var(--line);
background: transparent;
color: var(--ink);
border-radius: 4px;
padding: 4px 10px;
font: inherit;
cursor: pointer;
}
.kind-chip span {
margin-left: 6px;
color: var(--muted);
font-variant-numeric: tabular-nums;
}
.kind-chip.active {
border-color: var(--accent);
color: var(--accent);
}
.course-url {
max-width: 280px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.course-url a {
color: var(--accent);
}
.course-pager {
flex: none;
justify-content: flex-end;
margin-top: 12px;
}
.admin-page > .settings-form {
display: flex;
flex: 1;