feat: 首页拆分独立加载、骨架动画和手动刷新
This commit is contained in:
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user