119 lines
4.1 KiB
Vue
119 lines
4.1 KiB
Vue
<script setup lang="ts">
|
|
import { onMounted, onUnmounted, ref } from "vue";
|
|
import { NButton, NDropdown, 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 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>(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(() => {
|
|
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" },
|
|
{ label: "注销当前账号", key: "revoke" },
|
|
];
|
|
|
|
function onMenu(key: string) {
|
|
if (key === "home" || key === "exams" || key === "trainings") {
|
|
page.value = key;
|
|
remember(key);
|
|
}
|
|
}
|
|
|
|
function onLogout(key: string) {
|
|
if (key === "revoke") emit("revoke");
|
|
else emit("logout");
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<n-layout has-sider class="shell">
|
|
<n-layout-sider
|
|
bordered
|
|
collapse-mode="width"
|
|
:collapsed-width="64"
|
|
:width="220"
|
|
:collapsed="collapsed"
|
|
show-trigger
|
|
:native-scrollbar="false"
|
|
:content-style="{ height: '100%', display: 'flex', flexDirection: 'column' }"
|
|
@collapse="collapsed = true"
|
|
@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"
|
|
:collapsed-icon-size="22"
|
|
:value="page"
|
|
:options="navOptions"
|
|
@update:value="onMenu"
|
|
/>
|
|
<div class="sider-user" :class="{ compact: collapsed }">
|
|
<n-icon :size="18"><PersonOutline /></n-icon>
|
|
<div v-if="!collapsed" class="who">
|
|
<strong>{{ props.employeeId }}</strong>
|
|
<span>已登录</span>
|
|
</div>
|
|
<n-tooltip>
|
|
<template #trigger>
|
|
<n-button quaternary circle size="small" :aria-label="isDark ? '切换到日间' : '切换到夜间'" @click="toggleTheme">
|
|
<template #icon><n-icon><MoonOutline v-if="!isDark" /><SunnyOutline v-else /></n-icon></template>
|
|
</n-button>
|
|
</template>
|
|
{{ isDark ? "切换到日间" : "切换到夜间" }}
|
|
</n-tooltip>
|
|
<n-dropdown trigger="hover" placement="top-end" :options="logoutOptions" @select="onLogout">
|
|
<n-button class="logout-btn" quaternary circle size="small" aria-label="退出">
|
|
<template #icon><n-icon><LogOutOutline /></n-icon></template>
|
|
</n-button>
|
|
</n-dropdown>
|
|
</div>
|
|
</div>
|
|
</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>
|