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

911 lines
24 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<div class="admin-layout" :class="{ 'admin-layout--mobile': isMobile }">
<template v-if="!isMobile">
<a-layout>
<a-layout-sider
v-model:collapsed="collapsed"
:trigger="null"
collapsible
class="sider"
width="240"
>
<div class="logo">
<h1 v-if="!collapsed">后台管理</h1>
<h1 v-else>后台</h1>
</div>
<a-menu
:selectedKeys="selectedKeys"
mode="inline"
theme="dark"
@click="handleMenuClick"
>
<a-menu-item
v-for="item in filteredMenuItems"
: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">
<MenuUnfoldOutlined v-if="collapsed" class="trigger" @click="toggleCollapsed" />
<MenuFoldOutlined v-else class="trigger" @click="toggleCollapsed" />
<div v-if="isBootstrapAdmin" class="shop-hint">
<span>当前暂无可用门店,请先前往店铺管理创建</span>
<a-button type="link" @click="router.push('/admin/shops')">去创建</a-button>
</div>
<div v-else-if="shopOptions.length > 1" class="shop-switcher">
<span class="shop-switcher-label">当前门店</span>
<a-select
v-model:value="selectedShopId"
class="shop-select"
:options="shopSelectOptions"
:loading="shopSwitchLoading"
@change="handleShopChange"
/>
</div>
<div v-else class="shop-pill">
<ShopOutlined />
<span>{{ currentShopName }}</span>
</div>
</div>
<div class="header-right">
<a-dropdown>
<a class="user-dropdown" @click.prevent>
<a-avatar :size="32" :src="userInfo?.avatar || DEFAULT_AVATAR" />
<span class="username">{{ userInfo?.nickname || '管理员' }}</span>
<DownOutlined />
</a>
<template #overlay>
<a-menu>
<a-menu-item key="home" @click="router.push('/admin')">
<template #icon>
<UserOutlined />
</template>
工作台
</a-menu-item>
<a-menu-item key="profile" @click="router.push('/admin/profile')">
<template #icon>
<IdcardOutlined />
</template>
我的
</a-menu-item>
<a-menu-divider />
<a-menu-item key="logout" @click="handleLogout">
<template #icon>
<LogoutOutlined />
</template>
退出登录
</a-menu-item>
</a-menu>
</template>
</a-dropdown>
</div>
</div>
</a-layout-header>
<a-layout-content class="content">
<div class="content-shell">
<router-view />
</div>
</a-layout-content>
</a-layout>
</a-layout>
</template>
<template v-else>
<div class="mobile-shell">
<header class="mobile-topbar">
<button class="mobile-icon-btn" type="button" @click="mobileDrawerOpen = true">
<MenuOutlined />
</button>
<div class="mobile-title-block">
<span class="mobile-eyebrow">{{ currentMobileSectionLabel }}</span>
<strong class="mobile-title">{{ currentMobileTitle }}</strong>
</div>
<button class="mobile-avatar-btn" type="button" @click="router.push('/admin/profile')">
<a-avatar :size="34" :src="userInfo?.avatar || DEFAULT_AVATAR" />
</button>
</header>
<section class="mobile-context">
<div class="mobile-context__primary">{{ currentShopName }}</div>
<div class="mobile-context__secondary">{{ roleLabel }}</div>
</section>
<main class="mobile-content">
<router-view />
</main>
<nav class="mobile-tabbar">
<button
v-for="tab in mobileTabs"
:key="tab.key"
class="mobile-tabbar__item"
:class="{ 'is-active': activeMobileTab === tab.key }"
type="button"
@click="handleMobileTabClick(tab.path)"
>
<component :is="tab.icon" />
<span>{{ tab.label }}</span>
</button>
</nav>
<a-drawer
v-model:open="mobileDrawerOpen"
placement="left"
:width="320"
class="mobile-drawer"
:closable="false"
root-class-name="admin-mobile-drawer"
>
<div class="drawer-account">
<a-avatar :size="46" :src="userInfo?.avatar || DEFAULT_AVATAR" />
<div class="drawer-account__body">
<div class="drawer-account__title">{{ userInfo?.nickname || userInfo?.account || '后台账号' }}</div>
<div class="drawer-account__meta">{{ roleLabel }}</div>
</div>
</div>
<div class="drawer-shop-card">
<span class="drawer-shop-card__label">当前门店</span>
<strong class="drawer-shop-card__value">{{ currentShopName }}</strong>
<p class="drawer-shop-card__desc">{{ shopScopeText }}</p>
<a-button type="default" block @click="openProfileFromDrawer">
{{ shopOptions.length > 1 ? '切换门店' : '查看账号信息' }}
</a-button>
</div>
<div class="drawer-section">
<div class="drawer-section__title">二级模块</div>
<button
v-for="item in filteredMenuItems"
:key="item.key"
class="drawer-link"
type="button"
@click="openPathFromDrawer(item.key)"
>
<div class="drawer-link__icon">
<component :is="item.icon" />
</div>
<div class="drawer-link__body">
<div class="drawer-link__title">{{ item.label }}</div>
</div>
<RightOutlined class="drawer-link__arrow" />
</button>
</div>
<div class="drawer-section">
<div class="drawer-section__title">会话操作</div>
<a-space direction="vertical" style="width: 100%">
<a-button block @click="openPathFromDrawer('/admin/profile')">打开我的</a-button>
<a-button block danger @click="handleLogout">退出当前账号</a-button>
</a-space>
</div>
</a-drawer>
</div>
</template>
</div>
</template>
<script setup lang="ts">
import { computed, onMounted, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import {
DashboardOutlined,
DownOutlined,
GiftOutlined,
HomeOutlined,
IdcardOutlined,
LogoutOutlined,
MessageOutlined,
MenuFoldOutlined,
MenuOutlined,
MenuUnfoldOutlined,
RightOutlined,
SafetyCertificateOutlined,
ShopOutlined,
ShoppingOutlined,
TagOutlined,
TeamOutlined,
UserOutlined
} from '@ant-design/icons-vue'
import { message, Modal } from 'ant-design-vue'
import { adminAuthApi, type UserVO } from '@/api/admin'
import { DEFAULT_AVATAR } from '@/constants'
import { usePermission } from '@/utils/permission'
import { runWithGlobalLoading } from '@/utils/action-loading'
import { useAdminViewport } from '@/composables/useAdminViewport'
import {
getAdminCurrentShopName,
getAdminRoleLabel,
getAdminShopOptions,
isBootstrapAdminUser
} from './admin-helpers'
type AdminMenuItem = {
key: string
icon: any
label: string
permissionCode: string
}
const router = useRouter()
const route = useRoute()
const { isMobile } = useAdminViewport()
const collapsed = ref(false)
const mobileDrawerOpen = ref(false)
const userInfo = ref<UserVO | null>(null)
const selectedKeys = ref<string[]>([route.path])
const selectedShopId = ref<string>()
const shopSwitchLoading = ref(false)
const { loadUserPermissions, hasPermission, clearPermissions, userPermissions } = usePermission()
const menuItems: AdminMenuItem[] = [
{ key: '/admin', icon: HomeOutlined, label: '首页', permissionCode: '' },
{ key: '/admin/dashboard', icon: DashboardOutlined, label: '仪表盘', permissionCode: 'dashboard:view' },
{ key: '/admin/users', icon: UserOutlined, label: '用户管理', permissionCode: 'user:view' },
{ key: '/admin/products', icon: ShopOutlined, label: '作品管理', permissionCode: 'product:view' },
{ key: '/admin/coupons', icon: GiftOutlined, label: '优惠券管理', permissionCode: 'coupon:view' },
{ key: '/admin/comments', icon: MessageOutlined, label: '评论管理', permissionCode: 'comment:view' },
{ key: '/admin/shops', icon: HomeOutlined, label: '店铺管理', permissionCode: 'shop:view' },
{ key: '/admin/permissions', icon: SafetyCertificateOutlined, label: '权限管理', permissionCode: 'permission:view' },
{ key: '/admin/tags', icon: TagOutlined, label: '标签管理', permissionCode: 'tag:view' },
{ key: '/admin/orders', icon: ShoppingOutlined, label: '订单管理', permissionCode: 'order:view' },
{ key: '/admin/nail-artists', icon: TeamOutlined, label: '美甲师管理', permissionCode: 'nail_artist:view' }
]
const mobileTabs = [
{ key: 'workbench', label: '工作台', path: '/admin', icon: HomeOutlined },
{ key: 'orders', label: '订单', path: '/admin/orders', icon: ShoppingOutlined },
{ key: 'manage', label: '经营', path: '/admin/manage', icon: DashboardOutlined },
{ key: 'profile', label: '我的', path: '/admin/profile', icon: UserOutlined }
]
const isBootstrapAdmin = computed(() => isBootstrapAdminUser(userInfo.value))
const shopOptions = computed(() => getAdminShopOptions(userInfo.value))
const shopSelectOptions = computed(() => shopOptions.value.map(shop => ({ label: shop.name, value: shop.id })))
const currentShopName = computed(() => getAdminCurrentShopName(userInfo.value))
const roleLabel = computed(() => getAdminRoleLabel(userInfo.value?.type))
const shopScopeText = computed(() => {
if (!shopOptions.value.length) {
return '游客模式'
}
return `可登录门店:${shopOptions.value.map(shop => shop.name).join(' / ')}`
})
const filteredMenuItems = computed(() => {
userPermissions.value
let visibleItems = menuItems.filter(item => {
if (!item.permissionCode) {
return true
}
if (isBootstrapAdmin.value && item.key === '/admin/shops') {
return true
}
return hasPermission(item.permissionCode)
})
if (isBootstrapAdmin.value) {
visibleItems = visibleItems.filter(item => item.key === '/admin' || item.key === '/admin/shops')
}
return visibleItems
})
const activeMobileTab = computed(() => {
if (route.path === '/admin' || route.path === '/admin/dashboard') {
return 'workbench'
}
if (route.path.startsWith('/admin/orders')) {
return 'orders'
}
if (
route.path === '/admin/manage' ||
route.path.startsWith('/admin/users') ||
route.path.startsWith('/admin/products') ||
route.path.startsWith('/admin/coupons') ||
route.path.startsWith('/admin/comments') ||
route.path.startsWith('/admin/shops') ||
route.path.startsWith('/admin/permissions') ||
route.path.startsWith('/admin/tags') ||
route.path.startsWith('/admin/nail-artists')
) {
return 'manage'
}
if (route.path.startsWith('/admin/profile')) {
return 'profile'
}
return 'workbench'
})
const currentMobileSectionLabel = computed(() => {
switch (activeMobileTab.value) {
case 'orders':
return '订单'
case 'manage':
return '经营'
case 'profile':
return '我的'
default:
return '工作台'
}
})
const currentMobileTitle = computed(() => {
const currentMenu = menuItems.find(item => item.key === route.path)
if (currentMenu) {
return currentMenu.label
}
if (route.path === '/admin/manage') {
return '经营模块'
}
if (route.path === '/admin/profile') {
return '账号与门店'
}
return '后台管理'
})
const persistAdminSession = (payload: any) => {
localStorage.setItem('adminAccessToken', payload.accessToken)
localStorage.setItem('adminRefreshToken', payload.refreshToken)
localStorage.setItem('adminUserInfo', JSON.stringify(payload.userInfo))
sessionStorage.setItem('adminAccessToken', payload.accessToken)
}
const toggleCollapsed = () => {
collapsed.value = !collapsed.value
}
const handleMenuClick = (info: any) => {
router.push(info.key)
}
const openPathFromDrawer = (path: string) => {
mobileDrawerOpen.value = false
router.push(path)
}
const openProfileFromDrawer = () => {
openPathFromDrawer('/admin/profile')
}
const handleMobileTabClick = (path: string) => {
if (route.path !== path) {
router.push(path)
}
}
const handleLogout = () => {
Modal.confirm({
title: '确认退出登录',
content: '退出后需要重新登录后台。',
okText: '确认',
cancelText: '取消',
onOk: async () => {
await runWithGlobalLoading(async () => {
try {
await adminAuthApi.logout()
} catch (error) {
console.error('退出失败:', error)
} finally {
localStorage.removeItem('adminAccessToken')
localStorage.removeItem('adminRefreshToken')
localStorage.removeItem('adminUserInfo')
localStorage.removeItem('accessToken')
localStorage.removeItem('refreshToken')
localStorage.removeItem('userInfo')
sessionStorage.removeItem('adminAccessToken')
sessionStorage.removeItem('accessToken')
clearPermissions()
message.success('已退出登录')
window.location.href = '/admin/login'
}
})
}
})
}
const fetchUserInfo = async () => {
try {
const cache = localStorage.getItem('adminUserInfo')
if (cache) {
userInfo.value = JSON.parse(cache)
}
if (!userInfo.value) {
return
}
selectedShopId.value = userInfo.value.shopId ? String(userInfo.value.shopId) : undefined
} catch (error) {
console.error('获取后台用户信息失败:', error)
}
}
const handleShopChange = async (shopId: unknown) => {
if (typeof shopId !== 'string' && typeof shopId !== 'number') {
return
}
const normalizedShopId = String(shopId)
if (!normalizedShopId || normalizedShopId === String(userInfo.value?.shopId || '')) {
return
}
shopSwitchLoading.value = true
try {
await runWithGlobalLoading(async () => {
const response = await adminAuthApi.switchShop(normalizedShopId)
persistAdminSession(response as any)
await fetchUserInfo()
await loadUserPermissions()
})
message.success('门店上下文已切换')
window.location.reload()
} catch (error: any) {
console.error('切换门店失败:', error)
selectedShopId.value = userInfo.value?.shopId ? String(userInfo.value.shopId) : undefined
message.error(error?.message || '切换门店失败')
} finally {
shopSwitchLoading.value = false
}
}
watch(
() => route.path,
newPath => {
selectedKeys.value = menuItems.some(item => item.key === newPath) ? [newPath] : []
mobileDrawerOpen.value = false
},
{ immediate: true }
)
onMounted(async () => {
await fetchUserInfo()
if (isBootstrapAdmin.value) {
clearPermissions()
return
}
await loadUserPermissions()
})
</script>
<style scoped>
.admin-layout {
min-height: 100vh;
}
.sider {
background:
linear-gradient(
180deg,
var(--admin-mobile-shell-start) 0%,
var(--admin-mobile-shell-mid) 48%,
var(--admin-mobile-shell-end) 100%
);
overflow: auto;
height: 100vh;
position: fixed;
left: 0;
top: 0;
bottom: 0;
z-index: 1000;
}
.logo {
height: 64px;
display: flex;
align-items: center;
justify-content: center;
background: rgba(255, 248, 240, 0.14);
}
.logo h1 {
margin: 0;
color: white;
font-size: 18px;
font-weight: 700;
}
.header {
position: sticky;
top: 0;
z-index: 100;
padding: 0 24px;
background: rgba(255, 249, 242, 0.96);
box-shadow: 0 10px 24px rgba(145, 102, 82, 0.08);
backdrop-filter: blur(14px);
}
.header-shell {
width: 100%;
max-width: var(--admin-content-max-width);
margin: 0 auto;
min-height: 64px;
display: flex;
justify-content: space-between;
align-items: center;
gap: 16px;
}
.header-left {
display: flex;
align-items: center;
gap: 16px;
min-width: 0;
}
.trigger {
font-size: 18px;
cursor: pointer;
color: var(--admin-mobile-ink);
}
.shop-hint,
.shop-switcher,
.shop-pill {
display: inline-flex;
align-items: center;
gap: 10px;
min-height: 40px;
padding: 0 14px;
border-radius: 999px;
background: var(--admin-mobile-surface-alt);
color: var(--admin-mobile-ink);
}
.shop-hint {
flex-wrap: wrap;
}
.shop-switcher-label {
font-size: 13px;
color: var(--admin-mobile-muted);
}
.shop-select {
min-width: 220px;
}
.shop-pill {
font-weight: 600;
}
.header-right {
display: flex;
align-items: center;
}
.user-dropdown {
display: flex;
align-items: center;
gap: 12px;
padding: 8px 12px;
border-radius: 10px;
transition: background 0.2s ease;
}
.user-dropdown:hover {
background: rgba(204, 123, 95, 0.1);
}
.username {
color: var(--admin-mobile-ink);
}
.content {
margin-left: 240px;
min-height: calc(100vh - 64px);
padding: 24px;
background:
linear-gradient(180deg, #fbf6f0 0%, #f4e8dc 100%);
transition: margin-left 0.2s;
}
.content-shell {
width: 100%;
max-width: var(--admin-content-max-width);
margin: 0 auto;
}
.admin-layout :deep(.ant-layout-sider-collapsed) + .ant-layout .content {
margin-left: 80px;
}
.mobile-shell {
min-height: 100vh;
background:
linear-gradient(180deg, var(--admin-mobile-shell-mid) 0 132px, var(--admin-mobile-surface) 132px 100%);
}
.mobile-topbar {
position: sticky;
top: 0;
z-index: 120;
display: flex;
align-items: center;
gap: 12px;
padding: calc(14px + env(safe-area-inset-top)) 16px 10px;
background: var(--admin-mobile-shell-glass);
backdrop-filter: blur(16px);
}
.mobile-icon-btn,
.mobile-avatar-btn {
display: inline-flex;
align-items: center;
justify-content: center;
width: 40px;
height: 40px;
padding: 0;
border: 0;
border-radius: 14px;
background: var(--admin-mobile-shell-soft);
color: #fff8f1;
}
.mobile-title-block {
flex: 1;
min-width: 0;
display: grid;
gap: 4px;
}
.mobile-eyebrow {
font-size: 11px;
letter-spacing: 0.14em;
text-transform: uppercase;
color: rgba(247, 242, 235, 0.68);
}
.mobile-title {
color: #f7f2eb;
font-size: 18px;
line-height: 1.2;
}
.mobile-context {
padding: 0 16px 16px;
color: #fff8f1;
}
.mobile-context__primary {
font-size: 20px;
font-weight: 700;
line-height: 1.2;
}
.mobile-context__secondary {
margin-top: 4px;
color: rgba(255, 248, 241, 0.74);
font-size: 13px;
}
.mobile-content {
padding: 16px 16px calc(96px + env(safe-area-inset-bottom));
}
.mobile-tabbar {
position: fixed;
left: 12px;
right: 12px;
bottom: 12px;
z-index: 130;
display: grid;
grid-template-columns: repeat(4, minmax(0, 1fr));
gap: 8px;
padding: 10px;
background: rgba(150, 103, 82, 0.92);
border: 1px solid rgba(255, 248, 241, 0.16);
border-radius: 24px;
box-shadow: 0 16px 40px rgba(146, 103, 82, 0.24);
backdrop-filter: blur(18px);
}
.mobile-tabbar__item {
display: grid;
justify-items: center;
gap: 6px;
padding: 10px 4px;
border: 0;
border-radius: 18px;
background: transparent;
color: rgba(255, 248, 241, 0.68);
font-size: 12px;
}
.mobile-tabbar__item.is-active {
background: rgba(255, 248, 241, 0.16);
color: #fff7ef;
}
.mobile-tabbar__item :deep(.anticon) {
font-size: 18px;
}
.drawer-account {
display: flex;
align-items: center;
gap: 14px;
padding: 18px;
border-radius: 24px;
color: #fff8f1;
background:
radial-gradient(circle at top right, rgba(255, 244, 232, 0.2), transparent 42%),
radial-gradient(circle at left top, rgba(232, 195, 169, 0.22), transparent 44%),
linear-gradient(
160deg,
var(--admin-mobile-shell-start) 0%,
var(--admin-mobile-shell-mid) 58%,
var(--admin-mobile-shell-end) 100%
);
}
.drawer-account__body {
flex: 1;
min-width: 0;
}
.drawer-account__title {
font-size: 18px;
font-weight: 700;
}
.drawer-account__meta {
margin-top: 4px;
font-size: 13px;
color: rgba(255, 248, 241, 0.8);
}
.drawer-shop-card {
margin-top: 18px;
padding: 18px;
border-radius: 22px;
background: var(--admin-mobile-card);
border: 1px solid var(--admin-mobile-border);
box-shadow: 0 12px 24px rgba(144, 103, 84, 0.08);
}
.drawer-shop-card__label {
font-size: 12px;
color: var(--admin-mobile-muted);
}
.drawer-shop-card__value {
display: block;
margin-top: 6px;
color: var(--admin-mobile-ink);
font-size: 18px;
}
.drawer-shop-card__desc {
margin: 8px 0 14px;
color: var(--admin-mobile-muted);
line-height: 1.6;
}
.drawer-section {
margin-top: 20px;
}
.drawer-section__title {
margin-bottom: 12px;
font-size: 13px;
font-weight: 700;
color: #6f594e;
}
.drawer-link {
width: 100%;
display: flex;
align-items: center;
gap: 12px;
padding: 14px 0;
border: 0;
border-bottom: 1px solid var(--admin-mobile-border);
background: transparent;
text-align: left;
}
.drawer-link:last-child {
border-bottom: 0;
}
.drawer-link__icon {
display: inline-flex;
align-items: center;
justify-content: center;
width: 42px;
height: 42px;
border-radius: 14px;
background: var(--admin-mobile-accent-soft);
color: #9e5a44;
}
.drawer-link__body {
flex: 1;
min-width: 0;
}
.drawer-link__title {
font-size: 15px;
font-weight: 600;
color: var(--admin-mobile-ink);
}
.drawer-link__meta {
margin-top: 4px;
color: var(--admin-mobile-muted);
font-size: 12px;
line-height: 1.5;
}
.drawer-link__arrow {
color: #b39384;
}
.mobile-drawer :deep(.ant-drawer-body) {
padding: calc(18px + env(safe-area-inset-top)) 18px calc(24px + env(safe-area-inset-bottom));
background: var(--admin-mobile-surface);
}
.sider :deep(.ant-menu),
.sider :deep(.ant-menu-dark) {
background: transparent;
}
.sider :deep(.ant-menu-item) {
width: calc(100% - 24px);
margin: 6px 12px;
border-radius: 16px;
}
.sider :deep(.ant-menu-dark .ant-menu-item),
.sider :deep(.ant-menu-dark .ant-menu-submenu-title) {
color: rgba(255, 248, 241, 0.8);
}
.sider :deep(.ant-menu-dark .ant-menu-item:hover),
.sider :deep(.ant-menu-dark .ant-menu-submenu-title:hover) {
color: #fff8f1;
background: rgba(255, 248, 241, 0.12);
}
.sider :deep(.ant-menu-dark .ant-menu-item-selected) {
color: #fff8f1;
background: rgba(255, 248, 241, 0.18);
}
.admin-layout :deep(.ant-btn-primary) {
border-color: transparent;
background: linear-gradient(135deg, var(--admin-mobile-accent) 0%, var(--admin-mobile-accent-strong) 100%);
box-shadow: 0 10px 20px rgba(185, 100, 73, 0.18);
}
.admin-layout :deep(.ant-btn-primary:hover),
.admin-layout :deep(.ant-btn-primary:focus) {
border-color: transparent;
background: linear-gradient(135deg, #d4866c 0%, #c26e52 100%);
}
.admin-layout :deep(.ant-select:not(.ant-select-disabled):hover .ant-select-selector),
.admin-layout :deep(.ant-input:hover),
.admin-layout :deep(.ant-input-affix-wrapper:hover) {
border-color: rgba(204, 123, 95, 0.45);
}
.admin-layout :deep(.ant-select-focused .ant-select-selector),
.admin-layout :deep(.ant-input:focus),
.admin-layout :deep(.ant-input-affix-wrapper-focused) {
border-color: rgba(204, 123, 95, 0.6);
box-shadow: 0 0 0 2px rgba(204, 123, 95, 0.12);
}
</style>