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

292 lines
6.2 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>
</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
v-for="item in menuItems"
: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>
<a-layout>
<a-layout-header class="header">
<div class="header-left">
<MenuUnfoldOutlined
v-if="collapsed"
class="trigger"
@click="toggleCollapsed"
/>
<MenuFoldOutlined
v-else
class="trigger"
@click="toggleCollapsed"
/>
</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="profile" @click="goToProfile">
<template #icon>
<UserOutlined />
</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>
</a-layout-header>
<a-layout-content class="content">
<router-view />
</a-layout-content>
</a-layout>
</a-layout>
</div>
</template>
<script setup lang="ts">
import { ref, computed, onMounted, watch } from 'vue'
2026-02-25 10:37:16 +08:00
import { useRouter, useRoute } from 'vue-router'
import {
MenuFoldOutlined,
MenuUnfoldOutlined,
DashboardOutlined,
UserOutlined,
ShopOutlined,
HomeOutlined,
SafetyCertificateOutlined,
DownOutlined,
LogoutOutlined
} from '@ant-design/icons-vue'
import { message, Modal } from 'ant-design-vue'
import { userApi, type UserVO } from '@/api/user'
import { authApi } from '@/api/auth'
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 { loadUserPermissions, hasPermission, clearPermissions } = usePermission()
2026-02-25 10:37:16 +08:00
const menuItems = [
{
key: '/admin/dashboard',
icon: DashboardOutlined,
label: '仪表盘'
},
{
key: '/admin/users',
icon: UserOutlined,
label: '用户管理'
},
{
key: '/admin/products',
icon: ShopOutlined,
label: '商品管理'
},
{
key: '/admin/shops',
icon: HomeOutlined,
label: '店铺管理'
},
{
key: '/admin/permissions',
icon: SafetyCertificateOutlined,
label: '权限管理'
}
]
const selectedKeys = ref<string[]>([route.path])
2026-02-25 10:37:16 +08:00
const toggleCollapsed = () => {
collapsed.value = !collapsed.value
}
const handleMenuClick = ({ key }: { key: string }) => {
router.push(key)
}
const goToProfile = () => {
router.push('/profile')
}
const handleLogout = () => {
Modal.confirm({
title: '确认退出',
content: '确定要退出登录吗?',
onOk: async () => {
try {
await authApi.logout()
} catch (error) {
console.error('登出失败:', error)
} finally {
localStorage.removeItem('accessToken')
localStorage.removeItem('refreshToken')
localStorage.removeItem('adminAccessToken')
2026-02-25 10:37:16 +08:00
sessionStorage.removeItem('accessToken')
sessionStorage.removeItem('adminAccessToken')
clearPermissions()
2026-02-25 10:37:16 +08:00
message.success('已退出登录')
router.push('/auth')
}
}
})
}
const fetchUserInfo = async () => {
try {
const res = await userApi.getUserInfo()
userInfo.value = res as unknown as UserVO
} catch (error) {
console.error('获取用户信息失败:', error)
}
}
watch(() => route.path, (newPath) => {
selectedKeys.value = [newPath]
}, { immediate: true })
onMounted(async () => {
await Promise.all([
fetchUserInfo(),
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.1);
}
.logo h1 {
color: white;
font-size: 18px;
margin: 0;
font-weight: 600;
}
:deep(.ant-menu) {
border-right: none;
}
.header {
background: white;
padding: 0 24px;
display: flex;
justify-content: space-between;
align-items: center;
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
position: sticky;
top: 0;
z-index: 100;
}
.header-left {
display: flex;
align-items: center;
}
.trigger {
font-size: 18px;
cursor: pointer;
transition: color 0.3s;
}
.trigger:hover {
color: #1890ff;
}
.header-right {
display: flex;
align-items: center;
}
.user-dropdown {
display: flex;
align-items: center;
gap: 12px;
cursor: pointer;
padding: 8px 12px;
border-radius: 8px;
transition: background 0.3s;
}
.user-dropdown:hover {
background: #f5f5f5;
}
.username {
font-size: 14px;
color: #333;
}
.content {
margin-left: 240px;
padding: 24px;
background: #f0f2f5;
min-height: calc(100vh - 64px);
transition: margin-left 0.2s;
}
.admin-layout :deep(.ant-layout-sider-collapsed) + .ant-layout .content {
margin-left: 80px;
}
</style>