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

398 lines
11 KiB
Vue
Raw Normal View History

2026-02-25 10:37:16 +08:00
<template>
<div class="admin-layout">
<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>
2026-02-25 10:37:16 +08:00
</div>
<a-menu
2026-02-25 15:52:43 +08:00
:selectedKeys="selectedKeys"
2026-02-25 10:37:16 +08:00
mode="inline"
theme="dark"
@click="handleMenuClick"
>
<a-menu-item
2026-02-26 17:54:18 +08:00
v-for="item in filteredMenuItems"
:key="item.key"
>
2026-02-25 10:37:16 +08:00
<template #icon>
<component :is="item.icon" />
2026-02-25 10:37:16 +08:00
</template>
<span>{{ item.label }}</span>
2026-02-25 10:37:16 +08:00
</a-menu-item>
</a-menu>
</a-layout-sider>
2026-02-25 10:37:16 +08:00
<a-layout>
<a-layout-header class="header">
2026-03-24 09:50:55 +08:00
<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>
2026-03-24 09:50:55 +08:00
</div>
2026-03-24 09:50:55 +08:00
<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')">
2026-03-24 09:50:55 +08:00
<template #icon>
<UserOutlined />
</template>
工作台
2026-03-24 09:50:55 +08:00
</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>
2026-02-25 10:37:16 +08:00
</div>
</a-layout-header>
2026-02-25 10:37:16 +08:00
<a-layout-content class="content">
2026-03-24 09:50:55 +08:00
<div class="content-shell">
<router-view />
</div>
2026-02-25 10:37:16 +08:00
</a-layout-content>
</a-layout>
</a-layout>
</div>
</template>
<script setup lang="ts">
import { computed, onMounted, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
2026-02-25 10:37:16 +08:00
import {
DashboardOutlined,
DownOutlined,
GiftOutlined,
HomeOutlined,
LogoutOutlined,
2026-02-25 10:37:16 +08:00
MenuFoldOutlined,
MenuUnfoldOutlined,
SafetyCertificateOutlined,
ShopOutlined,
2026-03-04 15:42:43 +08:00
ShoppingOutlined,
TagOutlined,
TeamOutlined,
UserOutlined
2026-02-25 10:37:16 +08:00
} from '@ant-design/icons-vue'
import { message, Modal } from 'ant-design-vue'
import { adminAuthApi, type UserVO } from '@/api/admin'
2026-02-25 10:37:16 +08:00
import { DEFAULT_AVATAR } from '@/constants'
import { usePermission } from '@/utils/permission'
2026-02-25 10:37:16 +08:00
const router = useRouter()
const route = useRoute()
const collapsed = ref(false)
const userInfo = ref<UserVO | null>(null)
const selectedKeys = ref<string[]>([route.path])
const selectedShopId = ref<string>()
const shopSwitchLoading = ref(false)
2026-02-26 17:54:18 +08:00
const { loadUserPermissions, hasPermission, clearPermissions, userPermissions } = usePermission()
2026-02-25 10:37:16 +08:00
const menuItems = [
{ 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/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 isBootstrapAdmin = computed(() => userInfo.value?.type === 4 && !userInfo.value?.shopId)
const shopOptions = computed(() => {
const ids = userInfo.value?.shopIds || []
const names = userInfo.value?.shopNames || []
return ids.map((id, index) => ({
id: String(id),
name: names[index] || `门店 ${id}`
}))
})
const shopSelectOptions = computed(() => shopOptions.value.map(shop => ({ label: shop.name, value: shop.id })))
const currentShopName = computed(() => userInfo.value?.shopName || '未选择门店')
2026-02-25 10:37:16 +08:00
2026-02-26 17:54:18 +08:00
const filteredMenuItems = computed(() => {
userPermissions.value
let visibleItems = menuItems.filter(item => !item.permissionCode || hasPermission(item.permissionCode))
if (isBootstrapAdmin.value) {
visibleItems = visibleItems.filter(item => item.key === '/admin' || item.key === '/admin/shops')
}
return visibleItems
2026-02-26 17:54:18 +08:00
})
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)
}
2026-02-25 10:37:16 +08:00
const toggleCollapsed = () => {
collapsed.value = !collapsed.value
}
2026-03-10 23:23:15 +08:00
const handleMenuClick = (info: any) => {
router.push(info.key)
2026-02-25 10:37:16 +08:00
}
const handleLogout = () => {
Modal.confirm({
title: '确认退出登录',
content: '退出后需要重新登录后台。',
okText: '确认',
cancelText: '取消',
2026-02-25 10:37:16 +08:00
onOk: async () => {
try {
await adminAuthApi.logout()
2026-02-25 10:37:16 +08:00
} catch (error) {
console.error('退出失败:', error)
2026-02-25 10:37:16 +08:00
} finally {
localStorage.removeItem('adminAccessToken')
localStorage.removeItem('adminRefreshToken')
localStorage.removeItem('adminUserInfo')
2026-02-25 10:37:16 +08:00
localStorage.removeItem('accessToken')
localStorage.removeItem('refreshToken')
localStorage.removeItem('userInfo')
sessionStorage.removeItem('adminAccessToken')
sessionStorage.removeItem('accessToken')
clearPermissions()
2026-02-25 10:37:16 +08:00
message.success('已退出登录')
window.location.href = '/admin/login'
2026-02-25 10:37:16 +08:00
}
}
})
}
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
2026-02-25 10:37:16 +08:00
} catch (error) {
console.error('获取后台用户信息失败:', error)
2026-02-25 10:37:16 +08:00
}
}
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 {
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 = [newPath]
},
{ immediate: true }
)
onMounted(async () => {
2026-02-26 17:54:18 +08:00
await fetchUserInfo()
await loadUserPermissions()
2026-02-25 10:37:16 +08:00
})
</script>
<style scoped>
.admin-layout {
height: 100vh;
}
.sider {
background: #001529;
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, 255, 255, 0.08);
2026-02-25 10:37:16 +08:00
}
.logo h1 {
margin: 0;
2026-02-25 10:37:16 +08:00
color: white;
font-size: 18px;
font-weight: 700;
2026-02-25 10:37:16 +08:00
}
.header {
position: sticky;
top: 0;
z-index: 100;
padding: 0 24px;
background: white;
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
2026-02-25 10:37:16 +08:00
}
2026-03-24 09:50:55 +08:00
.header-shell {
width: 100%;
max-width: var(--admin-content-max-width);
margin: 0 auto;
min-height: 64px;
2026-03-24 09:50:55 +08:00
display: flex;
justify-content: space-between;
align-items: center;
gap: 16px;
2026-03-24 09:50:55 +08:00
}
2026-02-25 10:37:16 +08:00
.header-left {
display: flex;
align-items: center;
gap: 16px;
min-width: 0;
2026-02-25 10:37:16 +08:00
}
.trigger {
font-size: 18px;
cursor: pointer;
}
.shop-hint,
.shop-switcher,
.shop-pill {
display: inline-flex;
align-items: center;
gap: 10px;
min-height: 40px;
padding: 0 14px;
border-radius: 999px;
background: #f5f7fb;
color: #334155;
}
.shop-hint {
flex-wrap: wrap;
}
.shop-switcher-label {
font-size: 13px;
color: #64748b;
}
.shop-select {
min-width: 220px;
}
.shop-pill {
font-weight: 600;
2026-02-25 10:37:16 +08:00
}
.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;
2026-02-25 10:37:16 +08:00
}
.user-dropdown:hover {
background: #f8fafc;
2026-02-25 10:37:16 +08:00
}
.username {
color: #1f2937;
2026-02-25 10:37:16 +08:00
}
.content {
margin-left: 240px;
min-height: calc(100vh - 64px);
2026-02-25 10:37:16 +08:00
padding: 24px;
background: #f0f2f5;
transition: margin-left 0.2s;
}
2026-03-24 09:50:55 +08:00
.content-shell {
width: 100%;
max-width: var(--admin-content-max-width);
margin: 0 auto;
}
2026-02-25 10:37:16 +08:00
.admin-layout :deep(.ant-layout-sider-collapsed) + .ant-layout .content {
margin-left: 80px;
}
</style>