Files
JayuCloud/JC-Salon-Frontend/src/views/admin/AdminLayout.vue
T

451 lines
12 KiB
Vue

<template>
<div
class="admin-layout"
:class="{
'admin-layout--mobile': isMobile,
'admin-layout--collapsed': collapsed && !isMobile
}"
>
<a-layout>
<a-layout-sider v-if="!isMobile" v-model:collapsed="collapsed" :trigger="null" collapsible class="sider" :width="224" :collapsed-width="74">
<div class="logo">
<span class="logo-mark">S</span>
<strong v-if="!collapsed">Salon</strong>
</div>
<div v-if="!collapsed" class="sider-section-label">工作台</div>
<a-menu :selectedKeys="selectedKeys" mode="inline" theme="dark" @click="handleMenuClick">
<a-menu-item v-for="item in menuItems" :key="item.key">
<template #icon><component :is="item.icon" /></template>
<span>{{ item.label }}</span>
</a-menu-item>
</a-menu>
</a-layout-sider>
<a-layout>
<a-layout-header class="header">
<div class="header-shell">
<div class="header-left">
<button v-if="!isMobile" class="trigger" type="button" :aria-label="collapsed ? '展开导航' : '收起导航'" @click="collapsed = !collapsed">
<MenuUnfoldOutlined v-if="collapsed" /><MenuFoldOutlined v-else />
</button>
<button v-else class="trigger" type="button" aria-label="打开导航" @click="mobileDrawerOpen = true"><MenuOutlined /></button>
<div class="header-title"><span>Salon 管理驾驶舱</span><small>平台经营数据与运营事项</small></div>
</div>
<a-dropdown>
<button class="user-dropdown" type="button">
<a-avatar :size="32" :src="userInfo?.avatar || DEFAULT_AVATAR" />
<span>{{ userInfo?.nickname || '平台管理员' }}</span><DownOutlined />
</button>
<template #overlay>
<a-menu>
<a-menu-item key="profile" @click="router.push('/admin/profile')">个人设置</a-menu-item>
<a-menu-divider />
<a-menu-item key="logout" @click="handleLogout">退出登录</a-menu-item>
</a-menu>
</template>
</a-dropdown>
</div>
</a-layout-header>
<a-layout-content class="content"><router-view /></a-layout-content>
</a-layout>
</a-layout>
<a-drawer v-if="isMobile" v-model:open="mobileDrawerOpen" placement="left" :width="300" :closable="false" class="mobile-drawer" root-class-name="admin-mobile-drawer">
<div class="drawer-brand">
<span class="logo-mark">S</span>
<div class="drawer-brand__copy">
<strong>Salon</strong>
<span>平台管理后台</span>
</div>
</div>
<div class="drawer-section-label">工作台</div>
<a-menu :selectedKeys="selectedKeys" mode="inline" theme="dark" @click="openMobileMenu">
<a-menu-item v-for="item in menuItems" :key="item.key"><template #icon><component :is="item.icon" /></template>{{ item.label }}</a-menu-item>
</a-menu>
<div class="drawer-footer">
<a-avatar :size="34" :src="userInfo?.avatar || DEFAULT_AVATAR" />
<span class="drawer-footer__name">{{ userInfo?.nickname || '平台管理员' }}</span>
<a-button class="drawer-logout" aria-label="退出登录" title="退出登录" @click="handleLogout">
<LogoutOutlined />
</a-button>
</div>
</a-drawer>
</div>
</template>
<script setup lang="ts">
import { onMounted, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { message, Modal } from 'ant-design-vue'
import { DownOutlined, HomeOutlined, LogoutOutlined, MenuFoldOutlined, MenuOutlined, MenuUnfoldOutlined, SafetyCertificateOutlined, TeamOutlined, ToolOutlined, UserOutlined } from '@ant-design/icons-vue'
import type { Component } from 'vue'
import { adminNavigationItems, type AdminNavIconKey } from '@/config/admin-navigation'
import { DEFAULT_AVATAR } from '@/constants'
import { completeAdminLogout } from '@/utils/admin-logout'
import { runWithGlobalLoading } from '@/utils/action-loading'
import { useAdminViewport } from '@/composables/useAdminViewport'
const router = useRouter()
const route = useRoute()
const { isMobile } = useAdminViewport()
const collapsed = ref(false)
const mobileDrawerOpen = ref(false)
const selectedKeys = ref<string[]>([route.path])
const userInfo = ref<any>(null)
const iconMap: Record<AdminNavIconKey, Component> = { home: HomeOutlined, user: UserOutlined, shop: HomeOutlined, security: SafetyCertificateOutlined, tool: ToolOutlined, team: TeamOutlined, dashboard: HomeOutlined, gift: HomeOutlined, message: HomeOutlined, tag: HomeOutlined, shopping: HomeOutlined, calendar: HomeOutlined }
const menuItems = adminNavigationItems.map(item => ({ key: item.path, label: item.label, icon: iconMap[item.iconKey] }))
const handleMenuClick = ({ key }: { key: string | number }) => router.push(String(key))
const openMobileMenu = ({ key }: { key: string | number }) => { mobileDrawerOpen.value = false; router.push(String(key)) }
const handleLogout = () => Modal.confirm({ title: '确认退出登录', content: '退出后需要重新登录后台。', okText: '确认', cancelText: '取消', onOk: async () => { await runWithGlobalLoading(() => completeAdminLogout(router)); message.success('已退出登录') } })
watch(() => route.path, path => { selectedKeys.value = [path] }, { immediate: true })
onMounted(() => {
try { userInfo.value = JSON.parse(localStorage.getItem('adminUserInfo') || 'null') } catch { userInfo.value = null }
})
</script>
<style scoped>
.admin-layout {
min-height: 100vh;
--admin-sider-width: 224px;
background: var(--theme-page-bg);
}
.admin-layout--collapsed {
--admin-sider-width: 74px;
}
.admin-layout--mobile {
--admin-sider-width: 0px;
width: 100%;
max-width: 100vw;
overflow-x: hidden;
}
.sider {
position: fixed;
inset: 0 auto 0 0;
z-index: 1000;
height: 100vh;
overflow-x: hidden;
overflow-y: auto;
background: linear-gradient(
165deg,
var(--admin-mobile-shell-start) 0%,
var(--admin-mobile-shell-mid) 56%,
var(--admin-mobile-shell-end) 100%
);
transition: width 0.22s ease;
}
.logo,
.drawer-brand {
display: flex;
align-items: center;
justify-content: center;
gap: 10px;
height: 68px;
padding: 0 14px;
color: #fff;
}
.logo strong,
.drawer-brand__copy strong {
color: #fff;
font-size: 18px;
font-weight: 760;
letter-spacing: 0.02em;
}
.logo-mark {
display: inline-grid;
flex: 0 0 34px;
place-items: center;
width: 34px;
height: 34px;
border: 1px solid rgba(255, 255, 255, 0.55);
border-radius: 12px;
background: #d7f5e9;
color: #1e5b4e;
font-family: Georgia, serif;
font-size: 21px;
font-weight: 800;
box-shadow: 0 8px 20px rgba(16, 55, 46, 0.16);
}
.sider-section-label,
.drawer-section-label {
margin: 0 22px 8px;
color: rgba(255, 255, 255, 0.92);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.sider :deep(.ant-menu),
.sider :deep(.ant-menu-dark),
.mobile-drawer :deep(.ant-menu),
.mobile-drawer :deep(.ant-menu-dark) {
background: transparent;
}
.sider :deep(.ant-menu-item),
.mobile-drawer :deep(.ant-menu-item) {
width: calc(100% - 24px);
height: 42px;
margin: 5px 12px;
padding-inline: 12px;
border-radius: 13px;
color: #f4f8f7;
line-height: 42px;
}
.sider :deep(.ant-menu-item .ant-menu-item-icon),
.mobile-drawer :deep(.ant-menu-item .ant-menu-item-icon) {
color: currentColor;
font-size: 16px;
}
.sider :deep(.ant-menu-item::after),
.mobile-drawer :deep(.ant-menu-item::after) {
display: none;
}
.sider :deep(.ant-menu-dark .ant-menu-item:hover),
.mobile-drawer :deep(.ant-menu-dark .ant-menu-item:hover) {
color: #fff;
background: rgba(255, 255, 255, 0.12);
}
.sider :deep(.ant-menu-dark .ant-menu-item-selected),
.mobile-drawer :deep(.ant-menu-dark .ant-menu-item-selected) {
color: #185448;
background: #d9f4eb;
font-weight: 700;
box-shadow: 0 8px 18px rgba(17, 74, 61, 0.12);
}
.sider :deep(.ant-menu-dark .ant-menu-item-selected:hover),
.mobile-drawer :deep(.ant-menu-dark .ant-menu-item-selected:hover) {
color: #185448;
background: #e5f8f1;
}
.admin-layout--collapsed .logo {
padding-inline: 0;
}
.admin-layout--collapsed .sider :deep(.ant-menu-item) {
width: 50px;
padding-inline: 0;
text-align: center;
}
.header {
position: sticky;
top: 0;
z-index: 100;
height: 64px;
margin-left: var(--admin-sider-width);
padding: 0 24px;
border-bottom: 1px solid var(--admin-mobile-border);
background: var(--theme-card-bg);
transition: margin-left 0.22s ease;
}
.header-shell {
display: flex;
align-items: center;
justify-content: space-between;
min-height: 64px;
gap: 20px;
line-height: normal;
}
.header-left {
display: flex;
align-items: center;
gap: 14px;
min-width: 0;
}
.trigger {
display: grid;
flex: 0 0 36px;
place-items: center;
width: 36px;
height: 36px;
border: 0;
border-radius: 10px;
background: transparent;
color: #54736e;
cursor: pointer;
font-size: 18px;
}
.trigger:hover {
background: var(--admin-mobile-accent-soft);
color: var(--theme-primary-action);
}
.trigger:focus-visible,
.user-dropdown:focus-visible {
outline: 2px solid var(--theme-primary-action);
outline-offset: 2px;
}
.header-title {
display: grid;
gap: 3px;
color: #224d47;
}
.header-title span {
font-size: 15px;
font-weight: 750;
}
.header-title small {
color: #78918b;
font-size: 11px;
}
.user-dropdown {
display: flex;
align-items: center;
gap: 9px;
min-height: 40px;
padding: 4px 8px;
border: 0;
border-radius: 10px;
background: transparent;
color: #315650;
cursor: pointer;
font-weight: 700;
}
.user-dropdown:hover {
background: var(--admin-mobile-accent-soft);
}
.content {
min-height: calc(100vh - 64px);
margin-left: var(--admin-sider-width);
padding: 30px clamp(20px, 4vw, 54px);
transition: margin-left 0.22s ease;
}
.content :deep(.ant-table) {
table-layout: auto;
width: max-content;
min-width: 100%;
}
.content :deep(.ant-table-cell) {
word-break: keep-all;
}
.drawer-brand {
justify-content: flex-start;
height: 74px;
padding-inline: 22px;
}
.drawer-brand__copy {
display: grid;
gap: 3px;
}
.drawer-brand__copy span {
color: rgba(255, 255, 255, 0.92);
font-size: 11px;
}
:global(.admin-mobile-drawer .ant-drawer-body) {
display: flex;
flex-direction: column;
min-height: 100%;
padding: 0 0 16px;
background: linear-gradient(
165deg,
var(--admin-mobile-shell-start) 0%,
var(--admin-mobile-shell-mid) 56%,
var(--admin-mobile-shell-end) 100%
);
}
:global(.admin-mobile-drawer .drawer-section-label) {
margin-inline: 22px;
}
.drawer-footer {
display: flex;
align-items: center;
gap: 10px;
margin: auto 14px 0;
padding: 14px 8px 0;
border-top: 1px solid rgba(255, 255, 255, 0.2);
}
.drawer-footer__name {
flex: 1;
min-width: 0;
overflow: hidden;
color: #fff;
font-size: 13px;
font-weight: 650;
text-overflow: ellipsis;
white-space: nowrap;
}
.drawer-logout {
display: grid;
flex: 0 0 36px;
place-items: center;
width: 36px;
height: 36px;
padding: 0;
border-color: rgba(255, 255, 255, 0.28);
background: transparent;
color: #fff;
}
.drawer-logout:hover,
.drawer-logout:focus-visible {
border-color: rgba(255, 255, 255, 0.6);
background: rgba(255, 255, 255, 0.12);
color: #fff;
}
@media (max-width: 960px) {
.header,
.content {
margin-left: 0;
}
}
@media (max-width: 760px) {
.header {
padding: 0 16px;
}
.header-title small {
display: none;
}
.content {
padding: 18px 14px 28px;
}
}
@media (prefers-reduced-motion: reduce) {
.sider,
.header,
.content {
transition: none;
}
}
</style>