Files
datahub/frontend/src/components/layouts/MainLayout.vue
T

251 lines
7.4 KiB
Vue
Raw Normal View History

2026-03-18 13:53:36 +08:00
<script setup lang="ts">
import Brand from '@/components/Brand.vue'
import { useUserStore } from '@/stores/user'
2026-03-18 13:53:36 +08:00
import {
MenuFoldOutlined,
MenuUnfoldOutlined,
DashboardOutlined,
UserOutlined,
ShoppingOutlined,
FileTextOutlined,
UnorderedListOutlined,
DollarOutlined,
MonitorOutlined,
2026-03-20 14:01:38 +08:00
WarningOutlined,
2026-03-18 13:53:36 +08:00
LogoutOutlined,
SettingOutlined,
KeyOutlined,
2026-03-19 10:50:19 +08:00
TeamOutlined,
ApartmentOutlined,
2026-03-20 15:29:52 +08:00
FileSearchOutlined,
ApiOutlined,
HistoryOutlined,
2026-03-18 13:53:36 +08:00
} from '@ant-design/icons-vue'
import type { Component } from 'vue'
interface MenuItem {
key: string
icon: Component
label: string
2026-03-19 08:44:32 +08:00
adminOnly?: boolean
2026-03-18 13:53:36 +08:00
children?: MenuItem[]
}
const router = useRouter()
const route = useRoute()
const userStore = useUserStore()
2026-03-18 13:53:36 +08:00
// 侧边栏折叠状态,持久化到 localStorage
const collapsed = ref(localStorage.getItem('sidebarCollapsed') === 'true')
watch(collapsed, (val) => {
localStorage.setItem('sidebarCollapsed', String(val))
})
// 菜单选中项与路由同步
const selectedKeys = computed(() => [route.path])
// 子菜单展开状态
const openKeys = ref<string[]>([])
const initOpenKeys = () => {
if (collapsed.value) return
const path = route.path
if (path.startsWith('/order')) openKeys.value = ['orders-group']
else if (path.startsWith('/refund')) openKeys.value = ['refunds-group']
2026-03-20 15:29:52 +08:00
else if (path.startsWith('/logs')) openKeys.value = ['logs-group']
2026-03-18 13:53:36 +08:00
}
onMounted(initOpenKeys)
watch(() => route.path, initOpenKeys)
// 导航菜单配置
const menuItems: MenuItem[] = [
{ key: '/', icon: DashboardOutlined, label: '首页' },
2026-03-19 08:44:32 +08:00
{ key: '/users', icon: UserOutlined, label: '用户管理', adminOnly: true },
2026-03-18 13:53:36 +08:00
{ key: '/products', icon: ShoppingOutlined, label: '产品管理' },
{
key: 'orders-group',
icon: FileTextOutlined,
label: '订单管理',
children: [
{ key: '/orders', icon: FileTextOutlined, label: '订单列表' },
{ key: '/order-items', icon: UnorderedListOutlined, label: '订单子项' },
],
},
{
key: 'refunds-group',
icon: DollarOutlined,
label: '退款管理',
children: [
{ key: '/refunds', icon: DollarOutlined, label: '退款列表' },
{ key: '/refund-items', icon: UnorderedListOutlined, label: '退款子项' },
],
},
2026-03-19 10:50:19 +08:00
{ key: '/roles', icon: TeamOutlined, label: '角色管理', adminOnly: true },
{ key: '/route-groups', icon: ApartmentOutlined, label: '路由组管理', adminOnly: true },
2026-03-19 08:44:32 +08:00
{ key: '/mq-status', icon: MonitorOutlined, label: '队列监控', adminOnly: true },
2026-03-20 14:01:38 +08:00
{ key: '/failed-messages', icon: WarningOutlined, label: '失败消息', adminOnly: true },
2026-03-20 15:29:52 +08:00
{
key: 'logs-group',
icon: FileSearchOutlined,
label: '系统日志',
adminOnly: true,
children: [
{ key: '/logs/requests', icon: ApiOutlined, label: '请求日志' },
{ key: '/logs/operations', icon: HistoryOutlined, label: '操作日志' },
],
},
2026-03-18 13:53:36 +08:00
]
2026-03-19 08:44:32 +08:00
const filteredMenuItems = computed(() =>
menuItems.filter((item) => !item.adminOnly || userStore.isAdmin),
)
const username = computed(() => userStore.username || 'admin')
2026-03-18 13:53:36 +08:00
2026-03-19 08:44:32 +08:00
const handleMenuClick = ({ key }: { key: string | number }) => {
const path = String(key)
if (path.startsWith('/')) {
router.push(path)
2026-03-18 13:53:36 +08:00
}
}
const handleLogout = () => {
userStore.logout()
2026-03-18 13:53:36 +08:00
router.push('/login')
}
// 面包屑
const breadcrumbItems = computed(() => {
const pathMap: Record<string, string> = {
'/': '首页',
'/users': '用户管理',
'/products': '产品管理',
'/orders': '订单列表',
'/order-items': '订单子项',
'/refunds': '退款列表',
'/refund-items': '退款子项',
2026-03-19 10:50:19 +08:00
'/roles': '角色管理',
'/route-groups': '路由组管理',
2026-03-18 13:53:36 +08:00
'/mq-status': '队列监控',
2026-03-20 14:01:38 +08:00
'/failed-messages': '失败消息',
2026-03-20 15:29:52 +08:00
'/logs/requests': '请求日志',
'/logs/operations': '操作日志',
2026-03-18 18:04:41 +08:00
'/profile': '个人信息',
'/profile/password': '修改密码',
2026-03-18 13:53:36 +08:00
}
const items: Array<{ title: string; path: string }> = [{ title: '首页', path: '/' }]
if (route.path !== '/') {
items.push({ title: pathMap[route.path] || route.path, path: route.path })
}
return items
})
</script>
<template>
<a-layout class="min-h-screen">
<!-- Header -->
<a-layout-header
class="fixed top-0 w-full z-10 flex items-center justify-between px-4"
style="height: 64px; line-height: 64px"
>
<div class="flex items-center gap-4">
<Brand size="small" :show-app-name="!collapsed" />
<component
:is="collapsed ? MenuUnfoldOutlined : MenuFoldOutlined"
class="text-white text-lg cursor-pointer hover:text-[#4da30d]"
@click="collapsed = !collapsed"
/>
</div>
<a-dropdown>
<span class="cursor-pointer text-white/85 hover:text-white flex items-center gap-1">
<UserOutlined />
{{ username }}
</span>
<template #overlay>
<a-menu>
2026-03-18 18:04:41 +08:00
<a-menu-item key="profile" @click="router.push('/profile')">
2026-03-18 13:53:36 +08:00
<SettingOutlined class="mr-2" />
个人信息
</a-menu-item>
2026-03-18 18:04:41 +08:00
<a-menu-item key="password" @click="router.push('/profile/password')">
2026-03-18 13:53:36 +08:00
<KeyOutlined class="mr-2" />
修改密码
</a-menu-item>
<a-menu-divider />
<a-menu-item key="logout" @click="handleLogout">
<LogoutOutlined class="mr-2" />
退出登录
</a-menu-item>
</a-menu>
</template>
</a-dropdown>
</a-layout-header>
<a-layout class="mt-16">
<!-- Sidebar -->
<a-layout-sider
v-model:collapsed="collapsed"
:trigger="null"
collapsible
class="fixed left-0 top-16 bottom-0 overflow-y-auto"
>
<a-menu
:selected-keys="selectedKeys"
v-model:open-keys="openKeys"
mode="inline"
theme="dark"
@click="handleMenuClick"
>
2026-03-19 08:44:32 +08:00
<template v-for="item in filteredMenuItems" :key="item.key">
2026-03-18 13:53:36 +08:00
<a-sub-menu v-if="item.children" :key="item.key">
<template #icon><component :is="item.icon" /></template>
<template #title>{{ item.label }}</template>
<a-menu-item v-for="child in item.children" :key="child.key">
<template #icon><component :is="child.icon" /></template>
{{ child.label }}
</a-menu-item>
</a-sub-menu>
<a-menu-item v-else :key="item.key">
<template #icon><component :is="item.icon" /></template>
{{ item.label }}
</a-menu-item>
</template>
</a-menu>
</a-layout-sider>
<!-- Content + Footer -->
<a-layout
:style="{
marginLeft: collapsed ? '80px' : '200px',
transition: 'margin-left 0.2s',
}"
>
<a-layout-content class="p-6">
<a-breadcrumb class="mb-4">
<a-breadcrumb-item v-for="item in breadcrumbItems" :key="item.path">
<router-link v-if="item.path !== route.path" :to="item.path">
{{ item.title }}
</router-link>
<span v-else>{{ item.title }}</span>
</a-breadcrumb-item>
</a-breadcrumb>
<slot />
</a-layout-content>
<a-layout-footer class="text-center text-gray-500">
&copy; 2026 DataHub - 数据管理平台
</a-layout-footer>
</a-layout>
</a-layout>
</a-layout>
</template>
<style scoped>
:deep(.ant-layout-sider) {
z-index: 9;
}
</style>