99 lines
3.7 KiB
Vue
99 lines
3.7 KiB
Vue
<script setup lang="ts">
|
|
import { h, onMounted, onUnmounted, ref } from "vue";
|
|
import type { Component } from "vue";
|
|
import { useRoute, useRouter } from "vue-router";
|
|
import { NButton, NIcon, NLayout, NLayoutSider, NMenu } from "naive-ui";
|
|
import { AppsOutline, CloudDownloadOutline, HomeOutline, KeyOutline, ListOutline, LogOutOutline, PeopleOutline, PersonOutline, SettingsOutline } from "@vicons/ionicons5";
|
|
import { appUrl, platformHome } from "../shared/base";
|
|
import { api, clearSession, sessionInfo, token } from "../shared/http";
|
|
import { hosted } from "../shared/installState";
|
|
import ThemeButton from "./ThemeButton.vue";
|
|
|
|
function renderIcon(icon: Component) {
|
|
return () => h(NIcon, null, { default: () => h(icon) });
|
|
}
|
|
|
|
const route = useRoute();
|
|
const router = useRouter();
|
|
const { role, name } = sessionInfo();
|
|
const collapsed = ref(window.innerWidth < 800);
|
|
const items = role === "admin"
|
|
? [
|
|
{ label: "首页", key: "/admin", icon: renderIcon(HomeOutline) },
|
|
...(hosted ? [] : [{ label: "用户", key: "/admin/users", icon: renderIcon(PeopleOutline) }]),
|
|
{ label: "大数据账号池", key: "/admin/pool", icon: renderIcon(KeyOutline) },
|
|
{ label: "采集", key: "/admin/sources", icon: renderIcon(CloudDownloadOutline) },
|
|
{ label: "设置", key: "/admin/settings", icon: renderIcon(SettingsOutline) },
|
|
]
|
|
: [{ label: "任务", key: "/tasks", icon: renderIcon(ListOutline) }];
|
|
|
|
function onResize() {
|
|
if (window.innerWidth < 800) collapsed.value = true;
|
|
}
|
|
|
|
onMounted(() => window.addEventListener("resize", onResize));
|
|
onUnmounted(() => window.removeEventListener("resize", onResize));
|
|
|
|
async function logout() {
|
|
if (token()) {
|
|
try {
|
|
await api("/api/auth/logout", { method: "POST" });
|
|
} catch {
|
|
/* 本地会话仍会清掉 */
|
|
}
|
|
}
|
|
clearSession();
|
|
window.location.assign(appUrl("login"));
|
|
}
|
|
</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">D</div>
|
|
<strong v-if="!collapsed">DecomEval</strong>
|
|
</div>
|
|
<n-menu
|
|
:collapsed="collapsed"
|
|
:collapsed-width="64"
|
|
:collapsed-icon-size="22"
|
|
:value="route.path"
|
|
:options="items"
|
|
@update:value="(key) => router.push(String(key))"
|
|
/>
|
|
<a v-if="hosted" class="sider-back" :class="{ compact: collapsed }" :href="platformHome" :title="collapsed ? '返回平台' : undefined">
|
|
<n-icon :size="collapsed ? 22 : 18"><AppsOutline /></n-icon>
|
|
<span v-if="!collapsed">返回平台</span>
|
|
</a>
|
|
<div class="sider-user" :class="{ compact: collapsed }">
|
|
<n-icon :size="18"><PersonOutline /></n-icon>
|
|
<div v-if="!collapsed" class="who">
|
|
<strong>{{ name }}</strong>
|
|
<span>已登录</span>
|
|
</div>
|
|
<ThemeButton />
|
|
<n-button class="logout-btn" quaternary circle size="small" aria-label="退出" @click="logout">
|
|
<template #icon><n-icon><LogOutOutline /></n-icon></template>
|
|
</n-button>
|
|
</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;">
|
|
<slot />
|
|
</n-layout>
|
|
</n-layout>
|
|
</template>
|