feat: 登录后进入实时首页,并加密保存可刷新的登录凭证

This commit is contained in:
2026-10-26 18:12:00 +08:00
parent eaad01d5a8
commit 91707f959f
14 changed files with 748 additions and 136 deletions
@@ -1,22 +0,0 @@
<script setup lang="ts">
import { onMounted, ref } from "vue";
import { NDataTable } from "naive-ui";
const rows = ref<{ employee_id: string }[]>([]);
const columns = [{ title: "工号", key: "employee_id" }];
onMounted(async () => {
const response = await fetch("/api/accounts");
if (response.ok) rows.value = await response.json();
});
</script>
<template>
<section class="page">
<header class="page-head">
<h2>使用者</h2>
</header>
<n-data-table :columns="columns" :data="rows" :bordered="false" />
</section>
</template>
+115
View File
@@ -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>
+92 -38
View File
@@ -7,60 +7,114 @@ export interface Scheme {
primary: string;
primaryHover: string;
primaryPressed: string;
danger: string;
vars: Record<string, string>;
}
export const schemes: Scheme[] = [
{
id: "light",
dark: false,
primary: "#2563eb",
primaryHover: "#1d4ed8",
primaryPressed: "#1e40af",
id: "graphite",
dark: true,
primary: "#1F6FEB",
primaryHover: "#388BFD",
primaryPressed: "#1158C7",
danger: "#F85149",
vars: {
"--bg": "#f3f4f6",
"--panel": "#ffffff",
"--ink": "#111827",
"--muted": "#6b7280",
"--line": "#e5e7eb",
"--accent": "#2563eb",
"--accent-soft": "#eff6ff",
"--sidebar": "#ffffff",
"--bg": "#0D1117",
"--panel": "#161A22",
"--elevated": "#1B212B",
"--ink": "#E6EDF3",
"--muted": "#9AA4AE",
"--line": "#262C36",
"--accent": "#79C0FF",
"--accent-soft": "rgba(121, 192, 255, 0.16)",
"--sidebar": "#0D1117",
"--danger": "#F85149",
},
},
{
id: "dark",
dark: true,
primary: "#60a5fa",
primaryHover: "#93c5fd",
primaryPressed: "#3b82f6",
id: "day",
dark: false,
primary: "#1F6FEB",
primaryHover: "#1158C7",
primaryPressed: "#0D419D",
danger: "#CF222E",
vars: {
"--bg": "#0f172a",
"--panel": "#1e293b",
"--ink": "#f8fafc",
"--muted": "#94a3b8",
"--line": "#334155",
"--accent": "#60a5fa",
"--accent-soft": "#1e3a8a",
"--sidebar": "#111827",
"--bg": "#F6F8FA",
"--panel": "#FFFFFF",
"--elevated": "#F6F8FA",
"--ink": "#0D1117",
"--muted": "#57606A",
"--line": "#D0D7DE",
"--accent": "#0969DA",
"--accent-soft": "rgba(9, 105, 218, 0.12)",
"--sidebar": "#FFFFFF",
"--danger": "#CF222E",
},
},
];
const stored = localStorage.getItem("euniv-scheme");
export const schemeId = ref(schemes.some((item) => item.id === stored) ? stored! : "light");
export const schemeId = ref(stored === "day" ? "day" : "graphite");
export const scheme = computed(() => schemes.find((item) => item.id === schemeId.value) ?? schemes[0]);
export const isDark = computed(() => scheme.value.dark);
export const themeOverrides = computed<GlobalThemeOverrides>(() => ({
common: {
primaryColor: scheme.value.primary,
primaryColorHover: scheme.value.primaryHover,
primaryColorPressed: scheme.value.primaryPressed,
primaryColorSuppl: scheme.value.primary,
borderRadius: "6px",
fontFamily: '"Segoe UI", "Microsoft YaHei", sans-serif',
},
}));
export const themeOverrides = computed<GlobalThemeOverrides>(() => {
const current = scheme.value;
return {
common: {
primaryColor: current.primary,
primaryColorHover: current.primaryHover,
primaryColorPressed: current.primaryPressed,
primaryColorSuppl: current.primary,
errorColor: current.danger,
errorColorHover: current.danger,
errorColorPressed: current.danger,
bodyColor: current.vars["--bg"],
cardColor: current.vars["--panel"],
modalColor: current.vars["--panel"],
popoverColor: current.vars["--elevated"],
tableColor: current.vars["--panel"],
inputColor: current.dark ? current.vars["--elevated"] : "#ffffff",
borderColor: current.vars["--line"],
dividerColor: current.vars["--line"],
textColorBase: current.vars["--ink"],
textColor1: current.vars["--ink"],
textColor2: current.vars["--muted"],
textColor3: current.vars["--muted"],
placeholderColor: current.vars["--muted"],
iconColor: current.vars["--muted"],
hoverColor: current.vars["--accent-soft"],
borderRadius: "4px",
fontFamily: '"Segoe UI", "Microsoft YaHei", sans-serif',
},
Button: {
textColorPrimary: "#ffffff",
textColorHoverPrimary: "#ffffff",
textColorPressedPrimary: "#ffffff",
textColorFocusPrimary: "#ffffff",
},
Menu: {
itemTextColor: current.vars["--ink"],
itemIconColor: current.vars["--muted"],
itemTextColorActive: current.vars["--accent"],
itemIconColorActive: current.vars["--accent"],
itemTextColorHover: current.vars["--ink"],
itemIconColorHover: current.vars["--ink"],
itemColorActive: "transparent",
itemColorActiveHover: "transparent",
itemColorActiveCollapsed: "transparent",
itemColorHover: current.vars["--elevated"],
borderRadius: "4px",
},
DataTable: {
thColor: "transparent",
tdColor: "transparent",
borderColor: current.vars["--line"],
thTextColor: current.vars["--muted"],
tdTextColor: current.vars["--ink"],
},
};
});
watch(scheme, (value) => {
localStorage.setItem("euniv-scheme", value.id);
@@ -73,5 +127,5 @@ watch(scheme, (value) => {
}, { immediate: true });
export function toggleTheme() {
schemeId.value = scheme.value.dark ? "light" : "dark";
schemeId.value = scheme.value.dark ? "day" : "graphite";
}
+18 -7
View File
@@ -1,18 +1,25 @@
<script setup lang="ts">
import { ref } from "vue";
import { onMounted, onUnmounted, ref } from "vue";
import { NButton, NIcon, NLayout, NLayoutSider, NMenu, NTooltip } from "naive-ui";
import { LogOutOutline, MoonOutline, PersonOutline, SunnyOutline } from "@vicons/ionicons5";
import { isDark, toggleTheme } from "../shared/theme";
import { navOptions, type PageKey } from "./navigation";
import AccountsView from "../features/accounts/AccountsView.vue";
import HomeView from "../features/home/HomeView.vue";
const props = defineProps<{ employeeId: string }>();
const emit = defineEmits<{ logout: [] }>();
const page = ref<PageKey>("accounts");
const collapsed = ref(false);
const page = ref<PageKey>("home");
const collapsed = ref(window.innerWidth < 800);
function onResize() {
if (window.innerWidth < 800) collapsed.value = true;
}
onMounted(() => window.addEventListener("resize", onResize));
onUnmounted(() => window.removeEventListener("resize", onResize));
function onMenu(key: string) {
if (key === "accounts") page.value = key;
if (key === "home") page.value = key;
}
</script>
@@ -31,6 +38,10 @@ function onMenu(key: string) {
@expand="collapsed = false"
>
<div class="sider-body">
<div class="sider-brand" :class="{ compact: collapsed }">
<div class="brand-mark" aria-hidden="true">E</div>
<strong v-if="!collapsed">EunivTools</strong>
</div>
<n-menu
:collapsed="collapsed"
:collapsed-width="64"
@@ -55,7 +66,7 @@ function onMenu(key: string) {
</n-tooltip>
<n-tooltip>
<template #trigger>
<n-button quaternary circle size="small" aria-label="退出" @click="emit('logout')">
<n-button class="logout-btn" quaternary circle size="small" aria-label="退出" @click="emit('logout')">
<template #icon><n-icon><LogOutOutline /></n-icon></template>
</n-button>
</template>
@@ -65,7 +76,7 @@ function onMenu(key: string) {
</div>
</n-layout-sider>
<n-layout class="content">
<AccountsView v-if="page === 'accounts'" />
<HomeView v-if="page === 'home'" :employee-id="props.employeeId" />
</n-layout>
</n-layout>
</template>
+3 -3
View File
@@ -1,14 +1,14 @@
import type { Component } from "vue";
import { h } from "vue";
import { NIcon } from "naive-ui";
import { PeopleOutline } from "@vicons/ionicons5";
import { HomeOutline } from "@vicons/ionicons5";
function renderIcon(icon: Component) {
return () => h(NIcon, null, { default: () => h(icon) });
}
export type PageKey = "accounts";
export type PageKey = "home";
export const navOptions = [
{ label: "使用者", key: "accounts" as const, icon: renderIcon(PeopleOutline) },
{ label: "首页", key: "home" as const, icon: renderIcon(HomeOutline) },
];
+196 -29
View File
@@ -3,25 +3,29 @@
background: var(--bg);
font-family: "Segoe UI", "Microsoft YaHei", sans-serif;
font-size: 14px;
--bg: #f3f4f6;
--panel: #ffffff;
--ink: #111827;
--muted: #6b7280;
--line: #e5e7eb;
--accent: #2563eb;
--accent-soft: #eff6ff;
--sidebar: #ffffff;
--bg: #0d1117;
--panel: #161a22;
--elevated: #1b212b;
--ink: #e6edf3;
--muted: #9aa4ae;
--line: #262c36;
--accent: #79c0ff;
--accent-soft: rgba(121, 192, 255, 0.16);
--sidebar: #0d1117;
--danger: #f85149;
}
:root[data-theme="dark"] {
--bg: #0f172a;
--panel: #1e293b;
--ink: #f8fafc;
--muted: #94a3b8;
--line: #334155;
--accent: #60a5fa;
--accent-soft: #1e3a8a;
--sidebar: #111827;
:root[data-scheme="day"] {
--bg: #f6f8fa;
--panel: #ffffff;
--elevated: #f6f8fa;
--ink: #0d1117;
--muted: #57606a;
--line: #d0d7de;
--accent: #0969da;
--accent-soft: rgba(9, 105, 218, 0.12);
--sidebar: #ffffff;
--danger: #cf222e;
}
::selection {
@@ -29,6 +33,15 @@
color: var(--ink);
}
:focus-visible {
outline: 2px solid var(--accent);
outline-offset: 2px;
}
* {
scrollbar-color: var(--line) transparent;
}
html,
body,
#app,
@@ -66,8 +79,7 @@ select {
width: min(440px, 100%);
background: var(--panel);
border: 1px solid var(--line);
border-radius: 12px;
box-shadow: 0 10px 28px rgba(17, 24, 39, 0.08);
border-radius: 4px;
padding: 28px 28px 22px;
}
@@ -80,23 +92,31 @@ select {
}
.brand-mark {
width: 44px;
height: 44px;
border-radius: 10px;
background: var(--accent);
color: #fff;
width: 28px;
height: 28px;
border-radius: 4px;
border: 1px solid var(--accent);
color: var(--accent);
background: transparent;
display: grid;
place-items: center;
flex: none;
font-size: 22px;
font-weight: 600;
font-size: 15px;
font-weight: 650;
line-height: 1;
}
.gate .brand-mark {
width: 36px;
height: 36px;
font-size: 18px;
}
.brand h1 {
margin: 0;
font-size: 20px;
line-height: 1.3;
letter-spacing: -0.03em;
}
.form-row {
@@ -127,8 +147,15 @@ select {
color: var(--ink);
}
.shell .n-layout,
.shell .n-layout-scroll-container,
.content {
background: var(--bg);
}
.shell .n-layout-sider {
background: var(--sidebar);
border-right: 1px solid var(--line);
}
.shell .n-layout-sider > .n-scrollbar,
@@ -145,9 +172,37 @@ select {
min-height: 0;
}
.sider-brand {
height: 56px;
display: flex;
align-items: center;
gap: 10px;
padding: 0 16px;
border-bottom: 1px solid var(--line);
flex: none;
}
.sider-brand.compact {
justify-content: center;
padding: 0;
}
.sider-brand strong {
font-size: 15px;
font-weight: 650;
letter-spacing: -0.02em;
}
.sider-body .n-menu {
flex: 1;
min-height: 0;
padding-top: 8px;
}
.shell .n-menu .n-menu-item-content--selected::before {
background-color: transparent !important;
border-left: 2px solid var(--accent);
border-radius: 0;
}
.sider-user {
@@ -182,6 +237,18 @@ select {
font-size: 12px;
}
.sider-user strong {
font-variant-numeric: tabular-nums;
}
.logout-btn.n-button {
color: var(--danger);
--n-text-color: var(--danger);
--n-text-color-hover: var(--danger);
--n-text-color-pressed: var(--danger);
--n-text-color-focus: var(--danger);
}
.content {
min-width: 0;
min-height: 0;
@@ -195,18 +262,118 @@ select {
min-height: 0;
display: flex;
flex-direction: column;
padding: 16px 20px;
padding: 28px 32px 20px;
box-sizing: border-box;
}
.page-head {
display: flex;
align-items: center;
align-items: flex-end;
justify-content: space-between;
margin-bottom: 12px;
margin-bottom: 16px;
}
.page-head h2 {
margin: 0 0 6px;
font-size: 22px;
font-weight: 650;
letter-spacing: -0.03em;
}
.page-head p {
margin: 0;
color: var(--muted);
}
.page .n-data-table {
font-variant-numeric: tabular-nums;
}
.page .n-data-table td {
white-space: nowrap;
}
.home {
min-height: 0;
overflow: auto;
}
.profile {
display: flex;
align-items: center;
gap: 14px;
margin-bottom: 16px;
}
.profile strong,
.profile span {
display: block;
}
.profile span {
margin-top: 4px;
color: var(--muted);
}
.avatar,
.avatar-fallback {
width: 48px;
height: 48px;
border-radius: 4px;
}
.avatar {
object-fit: cover;
border: 1px solid var(--line);
}
.avatar-fallback {
font-size: 20px;
}
.facts {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
margin: 0 0 20px;
border-top: 1px solid var(--line);
border-left: 1px solid var(--line);
}
.facts div {
padding: 12px 16px;
border-right: 1px solid var(--line);
border-bottom: 1px solid var(--line);
}
.facts dt {
color: var(--muted);
font-size: 12px;
}
.facts dd {
margin: 6px 0 0;
font-size: 22px;
font-weight: 650;
letter-spacing: -0.03em;
font-variant-numeric: tabular-nums;
}
.home h3 {
margin: 0 0 8px;
font-size: 16px;
}
.home-status {
margin: 0 0 12px;
color: var(--muted);
}
@media (max-width: 800px) {
.page {
padding: 16px 12px;
}
.facts {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
}