feat: 登录后进入实时首页,并加密保存可刷新的登录凭证
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, onUnmounted, ref } from "vue";
|
||||
import { NAlert, NDataTable } from "naive-ui";
|
||||
|
||||
interface ExamItem {
|
||||
name: string;
|
||||
deadline: string;
|
||||
}
|
||||
|
||||
interface HomeProfile {
|
||||
name: string;
|
||||
avatar: string;
|
||||
department: string;
|
||||
level: string;
|
||||
year_score: string;
|
||||
month_score: string;
|
||||
total_hours: string;
|
||||
total_score: string;
|
||||
today_hours: string | number | null;
|
||||
today_score: string | number | null;
|
||||
pending_exam_count: number;
|
||||
pending_exams: ExamItem[];
|
||||
}
|
||||
|
||||
const props = defineProps<{ employeeId: string }>();
|
||||
const profile = ref<HomeProfile | null>(null);
|
||||
const errorText = ref("");
|
||||
const loading = ref(false);
|
||||
let timer = 0;
|
||||
let requestId = 0;
|
||||
|
||||
const facts = computed(() => {
|
||||
const current = profile.value;
|
||||
if (!current) return [];
|
||||
return [
|
||||
{ label: "本年学分", value: current.year_score },
|
||||
{ label: "本月学分", value: current.month_score },
|
||||
{ label: "累计学时", value: current.total_hours },
|
||||
{ label: "累计学分", value: current.total_score },
|
||||
{ label: "今日学时", value: current.today_hours ?? "—" },
|
||||
{ label: "今日学分", value: current.today_score ?? "—" },
|
||||
];
|
||||
});
|
||||
|
||||
const columns = [
|
||||
{ title: "考试", key: "name" },
|
||||
{ title: "时间", key: "deadline" },
|
||||
];
|
||||
|
||||
async function load() {
|
||||
const current = ++requestId;
|
||||
loading.value = profile.value === null;
|
||||
try {
|
||||
const response = await fetch(`/api/home?employee_id=${encodeURIComponent(props.employeeId)}`);
|
||||
const payload = await response.json().catch(() => ({}));
|
||||
if (current !== requestId) return;
|
||||
if (!response.ok) {
|
||||
errorText.value = typeof payload.detail === "string" ? payload.detail : "首页信息没有获取到,30 秒后会再试。";
|
||||
return;
|
||||
}
|
||||
errorText.value = "";
|
||||
profile.value = payload;
|
||||
} catch {
|
||||
if (current === requestId) errorText.value = "首页信息没有获取到,30 秒后会再试。";
|
||||
} finally {
|
||||
if (current === requestId) loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
void load();
|
||||
timer = window.setInterval(() => void load(), 30000);
|
||||
});
|
||||
|
||||
onUnmounted(() => window.clearInterval(timer));
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section class="page">
|
||||
<header class="page-head">
|
||||
<div>
|
||||
<h2>首页</h2>
|
||||
<p>个人信息每次进入和之后每 30 秒重新获取,不写入本地。</p>
|
||||
</div>
|
||||
</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" style="margin-bottom: 12px">{{ errorText }}</n-alert>
|
||||
<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>
|
||||
</div>
|
||||
<dl class="facts">
|
||||
<div v-for="item in facts" :key="item.label">
|
||||
<dt>{{ item.label }}</dt>
|
||||
<dd>{{ item.value }}</dd>
|
||||
</div>
|
||||
</dl>
|
||||
<h3>待考试</h3>
|
||||
<p class="home-status">{{ profile.pending_exam_count > 0 ? `有 ${profile.pending_exam_count} 场待考试` : "没有待考试" }}</p>
|
||||
<n-data-table
|
||||
v-if="profile.pending_exams.length"
|
||||
:columns="columns"
|
||||
:data="profile.pending_exams"
|
||||
:bordered="false"
|
||||
/>
|
||||
<p v-else-if="profile.pending_exam_count > 0" class="home-status">清单还没有返回考试名称。</p>
|
||||
</div>
|
||||
</section>
|
||||
</template>
|
||||
Reference in New Issue
Block a user