From 5d36e1f987ef21e44ded2e8a1d06c28094ec1e76 Mon Sep 17 00:00:00 2001 From: baoshiwei <baoshiwei@shlanbao.cn> Date: 星期六, 19 四月 2025 12:39:47 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/master' --- zhitan-vue/src/layout/components/Sidebar/SidebarItem.vue | 234 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 231 insertions(+), 3 deletions(-) diff --git a/zhitan-vue/src/layout/components/Sidebar/SidebarItem.vue b/zhitan-vue/src/layout/components/Sidebar/SidebarItem.vue index 3a85e7e..8e38476 100644 --- a/zhitan-vue/src/layout/components/Sidebar/SidebarItem.vue +++ b/zhitan-vue/src/layout/components/Sidebar/SidebarItem.vue @@ -9,7 +9,7 @@ </app-link> </template> - <el-sub-menu v-else ref="subMenu" :index="resolvePath(item.path)" teleported> + <el-sub-menu v-else ref="subMenu" :index="resolvePath(item.path)" teleported @click="handleSubMenuClick"> <template v-if="item.meta" #title> <svg-icon :icon-class="item.meta && item.meta.icon" /> <span class="menu-title" :title="hasTitle(item.meta.title)">{{ item.meta.title }}</span> @@ -31,6 +31,9 @@ import { isExternal } from '@/utils/validate' import AppLink from './Link' import { getNormalPath } from '@/utils/ruoyi' +import { useRouter } from 'vue-router' + +const router = useRouter(); const props = defineProps({ // route object @@ -50,6 +53,204 @@ const onlyOneChild = ref({}); +/** + * 鏌ユ壘鏈�娣卞眰鐨勫瓙鑿滃崟锛堝彾瀛愯妭鐐癸級 + * 閫掑綊鏌ユ壘绗竴涓病鏈塩hildren鐨勫瓙鑿滃崟 + */ +function findDeepestLeafMenu(route) { + if (!route) return null; + + // 濡傛灉娌℃湁瀛愯彍鍗曟垨瀛愯彍鍗曚负绌猴紝鍒欒繑鍥炲綋鍓嶈矾鐢� + if (!route.children || route.children.length === 0) { + return route; + } + + // 鎵惧埌绗竴涓潪闅愯棌鐨勫瓙鑿滃崟 + const firstVisibleChild = route.children.find(child => !child.hidden); + if (!firstVisibleChild) { + return route; // 濡傛灉鎵�鏈夊瓙鑿滃崟閮芥槸闅愯棌鐨勶紝杩斿洖褰撳墠璺敱 + } + + // 閫掑綊鏌ユ壘杩欎釜瀛愯彍鍗曠殑鏈�娣卞眰瀛愯彍鍗� + return findDeepestLeafMenu(firstVisibleChild); +} + +// 澶勭悊瀛愯彍鍗曠偣鍑� +function handleSubMenuClick(e) { + // 闃绘浜嬩欢鍐掓场 + e.stopPropagation(); + + // 濡傛灉鐐瑰嚮鐨勬槸瀛愯彍鍗曟爣棰橈紝鍒欒嚜鍔ㄥ鑸埌鏈�娣卞眰鐨勫瓙鑿滃崟 + if (e.target.closest('.el-sub-menu__title')) { + // 鎸夌収姝g‘鐨勮矾寰勬瀯寤哄眰绾� + let currentNode = props.item; + + console.log('褰撳墠鐐瑰嚮鐨勮彍鍗曢」:', JSON.stringify(currentNode, null, 2)); + console.log('basePath:', props.basePath); + + // 鑾峰彇绗竴涓彲瑙佸瓙鑿滃崟锛屽鏋滄病鏈夊彲瑙佸瓙鑿滃崟锛屼笉杩涜璺宠浆 + if (!currentNode.children || currentNode.children.length === 0) { + return; + } + + const firstVisibleChild = currentNode.children.find(child => !child.hidden); + if (!firstVisibleChild) { + return; + } + + console.log('绗竴涓彲瑙佸瓙鑿滃崟:', JSON.stringify(firstVisibleChild, null, 2)); + + // 鏃ュ織绠$悊绛変笁绾ц彍鍗曠壒娈婂鐞� + // 妫�鏌ユ槸鍚︽湁棰勫厛鍐欏叆鐨勫畬鏁磋矾寰勶紝濡傛灉鏈夊垯鐩存帴浣跨敤 + if (firstVisibleChild.fullPath) { + console.log('浣跨敤棰勫厛璁剧疆鐨勫畬鏁磋矾寰�:', firstVisibleChild.fullPath); + router.push({ path: firstVisibleChild.fullPath }); + return; + } + + // 鍒ゆ柇鏄惁鏄郴缁�/鏃ュ織绠$悊绫诲瀷鐨勪笁绾ц彍鍗曪紙渚嬪锛�/system/log/operlog锛� + // 杩欑鎯呭喌涓嬶紝鐩存帴璺宠浆鍒扮涓�涓瓙鑿滃崟鐨勫畬鏁磋矾寰� + if (firstVisibleChild.component === 'ParentView' || + (typeof firstVisibleChild.component === 'object' && + firstVisibleChild.component.name === 'ParentView')) { + console.log('妫�娴嬪埌ParentView缁勪欢锛屽鐞嗕笁绾ц彍鍗�'); + + // 鏄湁涓夌骇鑿滃崟鐨勬儏鍐� + if (firstVisibleChild.children && firstVisibleChild.children.length > 0) { + const grandChild = firstVisibleChild.children.find(child => !child.hidden); + if (grandChild) { + console.log('鎵惧埌绗笁绾ц彍鍗�:', JSON.stringify(grandChild, null, 2)); + + // 鍒ゆ柇鏄惁搴旇浣跨敤parentPath + if (firstVisibleChild.parentPath && grandChild.path.startsWith('/')) { + console.log('浣跨敤parentPath灞炴��:', firstVisibleChild.parentPath); + // 濡傛灉瀛愯彍鍗曟槸缁濆璺緞锛屼絾鏈塸arentPath锛屽垯搴旇浣跨敤parentPath浣滀负鍩虹 + let fullPath = firstVisibleChild.parentPath; + if (!fullPath.startsWith('/')) { + fullPath = '/' + fullPath; + } + + // 绗簩绾ц矾寰勫熀浜庢牴璺緞 + if (firstVisibleChild.path.startsWith('/')) { + // 绗簩绾у凡缁忔槸缁濆璺緞锛屾埅鍙栨渶鍚庨儴鍒� + const pathParts = firstVisibleChild.path.split('/'); + const lastPart = pathParts[pathParts.length - 1]; + fullPath = fullPath + '/' + lastPart; + } else { + fullPath = buildFullPath(fullPath, firstVisibleChild.path); + } + console.log('浜岀骇璺緞:', fullPath); + + // 绗笁绾ц矾寰勫熀浜庝簩绾ц矾寰� + if (grandChild.path.startsWith('/')) { + // 绗笁绾ф槸缁濆璺緞锛屾埅鍙栨渶鍚庨儴鍒� + const pathParts = grandChild.path.split('/'); + const lastPart = pathParts[pathParts.length - 1]; + fullPath = fullPath + '/' + lastPart; + } else { + fullPath = buildFullPath(fullPath, grandChild.path); + } + console.log('涓夌骇璺緞 (鏈�缁�):', fullPath); + + // 瀵艰埅鍒扮涓夌骇鑿滃崟 + if (grandChild.query) { + router.push({ path: fullPath, query: grandChild.query }); + } else { + router.push({ path: fullPath }); + } + return; + } + + // 甯歌璺緞鏋勫缓 + let fullPath; + + // 绗竴绾ц矾寰勫繀椤绘槸瀹屾暣鐨勶紙渚嬪/system锛� + if (currentNode.path.startsWith('/')) { + fullPath = currentNode.path; + } else { + fullPath = '/' + currentNode.path; + } + console.log('涓�绾ц矾寰�:', fullPath); + + // 绗簩绾ц矾寰勫繀椤诲熀浜庣涓�绾ц矾寰勶紙渚嬪/system/log锛� + fullPath = buildFullPath(fullPath, firstVisibleChild.path); + console.log('浜岀骇璺緞:', fullPath); + + // 绗笁绾ц矾寰勫繀椤诲熀浜庝簩绾ц矾寰勶紙渚嬪/system/log/operlog锛� + fullPath = buildFullPath(fullPath, grandChild.path); + console.log('涓夌骇璺緞 (鏈�缁�):', fullPath); + + // 瀵艰埅鍒扮涓夌骇鑿滃崟 + if (grandChild.query) { + console.log('璺宠浆鍒�:', fullPath, '甯﹀弬鏁�:', grandChild.query); + router.push({ path: fullPath, query: grandChild.query }); + } else { + console.log('璺宠浆鍒�:', fullPath); + router.push({ path: fullPath }); + } + return; + } + } + } + + console.log('澶勭悊鏍囧噯浜岀骇鑿滃崟'); + + // 妫�鏌ユ槸鍚﹂渶瑕佷娇鐢╬arentPath + if (firstVisibleChild.parentPath && firstVisibleChild.path.startsWith('/')) { + console.log('浣跨敤parentPath灞炴��:', firstVisibleChild.parentPath); + // 濡傛灉瀛愯彍鍗曟槸缁濆璺緞锛屼絾鏈塸arentPath锛屽垯搴旇浣跨敤parentPath浣滀负鍩虹 + let fullPath = firstVisibleChild.parentPath; + if (!fullPath.startsWith('/')) { + fullPath = '/' + fullPath; + } + + // 鏋勫缓瀹屾暣璺緞 + if (firstVisibleChild.path.startsWith('/')) { + // 鎴彇瀛愯矾寰勭殑鏈�鍚庨儴鍒� + const pathParts = firstVisibleChild.path.split('/'); + const lastPart = pathParts[pathParts.length - 1]; + fullPath = fullPath + '/' + lastPart; + } else { + fullPath = buildFullPath(fullPath, firstVisibleChild.path); + } + console.log('鏋勫缓鐨勬渶缁堣矾寰�:', fullPath); + + // 瀵艰埅鍒扮洰鏍囪矾鐢� + if (firstVisibleChild.query) { + router.push({ path: fullPath, query: firstVisibleChild.query }); + } else { + router.push({ path: fullPath }); + } + return; + } + + // 鏍囧噯鐨勪簩绾ц彍鍗曞鐞� + // 鏋勫缓姝g‘鐨勮矾寰� + let fullPath; + + // 澶勭悊绗竴绾ц矾寰勶紙渚嬪/system锛�- 蹇呴』鏄畬鏁寸殑璺緞 + if (currentNode.path.startsWith('/')) { + fullPath = currentNode.path; + } else { + fullPath = '/' + currentNode.path; + } + console.log('涓�绾ц矾寰�:', fullPath); + + // 澶勭悊绗簩绾ц矾寰勶紙渚嬪/system/user锛�- 蹇呴』鍩轰簬绗竴绾ц矾寰� + fullPath = buildFullPath(fullPath, firstVisibleChild.path); + console.log('浜岀骇璺緞 (鏈�缁�):', fullPath); + + // 瀵艰埅鍒扮洰鏍囪矾鐢� + if (firstVisibleChild.query) { + console.log('璺宠浆鍒�:', fullPath, '甯﹀弬鏁�:', firstVisibleChild.query); + router.push({ path: fullPath, query: firstVisibleChild.query }); + } else { + console.log('璺宠浆鍒�:', fullPath); + router.push({ path: fullPath }); + } + } +} + function hasOneShowingChild(children = [], parent) { if (!children) { children = []; @@ -65,8 +266,13 @@ }) // When there is only one child router, the child router is displayed by default - if (showingChildren.length === 1) { + if (showingChildren.length === 1 && !showingChildren[0].children) { return true + } + + // If the single child also has children, don't treat it as a single showing child + if (showingChildren.length === 1 && showingChildren[0].children && showingChildren[0].children.length > 0) { + return false } // Show parent if there are no child router to display @@ -86,12 +292,34 @@ return props.basePath } if (routeQuery) { - let query = JSON.parse(routeQuery); + let query = routeQuery; + // 濡傛灉 routeQuery 鏄瓧绗︿覆锛屽垯灏濊瘯瑙f瀽瀹� + if (typeof routeQuery === 'string') { + try { + query = JSON.parse(routeQuery); + } catch (error) { + console.error('Error parsing query string:', routeQuery, error); + } + } return { path: getNormalPath(props.basePath + '/' + routePath), query: query } } return getNormalPath(props.basePath + '/' + routePath) } +// 姝g‘鏋勫缓璺緞 +function buildFullPath(base, segment) { + // 濡傛灉segment鏄粷瀵硅矾寰勶紝鐩存帴杩斿洖 + if (segment.startsWith('/')) { + return segment; + } + + // 纭繚base鏈夋纭殑寮�澶存枩鏉� + const normalizedBase = base.startsWith('/') ? base : '/' + base; + + // 鎷兼帴璺緞锛岄伩鍏嶅弻鏂滄潬 + return normalizedBase.endsWith('/') ? normalizedBase + segment : normalizedBase + '/' + segment; +} + function hasTitle(title){ if (title.length > 5) { return title; -- Gitblit v1.9.3