Files
schoolNews/schoolNewsWeb/src/components/base/TopNavigation.vue

557 lines
12 KiB
Vue
Raw Normal View History

2025-10-08 14:11:54 +08:00
<template>
<nav class="top-navigation">
<div class="nav-container">
<!-- Logo区域 -->
<div class="nav-logo">
2025-10-17 16:12:29 +08:00
<img src="@/assets/imgs/logo-icon.svg" alt="Logo" class="logo-icon" />
2025-10-16 18:03:46 +08:00
<span class="logo-text">红色思政学习平台</span>
2025-10-08 14:11:54 +08:00
</div>
<!-- 导航菜单 -->
2025-10-17 16:12:29 +08:00
<div class="nav-menu" @wheel="handleWheel">
2025-10-08 14:11:54 +08:00
<div
v-for="menu in navigationMenus"
:key="menu.menuID"
class="nav-item"
:class="{ active: isActive(menu) }"
2025-10-16 18:03:46 +08:00
@mouseenter="(e) => handleMouseEnter(menu, e)"
2025-10-08 14:11:54 +08:00
@mouseleave="handleMouseLeave"
>
<div class="nav-link" @click="handleNavClick(menu)">
<span>{{ menu.name }}</span>
2025-10-16 18:03:46 +08:00
<img v-if="hasNavigationChildren(menu)" class="arrow-down" src="@/assets/imgs/arrow-down.svg" alt="arrow" />
2025-10-08 14:11:54 +08:00
</div>
<!-- 下拉菜单 -->
2025-10-17 12:05:04 +08:00
<Teleport to="body" v-if="hasNavigationChildren(menu)">
2025-10-08 14:11:54 +08:00
<div
2025-10-16 18:03:46 +08:00
class="dropdown-menu"
:class="{ show: activeDropdown === menu.menuID }"
:style="getDropdownPosition(menu)"
2025-10-17 12:05:04 +08:00
@mouseenter="() => { if (menu.menuID) activeDropdown = menu.menuID }"
2025-10-16 18:03:46 +08:00
@mouseleave="handleMouseLeave"
2025-10-08 14:11:54 +08:00
>
2025-10-16 18:03:46 +08:00
<div
v-for="child in getNavigationChildren(menu)"
:key="child.menuID"
class="dropdown-item"
:class="{ active: isActive(child) }"
@click="handleNavClick(child)"
>
<span>{{ child.name }}</span>
</div>
2025-10-08 14:11:54 +08:00
</div>
2025-10-16 18:03:46 +08:00
</Teleport>
2025-10-08 14:11:54 +08:00
</div>
</div>
<!-- 右侧用户区域 -->
<div class="nav-right">
2025-10-16 18:03:46 +08:00
<!-- 搜索框 -->
2025-10-20 18:28:38 +08:00
<Search @search="handleSearch" />
2025-10-08 14:11:54 +08:00
<UserDropdown :user="userInfo" @logout="handleLogout" />
</div>
</div>
</nav>
</template>
<script setup lang="ts">
import { ref, computed } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { useStore } from 'vuex';
import type { SysMenu } from '@/types';
import { MenuType } from '@/types/enums';
// @ts-ignore - Vue 3.5 组件导入兼容性
2025-10-20 18:28:38 +08:00
import {UserDropdown, Search} from '@/components/base';
2025-10-08 14:11:54 +08:00
const router = useRouter();
const route = useRoute();
const store = useStore();
const activeDropdown = ref<string | null>(null);
2025-10-16 18:03:46 +08:00
const searchKeyword = ref('');
const dropdownPositions = ref<Record<string, { left: number; top: number; width: number }>>({});
2025-10-08 14:11:54 +08:00
// 获取所有菜单
const allMenus = computed(() => store.getters['auth/menuTree']);
2025-10-23 18:57:31 +08:00
const userInfo = computed(() => store.getters['auth/user']);
2025-10-08 14:11:54 +08:00
2025-10-17 16:12:29 +08:00
// 获取第一层的导航菜单MenuType.NAVIGATION过滤掉用户相关菜单
2025-10-08 14:11:54 +08:00
const navigationMenus = computed(() => {
2025-10-17 16:12:29 +08:00
const menus = allMenus.value.filter((menu: SysMenu) => {
// 过滤掉"用户下拉菜单"容器这些显示在UserDropdown中
if (menu.menuID === 'menu_user_dropdown') {
return false;
}
return menu.type === MenuType.NAVIGATION;
});
2025-10-18 18:19:19 +08:00
// console.log('导航菜单数据:', menus);
// menus.forEach((menu: SysMenu) => {
// console.log(`菜单 ${menu.name}:`, {
// menuID: menu.menuID,
// parentID: menu.parentID,
// children: menu.children,
// childrenCount: menu.children?.length || 0
// });
// });
2025-10-16 18:03:46 +08:00
return menus;
2025-10-08 14:11:54 +08:00
});
// 检查菜单是否有导航类型的子菜单
function hasNavigationChildren(menu: SysMenu): boolean {
return !!(menu.children && menu.children.some(child => child.type === MenuType.NAVIGATION));
}
// 获取导航类型的子菜单
function getNavigationChildren(menu: SysMenu): SysMenu[] {
2025-10-16 18:03:46 +08:00
if (!menu.children) {
2025-10-18 18:19:19 +08:00
// console.log(`菜单 ${menu.name} 没有子菜单`);
2025-10-16 18:03:46 +08:00
return [];
}
const children = menu.children.filter(child => child.type === MenuType.NAVIGATION);
2025-10-18 18:19:19 +08:00
// console.log(`菜单 ${menu.name} 的子菜单:`, children);
2025-10-16 18:03:46 +08:00
return children;
2025-10-08 14:11:54 +08:00
}
// 判断菜单是否激活
function isActive(menu: SysMenu): boolean {
if (!menu.url) return false;
// 精确匹配
if (route.path === menu.url) return true;
// 检查是否是子路由
if (menu.children && menu.children.length > 0) {
return isMenuOrChildActive(menu);
}
return false;
}
// 递归检查菜单或其子菜单是否激活
function isMenuOrChildActive(menu: SysMenu): boolean {
if (route.path === menu.url) return true;
if (menu.children) {
return menu.children.some(child => isMenuOrChildActive(child));
}
return false;
}
// 处理鼠标进入
2025-10-16 18:03:46 +08:00
function handleMouseEnter(menu: SysMenu, event?: MouseEvent) {
2025-10-08 14:11:54 +08:00
if (hasNavigationChildren(menu)) {
activeDropdown.value = menu.menuID || null;
2025-10-16 18:03:46 +08:00
// 计算下拉菜单位置
2025-10-17 12:05:04 +08:00
if (event && menu.menuID) {
const target = event.currentTarget as HTMLElement;
const navLink = target.querySelector('.nav-link') as HTMLElement;
const rect = (navLink || target).getBoundingClientRect();
2025-10-16 18:03:46 +08:00
dropdownPositions.value[menu.menuID] = {
left: rect.left,
top: rect.bottom,
width: rect.width
};
}
2025-10-08 14:11:54 +08:00
}
}
2025-10-16 18:03:46 +08:00
// 获取下拉菜单位置样式
function getDropdownPosition(menu: SysMenu) {
const menuID = menu.menuID;
2025-10-17 12:05:04 +08:00
const pos = menuID && dropdownPositions.value[menuID];
if (!pos) {
return {
position: 'fixed' as const,
visibility: 'hidden' as const
};
2025-10-16 18:03:46 +08:00
}
return {
position: 'fixed' as const,
left: `${pos.left}px`,
top: `${pos.top}px`,
width: `${pos.width}px`
};
}
2025-10-08 14:11:54 +08:00
// 处理鼠标离开
function handleMouseLeave() {
activeDropdown.value = null;
}
2025-10-17 16:12:29 +08:00
// 处理鼠标滚轮水平滚动
function handleWheel(event: WheelEvent) {
const container = event.currentTarget as HTMLElement;
if (container) {
event.preventDefault();
container.scrollLeft += event.deltaY;
}
}
2025-10-08 14:11:54 +08:00
// 处理导航点击
function handleNavClick(menu: SysMenu) {
activeDropdown.value = null;
if (menu.url) {
router.push(menu.url);
} else if (menu.children && menu.children.length > 0) {
// 如果没有url但有子菜单跳转到第一个子菜单
const firstChild = menu.children.find(child => child.url);
if (firstChild?.url) {
router.push(firstChild.url);
}
}
}
2025-10-16 18:03:46 +08:00
// 处理搜索
function handleSearch() {
if (searchKeyword.value.trim()) {
// 这里可以跳转到搜索页面或触发搜索功能
router.push(`/search?keyword=${encodeURIComponent(searchKeyword.value.trim())}`);
}
}
2025-10-08 14:11:54 +08:00
// 处理登出
function handleLogout() {
store.dispatch('auth/logout');
}
</script>
<style lang="scss" scoped>
.top-navigation {
2025-10-16 18:03:46 +08:00
height: 76px;
background: #ffffff;
border-bottom: 1px solid rgba(72, 72, 72, 0.1);
2025-10-08 14:11:54 +08:00
position: sticky;
top: 0;
z-index: 1000;
2025-10-17 16:12:29 +08:00
overflow: hidden;
2025-10-08 14:11:54 +08:00
}
.nav-container {
height: 100%;
display: flex;
align-items: center;
2025-10-16 18:03:46 +08:00
padding: 0 50px;
2025-10-08 14:11:54 +08:00
margin: 0 auto;
2025-10-16 18:03:46 +08:00
gap: 20px;
width: 100%;
position: relative;
2025-10-17 16:12:29 +08:00
overflow: visible;
2025-10-08 14:11:54 +08:00
}
.nav-logo {
display: flex;
align-items: center;
2025-10-16 18:03:46 +08:00
gap: 8px;
2025-10-08 14:11:54 +08:00
cursor: pointer;
2025-10-16 18:03:46 +08:00
.logo-icon {
width: 28px;
height: 28px;
display: block;
2025-10-08 14:11:54 +08:00
}
.logo-text {
2025-10-16 18:03:46 +08:00
font-family: 'PingFang SC', sans-serif;
font-weight: 700;
font-size: 20.6px;
line-height: 1.31;
color: #C62828;
2025-10-08 14:11:54 +08:00
white-space: nowrap;
}
}
.nav-menu {
display: flex;
align-items: center;
2025-10-16 18:03:46 +08:00
gap: 0;
flex: 1;
overflow-x: auto;
2025-10-17 16:12:29 +08:00
overflow-y: visible;
2025-10-16 18:03:46 +08:00
scroll-behavior: smooth;
/* 自定义滚动条样式 */
2025-10-17 16:12:29 +08:00
scrollbar-width: auto;
2025-10-16 18:03:46 +08:00
scrollbar-color: #C62828 #f5f5f5;
&::-webkit-scrollbar {
2025-10-17 16:12:29 +08:00
height: 8px;
2025-10-16 18:03:46 +08:00
}
&::-webkit-scrollbar-track {
background: #f5f5f5;
2025-10-17 16:12:29 +08:00
border-radius: 4px;
margin: 0 10px; /* 滚动条内缩 */
2025-10-16 18:03:46 +08:00
}
&::-webkit-scrollbar-thumb {
background: #C62828;
2025-10-17 16:12:29 +08:00
border-radius: 4px;
2025-10-16 18:03:46 +08:00
&:hover {
background: #B71C1C;
}
}
2025-10-08 14:11:54 +08:00
}
.nav-item {
position: relative;
2025-10-16 18:03:46 +08:00
height: 76px;
2025-10-08 14:11:54 +08:00
display: flex;
align-items: center;
2025-10-16 18:03:46 +08:00
flex-shrink: 0; /* 防止菜单项被压缩 */
&:hover {
.nav-link {
background: #f5f5f5;
font-weight: 600;
}
}
2025-10-08 14:11:54 +08:00
&.active {
.nav-link {
2025-10-16 18:03:46 +08:00
color: #C62828;
font-weight: 600;
2025-10-08 14:11:54 +08:00
}
}
}
.nav-link {
height: 100%;
display: flex;
align-items: center;
2025-10-16 18:03:46 +08:00
justify-content: center;
gap: 2px;
padding: 26px 21px;
color: #141F38;
font-family: 'PingFang SC', sans-serif;
font-weight: 500;
2025-10-08 14:11:54 +08:00
font-size: 16px;
2025-10-16 18:03:46 +08:00
line-height: 1.5;
2025-10-08 14:11:54 +08:00
cursor: pointer;
transition: all 0.3s;
white-space: nowrap;
user-select: none;
2025-10-16 18:03:46 +08:00
min-width: 106px;
2025-10-08 14:11:54 +08:00
&:hover {
2025-10-16 18:03:46 +08:00
color: #C62828;
2025-10-08 14:11:54 +08:00
}
.arrow-down {
2025-10-16 18:03:46 +08:00
font-size: 8px;
margin-left: 2px;
2025-10-08 14:11:54 +08:00
transition: transform 0.3s;
}
&:hover .arrow-down {
transform: rotate(180deg);
}
}
.dropdown-menu {
background: white;
border-radius: 4px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
opacity: 0;
visibility: hidden;
2025-10-17 12:05:04 +08:00
transform: scaleY(0);
transform-origin: top center;
transition: opacity 0.2s, transform 0.2s, visibility 0.2s;
2025-10-16 18:03:46 +08:00
z-index: 10000;
pointer-events: none;
2025-10-08 14:11:54 +08:00
&.show {
opacity: 1;
visibility: visible;
2025-10-17 12:05:04 +08:00
transform: scaleY(1);
2025-10-16 18:03:46 +08:00
pointer-events: auto;
2025-10-08 14:11:54 +08:00
}
}
.dropdown-item {
display: flex;
align-items: center;
2025-10-16 18:03:46 +08:00
justify-content: center;
2025-10-08 14:11:54 +08:00
gap: 8px;
2025-10-16 18:03:46 +08:00
padding: 12px 16px;
2025-10-08 14:11:54 +08:00
color: #333;
2025-10-16 18:03:46 +08:00
font-family: 'PingFang SC', sans-serif;
font-weight: 400;
2025-10-08 14:11:54 +08:00
font-size: 14px;
2025-10-16 18:03:46 +08:00
line-height: 1.5;
2025-10-08 14:11:54 +08:00
cursor: pointer;
transition: all 0.3s;
2025-10-16 18:03:46 +08:00
white-space: nowrap;
2025-10-08 14:11:54 +08:00
&:hover {
background: #f5f5f5;
2025-10-16 18:03:46 +08:00
color: #C62828;
2025-10-08 14:11:54 +08:00
}
&.active {
2025-10-16 18:03:46 +08:00
background: #ffe6e6;
color: #C62828;
font-weight: 500;
}
}
.nav-search {
margin-left: auto;
margin-right: 20px;
display: flex;
justify-content: center;
align-items: center;
}
.search-box {
position: relative;
width: 221px;
height: 36px;
border: 1px solid #BAC0CC;
border-radius: 30px;
background: white;
display: flex;
align-items: center;
overflow: hidden;
.search-input {
flex: 1;
height: 100%;
border: none;
outline: none;
padding: 7px 20px;
font-family: 'PingFang SC', sans-serif;
font-weight: 400;
font-size: 14px;
line-height: 1.57;
color: #333;
&::placeholder {
color: rgba(0, 0, 0, 0.3);
}
2025-10-08 14:11:54 +08:00
}
2025-10-16 18:03:46 +08:00
.search-icon {
position: absolute;
right: 10px;
top: 50%;
transform: translateY(-50%);
width: 17px;
height: 17px;
display: flex;
align-items: center;
justify-content: center;
background: #C62828;
border-radius: 50%;
padding: 2px;
img {
width: 14px;
height: 14px;
}
2025-10-08 14:11:54 +08:00
}
}
.nav-right {
margin-left: auto;
2025-10-16 18:03:46 +08:00
display: flex;
flex-shrink: 0; /* 防止右侧区域被压缩 */
2025-10-20 18:28:38 +08:00
gap: 20px;
align-items: center;
// 添加搜索框样式
:deep(.resource-search) {
width: 221px;
height: 36px;
padding: 0;
.search-box {
height: 36px;
}
input {
font-size: 14px;
padding: 0 70px 0 20px;
}
.search-button {
width: 48px;
height: 36px;
img {
width: 17px;
height: 17px;
}
}
}
2025-10-16 18:03:46 +08:00
}
/* 响应式设计 */
@media (max-width: 1200px) {
.nav-container {
padding: 0 30px;
gap: 15px;
}
.nav-link {
min-width: 90px;
padding: 26px 15px;
font-size: 15px;
}
.nav-menu {
/* 在小屏幕上确保滚动条可见 */
scrollbar-width: auto;
}
}
@media (max-width: 768px) {
.nav-container {
padding: 0 20px;
gap: 10px;
}
.nav-logo .logo-text {
font-size: 18px;
}
.nav-link {
min-width: 80px;
padding: 26px 12px;
font-size: 14px;
}
.search-box {
width: 180px;
}
}
@media (max-width: 480px) {
.nav-container {
padding: 0 15px;
gap: 8px;
}
.nav-logo .logo-text {
font-size: 16px;
}
.nav-link {
min-width: 70px;
padding: 26px 10px;
font-size: 13px;
}
.search-box {
width: 150px;
}
2025-10-08 14:11:54 +08:00
}
</style>