feat: 考试和培训独立成页并加入导航
This commit is contained in:
@@ -0,0 +1,224 @@
|
||||
<script setup lang="ts">
|
||||
import { onMounted, ref } from "vue";
|
||||
import { NAlert, NButton, NIcon, NModal, NTooltip } from "naive-ui";
|
||||
import { RefreshOutline } from "@vicons/ionicons5";
|
||||
|
||||
interface TrainingItem {
|
||||
name: string;
|
||||
status: string;
|
||||
progress: number;
|
||||
assigned: string;
|
||||
credits: string;
|
||||
detail: string;
|
||||
}
|
||||
|
||||
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[];
|
||||
}
|
||||
|
||||
type SectionState = "loading" | "ready" | "error";
|
||||
|
||||
const items = ref<TrainingItem[] | null>(null);
|
||||
const state = ref<SectionState>("loading");
|
||||
const errorText = ref("");
|
||||
const refreshing = ref(false);
|
||||
const dialogOpen = ref(false);
|
||||
const dialogTitle = ref("");
|
||||
const detail = ref<TrainingDetail | null>(null);
|
||||
const detailError = ref("");
|
||||
const detailLoading = ref(false);
|
||||
let detailRequest = 0;
|
||||
let refreshRunning = false;
|
||||
let busy = false;
|
||||
|
||||
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 load() {
|
||||
if (busy) return;
|
||||
busy = true;
|
||||
const hadData = items.value !== null;
|
||||
if (!hadData) state.value = "loading";
|
||||
try {
|
||||
const response = await fetch("/api/home/trainings");
|
||||
const payload = await response.json().catch(() => ({}));
|
||||
if (!response.ok) {
|
||||
if (!hadData) {
|
||||
errorText.value = failureText(payload, "培训列表没有获取到。");
|
||||
state.value = "error";
|
||||
}
|
||||
return;
|
||||
}
|
||||
items.value = Array.isArray(payload.trainings) ? payload.trainings : [];
|
||||
errorText.value = "";
|
||||
state.value = "ready";
|
||||
} catch {
|
||||
if (!hadData) {
|
||||
errorText.value = "培训列表没有获取到。";
|
||||
state.value = "error";
|
||||
}
|
||||
} finally {
|
||||
busy = false;
|
||||
}
|
||||
}
|
||||
|
||||
function refresh() {
|
||||
if (refreshRunning) return;
|
||||
refreshRunning = true;
|
||||
refreshing.value = true;
|
||||
void load().finally(() => {
|
||||
refreshRunning = false;
|
||||
refreshing.value = false;
|
||||
});
|
||||
}
|
||||
|
||||
async function openDetail(item: TrainingItem) {
|
||||
const current = ++detailRequest;
|
||||
dialogTitle.value = item.name;
|
||||
detail.value = null;
|
||||
detailError.value = "";
|
||||
dialogOpen.value = true;
|
||||
if (!item.detail) {
|
||||
detailError.value = "明细没有获取到。";
|
||||
detailLoading.value = false;
|
||||
return;
|
||||
}
|
||||
detailLoading.value = true;
|
||||
try {
|
||||
const response = await fetch(`/api/training?${new URLSearchParams({ path: item.detail })}`);
|
||||
const payload = await response.json().catch(() => ({}));
|
||||
if (current !== detailRequest) return;
|
||||
if (!response.ok) {
|
||||
detailError.value = failureText(payload, "明细没有获取到。");
|
||||
return;
|
||||
}
|
||||
detail.value = payload;
|
||||
} catch {
|
||||
if (current === detailRequest) detailError.value = "明细没有获取到。";
|
||||
} finally {
|
||||
if (current === detailRequest) detailLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(refresh);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section class="page list-page">
|
||||
<section class="board" aria-labelledby="training-heading" :aria-busy="state === 'loading'">
|
||||
<h3>
|
||||
<span id="training-heading">线上培训</span>
|
||||
<span class="board-tools">
|
||||
<span v-if="state === 'loading'" class="spin" role="status" aria-label="正在获取"></span>
|
||||
<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>
|
||||
</span>
|
||||
</h3>
|
||||
<div class="rail" tabindex="0">
|
||||
<n-alert v-if="state === 'error'" class="section-error" type="error">{{ errorText }}</n-alert>
|
||||
<p v-else-if="items && !items.length" class="rail-empty">没有线上培训</p>
|
||||
<ol v-else-if="items">
|
||||
<li v-for="item in items" :key="`${item.assigned}-${item.name}`" class="event">
|
||||
<span class="mark" aria-hidden="true"></span>
|
||||
<button type="button" class="event-hit" @click="openDetail(item)">
|
||||
<time>{{ item.assigned }}</time>
|
||||
<strong>{{ item.name }}</strong>
|
||||
<p class="meta"><span>{{ trainingMeta(item) }}</span></p>
|
||||
</button>
|
||||
</li>
|
||||
</ol>
|
||||
<ol v-else class="bone-list" aria-hidden="true">
|
||||
<li v-for="row in 6" :key="row" class="bone-event">
|
||||
<span class="bone bone-time"></span>
|
||||
<span class="bone bone-title"></span>
|
||||
</li>
|
||||
</ol>
|
||||
</div>
|
||||
</section>
|
||||
<n-modal
|
||||
v-model:show="dialogOpen"
|
||||
preset="card"
|
||||
:title="dialogTitle"
|
||||
:style="{ width: 'min(760px, calc(100vw - 32px))' }"
|
||||
:content-style="{ maxHeight: '70vh', overflow: 'auto' }"
|
||||
>
|
||||
<div v-if="detailLoading" class="detail-loading" role="status" aria-label="正在获取">
|
||||
<span class="spin"></span>
|
||||
<div class="bone-copy">
|
||||
<span class="bone bone-meta"></span>
|
||||
<span class="bone bone-title"></span>
|
||||
<span class="bone bone-time"></span>
|
||||
</div>
|
||||
</div>
|
||||
<n-alert v-else-if="detailError" type="error">{{ detailError }}</n-alert>
|
||||
<div v-else-if="detail" class="detail">
|
||||
<dl class="detail-facts">
|
||||
<div><dt>负责人</dt><dd>{{ detail.owner || "—" }}</dd></div>
|
||||
<div><dt>进度</dt><dd>{{ detail.progress }}</dd></div>
|
||||
<div><dt>进度排名</dt><dd>{{ detail.rank }}</dd></div>
|
||||
<div><dt>要求完成日期</dt><dd>{{ detail.deadline }}</dd></div>
|
||||
<div><dt>已获总学时</dt><dd>{{ detail.earned_hours }}</dd></div>
|
||||
<div><dt>实际学时</dt><dd>{{ detail.actual_hours }}</dd></div>
|
||||
<div><dt>奖励学时</dt><dd>{{ detail.reward_hours }}</dd></div>
|
||||
<div><dt>已获总学分</dt><dd>{{ detail.earned_credits }}</dd></div>
|
||||
<div><dt>实际学分</dt><dd>{{ detail.actual_credits }}</dd></div>
|
||||
<div><dt>奖励学分</dt><dd>{{ detail.reward_credits }}</dd></div>
|
||||
</dl>
|
||||
<section v-for="phase in detail.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