feat: 考试和培训独立成页并加入导航

This commit is contained in:
2026-11-15 00:48:36 +08:00
parent fa6811dab7
commit df3201945e
4 changed files with 476 additions and 6 deletions
+215
View File
@@ -0,0 +1,215 @@
<script setup lang="ts">
import { onMounted, ref } from "vue";
import { NAlert, NButton, NIcon, NModal, NTooltip } from "naive-ui";
import { RefreshOutline } from "@vicons/ionicons5";
interface ExamItem {
name: string;
status: string;
result: string;
start: 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[];
}
type SectionState = "loading" | "ready" | "error";
const items = ref<ExamItem[] | null>(null);
const state = ref<SectionState>("loading");
const errorText = ref("");
const refreshing = ref(false);
const dialogOpen = ref(false);
const dialogTitle = ref("");
const detail = ref<ExamDetail | null>(null);
const detailError = ref("");
const detailLoading = ref(false);
let detailRequest = 0;
let refreshRunning = false;
let busy = false;
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/exams");
const payload = await response.json().catch(() => ({}));
if (!response.ok) {
if (!hadData) {
errorText.value = failureText(payload, "考试列表没有获取到。");
state.value = "error";
}
return;
}
items.value = Array.isArray(payload.exams) ? payload.exams : [];
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: ExamItem) {
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/exam?${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="exam-heading" :aria-busy="state === 'loading'">
<h3>
<span id="exam-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.start}-${item.name}`" class="event">
<span class="mark" aria-hidden="true"></span>
<button type="button" class="event-hit" @click="openDetail(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>
<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">
<p v-if="detail.notice" class="detail-notice">{{ detail.notice }}</p>
<dl class="detail-facts">
<div><dt>试卷总分</dt><dd>{{ detail.total_score }}</dd></div>
<div><dt>合格分数</dt><dd>{{ detail.pass_score }}</dd></div>
<div><dt>试卷题数</dt><dd>{{ detail.questions }}</dd></div>
<div><dt>考试次数</dt><dd>{{ detail.times }}</dd></div>
<div><dt>开考时间</dt><dd>{{ detail.window }}</dd></div>
<div><dt>考试时限</dt><dd>{{ detail.limit }}</dd></div>
<div><dt>姓名</dt><dd>{{ detail.examinee }}</dd></div>
<div><dt>查看答卷</dt><dd>{{ detail.view_policy }}</dd></div>
</dl>
<h4>考试历史</h4>
<p v-if="!detail.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 detail.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>
</n-modal>
</section>
</template>
@@ -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>
+33 -4
View File
@@ -5,18 +5,42 @@ import { LogOutOutline, MoonOutline, PersonOutline, SunnyOutline } from "@vicons
import { isDark, toggleTheme } from "../shared/theme";
import { navOptions, type PageKey } from "./navigation";
import HomeView from "../features/home/HomeView.vue";
import ExamsView from "../features/exams/ExamsView.vue";
import TrainingsView from "../features/trainings/TrainingsView.vue";
const props = defineProps<{ employeeId: string }>();
const emit = defineEmits<{ logout: []; revoke: [] }>();
const page = ref<PageKey>("home");
const page = ref<PageKey>(pageFromHash());
const collapsed = ref(window.innerWidth < 800);
function pageFromHash(): PageKey {
const name = window.location.hash.replace(/^#\/?/, "");
if (name === "exams" || name === "trainings" || name === "home") return name;
return "home";
}
function remember(next: PageKey) {
const hash = `#/${next}`;
if (window.location.hash !== hash) history.replaceState(null, "", hash);
}
function onHashChange() {
page.value = pageFromHash();
}
function onResize() {
if (window.innerWidth < 800) collapsed.value = true;
}
onMounted(() => window.addEventListener("resize", onResize));
onUnmounted(() => window.removeEventListener("resize", onResize));
onMounted(() => {
remember(page.value);
window.addEventListener("resize", onResize);
window.addEventListener("hashchange", onHashChange);
});
onUnmounted(() => {
window.removeEventListener("resize", onResize);
window.removeEventListener("hashchange", onHashChange);
});
const logoutOptions = [
{ label: "退出当前系统", key: "logout" },
@@ -24,7 +48,10 @@ const logoutOptions = [
];
function onMenu(key: string) {
if (key === "home") page.value = key;
if (key === "home" || key === "exams" || key === "trainings") {
page.value = key;
remember(key);
}
}
function onLogout(key: string) {
@@ -84,6 +111,8 @@ function onLogout(key: string) {
</n-layout-sider>
<n-layout class="content" :native-scrollbar="false" content-style="height: 100%; min-height: 0; overflow: hidden; display: flex; flex-direction: column;">
<HomeView v-if="page === 'home'" :employee-id="props.employeeId" />
<ExamsView v-else-if="page === 'exams'" />
<TrainingsView v-else />
</n-layout>
</n-layout>
</template>
+4 -2
View File
@@ -1,14 +1,16 @@
import type { Component } from "vue";
import { h } from "vue";
import { NIcon } from "naive-ui";
import { HomeOutline } from "@vicons/ionicons5";
import { HomeOutline, ReaderOutline, SchoolOutline } from "@vicons/ionicons5";
function renderIcon(icon: Component) {
return () => h(NIcon, null, { default: () => h(icon) });
}
export type PageKey = "home";
export type PageKey = "home" | "exams" | "trainings";
export const navOptions = [
{ label: "首页", key: "home" as const, icon: renderIcon(HomeOutline) },
{ label: "我的考试", key: "exams" as const, icon: renderIcon(ReaderOutline) },
{ label: "线上培训", key: "trainings" as const, icon: renderIcon(SchoolOutline) },
];