feat: 实现后台管理前端全部页面与登录布局

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Nixevol
2026-06-28 16:52:41 +08:00
co-authored by Cursor
parent fa61840cfb
commit 5fa84f346b
40 changed files with 4481 additions and 1 deletions
+227
View File
@@ -0,0 +1,227 @@
<template>
<n-layout has-sider position="absolute">
<n-layout-sider
bordered
:collapsed="collapsed"
collapse-mode="width"
:collapsed-width="64"
:width="224"
:native-scrollbar="false"
:inverted="true"
show-trigger="bar"
@collapse="collapsed = true"
@expand="collapsed = false"
>
<div class="brand">
<span class="brand-logo">艺</span>
<span v-show="!collapsed" class="brand-name text-ellipsis">{{ appName }}</span>
</div>
<n-menu
:inverted="true"
:collapsed="collapsed"
:collapsed-width="64"
:value="activeKey"
:options="menuOptions"
@update:value="onMenu"
/>
</n-layout-sider>
<n-layout>
<n-layout-header bordered class="header">
<n-button quaternary circle @click="collapsed = !collapsed">
<template #icon><n-icon :component="MenuOutline" /></template>
</n-button>
<div class="header-title text-ellipsis">{{ currentTitle }}</div>
<div class="spacer" />
<n-select
v-model:value="currentLocale"
:options="localeOptions"
size="small"
style="width: 110px"
@update:value="changeLocale"
/>
<n-dropdown :options="userOptions" @select="onUser">
<n-button quaternary>
<template #icon><n-icon :component="PersonCircleOutline" /></template>
{{ auth.user?.realName || auth.user?.username }}
</n-button>
</n-dropdown>
</n-layout-header>
<n-layout-content class="content" :native-scrollbar="false">
<router-view v-slot="{ Component }">
<transition name="fade" mode="out-in">
<component :is="Component" />
</transition>
</router-view>
</n-layout-content>
</n-layout>
</n-layout>
</template>
<script setup lang="ts">
import { computed, h, onMounted, onUnmounted, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { useI18n } from 'vue-i18n';
import { NIcon } from 'naive-ui';
import type { MenuOption } from 'naive-ui';
import {
GridOutline,
DocumentTextOutline,
ImagesOutline,
QrCodeOutline,
CubeOutline,
ListOutline,
PeopleOutline,
SettingsOutline,
MenuOutline,
PersonCircleOutline,
LogOutOutline,
KeyOutline,
} from '@vicons/ionicons5';
import { useAuthStore } from '@/stores/auth';
import { settingsApi } from '@/api';
import { SUPPORTED_LOCALES, setLocale } from '@/locales';
import { dialog } from '@/utils/feedback';
const route = useRoute();
const router = useRouter();
const { t, locale } = useI18n();
const auth = useAuthStore();
const collapsed = ref(window.innerWidth < 900);
const appName = ref(t('pages.login.title'));
const currentLocale = ref(locale.value);
const localeOptions = SUPPORTED_LOCALES.map((l) => ({ label: l.label, value: l.value }));
function renderIcon(icon: unknown) {
return () => h(NIcon, null, { default: () => h(icon as never) });
}
const menuOptions = computed<MenuOption[]>(() => {
const items: Array<MenuOption & { superOnly?: boolean }> = [
{ label: t('menu.dashboard'), key: 'dashboard', icon: renderIcon(GridOutline) },
{ label: t('menu.templates'), key: 'templates', icon: renderIcon(DocumentTextOutline) },
{ label: t('menu.artworks'), key: 'artworks', icon: renderIcon(ImagesOutline) },
{ label: t('menu.traceCodes'), key: 'traceCodes', icon: renderIcon(QrCodeOutline) },
{ label: t('menu.outbound'), key: 'outbound', icon: renderIcon(CubeOutline) },
{ label: t('menu.queryLogs'), key: 'queryLogs', icon: renderIcon(ListOutline) },
{ label: t('menu.users'), key: 'users', icon: renderIcon(PeopleOutline), superOnly: true },
{ label: t('menu.settings'), key: 'settings', icon: renderIcon(SettingsOutline), superOnly: true },
];
return items.filter((i) => !i.superOnly || auth.isSuperAdmin);
});
const activeKey = computed(() => {
if (route.name === 'artwork-detail') return 'artworks';
return route.name as string;
});
const currentTitle = computed(() => {
const key = activeKey.value;
return key ? t(`menu.${key}`) : '';
});
const userOptions = computed(() => [
{ label: t('menu.profile'), key: 'profile', icon: renderIcon(KeyOutline) },
{ label: t('common.action.logout'), key: 'logout', icon: renderIcon(LogOutOutline) },
]);
function onMenu(key: string) {
router.push({ name: key });
}
function changeLocale(val: string) {
setLocale(val);
currentLocale.value = val;
}
function onUser(key: string) {
if (key === 'profile') {
router.push({ name: 'profile' });
} else if (key === 'logout') {
dialog.warning({
title: t('common.action.logout'),
content: '确定退出登录?',
positiveText: t('common.action.confirm'),
negativeText: t('common.action.cancel'),
onPositiveClick: () => {
auth.logout();
router.push({ name: 'login' });
},
});
}
}
let resizeHandler: () => void;
onMounted(async () => {
resizeHandler = () => {
collapsed.value = window.innerWidth < 900;
};
window.addEventListener('resize', resizeHandler);
try {
const s = await settingsApi.getAll();
if (s.appName) appName.value = s.appName;
} catch {
/* ignore */
}
});
onUnmounted(() => window.removeEventListener('resize', resizeHandler));
</script>
<style scoped>
.brand {
display: flex;
align-items: center;
gap: 10px;
height: 56px;
padding: 0 16px;
color: #fff;
border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}
.brand-logo {
display: inline-flex;
align-items: center;
justify-content: center;
width: 32px;
height: 32px;
border-radius: 8px;
background: #2e5496;
font-weight: 700;
flex-shrink: 0;
}
.brand-name {
font-size: 15px;
font-weight: 600;
}
.header {
display: flex;
align-items: center;
gap: 12px;
height: 56px;
padding: 0 16px;
background: #fff;
}
.header-title {
font-size: 16px;
font-weight: 600;
color: #1f2937;
max-width: 40vw;
}
.spacer {
flex: 1 1 auto;
}
.content {
height: calc(100vh - 56px);
background: #f0f2f5;
}
.fade-enter-active,
.fade-leave-active {
transition: opacity 0.15s ease;
}
.fade-enter-from,
.fade-leave-to {
opacity: 0;
}
</style>