Files
continew-admin-ui/src/layout/LayoutMix.vue
kiki1373639299 069175bf16 fix: 修复混合布局模式下详情页左侧菜单消失问题
Closes #ID5HXD

Co-authored-by: kiki1373639299<zkai0106@163.com>



# message auto-generated for no-merge-commit merge:
!13 merge menu-error into dev

fix: 修复混合布局模式下详情页左侧菜单消失问题

Created-by: kiki1373639299
Commit-by: kiki1373639299
Merged-by: Charles_7c
Description: <!--
  非常感谢您的 PR!在提交之前,请务必确保您 PR 的代码经过了完整测试,并且通过了代码规范检查。
-->

<!-- 在 [] 中输入 x 来勾选) -->

## PR 类型

<!-- 您的 PR 引入了哪种类型的变更? -->
<!-- 只支持选择一种类型,如果有多种类型,可以在更新日志中增加 “类型” 列。 -->

- [ ] 新 feature
- [X] Bug 修复
- [ ] 功能增强
- [ ] 文档变更
- [ ] 代码样式变更
- [ ] 重构
- [ ] 性能改进
- [ ] 单元测试
- [ ] CI/CD
- [ ] 其他

## PR 目的

<!-- 描述一下您的 PR 解决了什么问题。如果可以,请链接到相关 issues。 -->

## 解决方案
  修改 src/layout/LayoutMix.vue 中的 getLeftMenus 函数:
  - 优先从路由的 meta.activeMenu 获取菜单 key
  - 如果没有 activeMenu,则使用 path
  - 确保详情页能正确关联到父菜单并显示其子菜单
<!-- 详细描述您是如何解决的问题 -->

## PR 测试

<!-- 如果可以,请为您的 PR 添加或更新单元测试。 -->
<!-- 请描述一下您是如何测试 PR 的。例如:创建/更新单元测试或添加相关的截图。 -->

## Changelog

| 模块  | Changelog | Related issues |
|-----|-----------| -------------- |
|  src/layout/LayoutMix.vue    |      src/layout/LayoutMix.vue:101-112: 重构 getLeftMenus 函数,优先使用 meta.activeMenu 作为菜单 key      |     Closes #ID5HXD           |

<!-- 如果有多种类型的变更,可以在变更日志表中增加 “类型” 列,该列的值与上方 “PR 类型” 相同。 -->
<!-- Related issues 格式为 Closes #<issue号>,或者 Fixes #<issue号>,或者 Resolves #<issue号>。 -->

## 其他信息

<!-- 请描述一下还有哪些注意事项。例如:如果引入了一个不向下兼容的变更,请描述其影响。 -->

## 提交前确认

- [X] PR 代码经过了完整测试,并且通过了代码规范检查
- [X] 已经完整填写 Changelog,并链接到了相关 issues
- [X] PR 代码将要提交到 dev 分支

See merge request: continew/continew-admin-ui!13
2025-11-07 14:24:59 +08:00

217 lines
5.7 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<div class="layout-mix">
<section
v-if="isDesktop" class="layout-mix-left" :class="{ 'app-menu-dark': appStore.menuDark }"
:style="appStore.menuDark ? appStore.themeCSSVar : undefined"
>
<Logo :collapsed="appStore.menuCollapse"></Logo>
<div class="menu-container">
<Menu :menus="leftMenus" :menu-style="{ width: '220px', flex: 1 }"></Menu>
</div>
<WwAds class="ads" />
</section>
<section class="layout-mix-right">
<header class="header">
<MenuFoldBtn></MenuFoldBtn>
<a-menu
v-if="isDesktop" mode="horizontal" :selected-keys="activeMenu" :auto-open-selected="false"
:trigger-props="{ animationName: 'slide-dynamic-origin' }" @menu-item-click="onMenuItemClick"
>
<a-menu-item v-for="item in topMenus" :key="item.path">
<template #icon>
<GiSvgIcon :name="getMenuIcon(item)" :size="24" />
</template>
<span>{{ item.meta?.title || item.children?.[0]?.meta?.title || '' }}</span>
</a-menu-item>
</a-menu>
<HeaderRightBar></HeaderRightBar>
</header>
<Tabs></Tabs>
<Main></Main>
<GiFooter v-if="appStore.copyrightDisplay" />
</section>
<!-- 公告弹窗 -->
<NoticePopup ref="noticePopupRef" />
</div>
</template>
<script setup lang="ts">
import { nextTick, onMounted, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import { searchTree } from 'xe-utils'
import Main from './components/Main.vue'
import Tabs from './components/Tabs/index.vue'
import Menu from './components/Menu/index.vue'
import HeaderRightBar from './components/HeaderRightBar/index.vue'
import Logo from './components/Logo.vue'
import MenuFoldBtn from './components/MenuFoldBtn.vue'
import WwAds from './components/WwAds.vue'
import GiFooter from '@/components/GiFooter/index.vue'
import NoticePopup from '@/views/user/message/components/NoticePopup.vue'
import { useAppStore, useRouteStore } from '@/stores'
import { isExternal } from '@/utils/validate'
import { filterTree } from '@/utils'
import { useDevice } from '@/hooks'
import { getToken } from '@/utils/auth'
defineOptions({ name: 'LayoutMix' })
const route = useRoute()
const router = useRouter()
const appStore = useAppStore()
const routeStore = useRouteStore()
const { isDesktop } = useDevice()
// 过滤是菜单的路由
const cloneRoutes = JSON.parse(JSON.stringify(routeStore.routes)) as RouteRecordRaw[]
const menuRoutes = filterTree(cloneRoutes, (i) => i.meta?.hidden === false)
// 顶部一级菜单
const topMenus = ref<RouteRecordRaw[]>([])
topMenus.value = JSON.parse(JSON.stringify(menuRoutes))
// 公告弹窗引用
const noticePopupRef = ref<InstanceType<typeof NoticePopup>>()
// 检查并显示未读公告
const checkAndShowNotices = () => {
const token = getToken()
// 如果有token检查未读公告
if (token) {
setTimeout(() => {
noticePopupRef.value?.open()
}, 1000) // 延迟1秒显示让页面先加载完成
}
}
const getMenuIcon = (item: RouteRecordRaw) => {
return item.meta?.icon || item.children?.[0].meta?.icon
}
// 克隆是菜单的路由
const cloneMenuRoutes: RouteRecordRaw[] = JSON.parse(JSON.stringify(menuRoutes))
// 顶部一级菜单选中的
const activeMenu = ref<string[]>([])
// 左侧的菜单
const leftMenus = ref<RouteRecordRaw[]>([])
// 获取左侧菜单
const getLeftMenus = (currentRoute?: RouteRecordRaw, key?: string) => {
// 优先从路由的 meta.activeMenu 获取key如果没有则使用path
const menuKey = currentRoute
? (currentRoute.meta?.activeMenu as string) || currentRoute.path
: key || ''
const arr = searchTree(cloneMenuRoutes, (i) => i.path === menuKey, { children: 'children' })
const rootPath = arr.length ? arr[0].path : ''
const obj = cloneMenuRoutes.find((i) => i.path === rootPath)
activeMenu.value = obj ? [obj.path] : ['']
leftMenus.value = obj ? (obj.children as RouteRecordRaw[]) : []
}
const onMenuItemClick = (key: string) => {
if (isExternal(key)) {
window.open(key)
return
}
setTimeout(() => getLeftMenus(undefined, key))
const obj = topMenus.value.find((i) => i.path === key)
if (obj && obj.redirect === 'noRedirect') return
router.push({ path: key })
}
watch(
() => route.path,
() => {
nextTick(() => {
getLeftMenus(route)
})
},
{ immediate: true },
)
onMounted(() => {
checkAndShowNotices()
})
</script>
<style scoped lang="scss">
:deep(.arco-menu-pop) {
white-space: nowrap;
}
:deep(.arco-menu.arco-menu-vertical.arco-menu-collapsed) {
// Menu菜单组件修改
.arco-menu-icon {
margin-right: 0;
padding: 10px 0;
}
.arco-menu-has-icon {
padding: 0;
justify-content: center;
}
.arco-menu-title {
display: none;
}
}
:deep(.arco-menu-horizontal) {
flex: 1;
overflow: hidden;
.arco-menu-inner {
padding-left: 0;
.arco-menu-overflow-wrap {
white-space: nowrap;
}
}
}
.layout-mix {
height: 100%;
display: flex;
align-items: stretch;
overflow: hidden;
&-left {
border-right: 1px solid var(--color-border);
background-color: var(--color-bg-1);
display: flex;
flex-direction: column;
overflow: hidden;
height: 100vh;
.menu-container {
flex: 1;
overflow-y: auto;
overflow-x: hidden;
}
}
&-right {
flex: 1;
display: flex;
flex-direction: column;
overflow: hidden;
}
}
.header {
padding: 0 $padding;
height: 56px;
color: var(--color-text-1);
background: var(--color-bg-1);
border-bottom: 1px solid var(--color-border);
display: flex;
justify-content: space-between;
align-items: center;
overflow: hidden;
}
</style>