Files
schoolNews/schoolNewsWeb/src/layouts/SidebarLayout.vue

244 lines
5.0 KiB
Vue
Raw Normal View History

2025-10-27 16:21:00 +08:00
<template>
<div class="sidebar-layout">
<!-- 顶部导航栏 -->
<TopNavigation />
<!-- 主内容区域 -->
<div class="layout-content">
<!-- 侧边栏和内容 -->
<div class="content-wrapper" v-if="hasSidebarMenus">
<!-- 侧边栏 -->
<aside class="sidebar" :class="{ collapsed: sidebarCollapsed }">
<div class="sidebar-toggle-btn" @click="toggleSidebar">
<i class="toggle-icon">{{ sidebarCollapsed ? '▶' : '◀' }}</i>
</div>
<nav class="sidebar-nav">
<MenuSidebar
:menus="sidebarMenus"
:collapsed="sidebarCollapsed"
@menu-click="handleMenuClick"
/>
</nav>
</aside>
<!-- 页面内容 -->
<main class="main-content">
<router-view />
</main>
</div>
<!-- 没有侧边栏时直接显示内容 -->
<div class="content-wrapper-full" v-else>
<main class="main-content-full">
<router-view />
</main>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, computed } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { useStore } from 'vuex';
import type { SysMenu } from '@/types';
import { MenuType } from '@/types/enums';
import { TopNavigation, MenuSidebar } from '@/components';
const route = useRoute();
const router = useRouter();
const store = useStore();
const sidebarCollapsed = ref(false);
// 获取所有菜单
const allMenus = computed(() => store.getters['auth/menuTree']);
// 获取所有顶层SIDEBAR菜单作为侧边栏
const sidebarMenus = computed(() => {
// 严格过滤type=SIDEBAR 且 layout='SidebarLayout' 的顶层菜单
return allMenus.value.filter((menu: SysMenu) =>
menu.type === MenuType.SIDEBAR &&
!menu.parentID &&
(menu as any).layout === 'SidebarLayout'
);
});
// 是否有侧边栏菜单
const hasSidebarMenus = computed(() => sidebarMenus.value.length > 0);
// 切换侧边栏
function toggleSidebar() {
sidebarCollapsed.value = !sidebarCollapsed.value;
localStorage.setItem('sidebarCollapsed', String(sidebarCollapsed.value));
}
// 处理菜单点击
function handleMenuClick(menu: SysMenu) {
if (menu.url && menu.url !== route.path) {
router.push(menu.url);
}
}
// 恢复侧边栏状态
const savedState = localStorage.getItem('sidebarCollapsed');
if (savedState !== null) {
sidebarCollapsed.value = savedState === 'true';
}
</script>
<style lang="scss" scoped>
.sidebar-layout {
display: flex;
flex-direction: column;
background: #f0f2f5;
}
.layout-content {
flex: 1;
display: flex;
flex-direction: column;
max-height: calc(100vh - 76px);
overflow-y: auto;
}
.content-wrapper {
flex: 1;
display: flex;
margin: 16px;
gap: 16px;
}
.sidebar {
width: 260px;
background: #001529;
border-radius: 4px;
box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
transition: width 0.3s ease;
flex-shrink: 0;
display: flex;
flex-direction: column;
position: relative;
max-height: calc(100vh - 108px); // 顶部导航76px + 上下边距32px
overflow: hidden;
&.collapsed {
width: 80px;
}
}
.sidebar-toggle-btn {
position: absolute;
top: 50%;
right: -12px;
width: 24px;
height: 48px;
background: white;
border: 1px solid #e8e8e8;
border-radius: 0 12px 12px 0;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
z-index: 10;
transition: all 0.3s;
&:hover {
background: #f0f2f5;
}
.toggle-icon {
font-size: 12px;
color: #666;
}
}
.sidebar-nav {
flex: 1;
overflow-y: auto;
padding: 16px 0;
// 美化滚动条
&::-webkit-scrollbar {
width: 6px;
}
&::-webkit-scrollbar-track {
background: rgba(255, 255, 255, 0.05);
border-radius: 3px;
}
&::-webkit-scrollbar-thumb {
background: rgba(255, 255, 255, 0.2);
border-radius: 3px;
&:hover {
background: rgba(255, 255, 255, 0.3);
}
}
}
.main-content {
flex: 1;
background: white;
border-radius: 4px;
box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
overflow-y: auto;
min-width: 0;
height: calc(100vh - 108px); // 固定高度:视口高度 - 顶部导航76px - 上下边距32px
max-height: calc(100vh - 108px);
// 美化滚动条
&::-webkit-scrollbar {
width: 8px;
}
&::-webkit-scrollbar-track {
background: #f1f1f1;
border-radius: 4px;
}
&::-webkit-scrollbar-thumb {
background: #c1c1c1;
border-radius: 4px;
&:hover {
background: #a8a8a8;
}
}
}
.content-wrapper-full {
flex: 1;
}
.main-content-full {
background: white;
border-radius: 4px;
box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
height: calc(100vh - 108px);
overflow-y: auto;
// 美化滚动条
&::-webkit-scrollbar {
width: 8px;
}
&::-webkit-scrollbar-track {
background: #f1f1f1;
border-radius: 4px;
}
&::-webkit-scrollbar-thumb {
background: #c1c1c1;
border-radius: 4px;
&:hover {
background: #a8a8a8;
}
}
}
</style>