From 725da078afc3398f49f1efdc25bc82b9ec1dac35 Mon Sep 17 00:00:00 2001 From: DYL0109 <dn18191638832@163.com> Date: 星期三, 16 四月 2025 19:27:30 +0800 Subject: [PATCH] Merge pull request #64 from zhitan-cloud/develop1.0 --- zhitan-vue/src/permission.js | 139 +++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 131 insertions(+), 8 deletions(-) diff --git a/zhitan-vue/src/permission.js b/zhitan-vue/src/permission.js index a521d95..3427d73 100644 --- a/zhitan-vue/src/permission.js +++ b/zhitan-vue/src/permission.js @@ -14,6 +14,85 @@ const whiteList = ['/login', '/register', '/energy'] +/** + * 鏌ユ壘鏈�娣卞眰鐨勫瓙鑿滃崟骞舵瀯寤哄畬鏁磋矾寰� + */ +function findDeepestPath(route) { + if (!route) return { path: null, query: null }; + + // 棣栧厛娣诲姞褰撳墠鑺傜偣鐨勮矾寰� + let currentNode = route; + let pathSegments = []; + + if (currentNode.path) { + pathSegments.push(currentNode.path); + } + + // 閫愬眰娣诲姞瀛愯矾寰� + while (currentNode.children && currentNode.children.length > 0) { + const firstChild = currentNode.children.find(child => !child.hidden); + if (!firstChild) break; + + // 璺宠繃ParentView绫诲瀷鐨勪腑闂磋妭鐐� + if (firstChild.component === 'ParentView' || + (typeof firstChild.component === 'object' && + firstChild.component.name === 'ParentView')) { + currentNode = firstChild; + // 濡傛灉璺緞涓嶆槸浠�/寮�澶达紝鍒欐坊鍔犲埌璺緞鐗囨涓� + if (!firstChild.path.startsWith('/')) { + pathSegments.push(firstChild.path); + } else { + // 濡傛灉鏄粷瀵硅矾寰勶紝鍒欐浛鎹箣鍓嶆墍鏈夎矾寰� + pathSegments = [firstChild.path]; + } + continue; + } + + // 鏅�氳妭鐐瑰鐞� + currentNode = firstChild; + // 濡傛灉璺緞涓嶆槸浠�/寮�澶达紝鍒欐坊鍔犲埌璺緞鐗囨涓� + if (!firstChild.path.startsWith('/')) { + pathSegments.push(firstChild.path); + } else { + // 濡傛灉鏄粷瀵硅矾寰勶紝鍒欐浛鎹箣鍓嶆墍鏈夎矾寰� + pathSegments = [firstChild.path]; + } + + // 濡傛灉鍒拌揪鍙跺瓙鑺傜偣锛屽垯缁撴潫鏌ユ壘 + if (!firstChild.children || firstChild.children.length === 0) { + break; + } + } + + // 鏋勫缓鏈�缁堣矾寰� + let targetPath = ''; + if (pathSegments.length > 0) { + // 濡傛灉绗竴娈典笉鏄互/寮�澶达紝娣诲姞/ + if (!pathSegments[0].startsWith('/')) { + pathSegments[0] = '/' + pathSegments[0]; + } + + // 缁勫悎璺緞 + targetPath = pathSegments.reduce((fullPath, segment, index) => { + if (segment.startsWith('/')) { + return segment; + } else if (index === 0) { + return segment; + } else { + // 纭繚璺緞涔嬮棿涓嶄細鍑虹幇閲嶅鐨勬枩鏉� + const base = fullPath.endsWith('/') ? fullPath.slice(0, -1) : fullPath; + const part = segment.startsWith('/') ? segment : '/' + segment; + return `${base}${part}`; + } + }); + } + + return { + path: targetPath, + query: currentNode.query + }; +} + router.beforeEach((to, from, next) => { NProgress.start() if (getToken()) { @@ -45,11 +124,23 @@ if (topMenus.length > 0) { // 璺宠浆鍒扮涓�涓彍鍗� const firstMenu = topMenus[0] - if (firstMenu.children && firstMenu.children.length > 0) { - // 鏈夊瓙鑿滃崟锛岃烦杞埌绗竴涓瓙鑿滃崟 + + // 鏌ユ壘鏈�娣卞眰鐨勫瓙鑿滃崟骞舵瀯寤鸿矾寰� + const { path, query } = findDeepestPath(firstMenu); + + if (path) { + // 鏈夋渶娣卞眰瀛愯彍鍗曪紝璺宠浆鍒拌鑿滃崟 + if (query) { + next({ path, query, replace: true }); + } else { + next({ path, replace: true }); + } + return; + } else if (firstMenu.children && firstMenu.children.length > 0) { + // 浣跨敤鍘熸湁閫昏緫 const firstChild = firstMenu.children[0] - const path = firstMenu.path.endsWith('/') ? firstMenu.path + firstChild.path : `${firstMenu.path}/${firstChild.path}` - next({ path: path, replace: true }) + const childPath = firstMenu.path.endsWith('/') ? firstMenu.path + firstChild.path : `${firstMenu.path}/${firstChild.path}` + next({ path: childPath, replace: true }) return } else { // 娌℃湁瀛愯彍鍗曪紝鐩存帴璺宠浆 @@ -75,11 +166,23 @@ if (topMenus.length > 0) { // 璺宠浆鍒扮涓�涓彍鍗� const firstMenu = topMenus[0] - if (firstMenu.children && firstMenu.children.length > 0) { - // 鏈夊瓙鑿滃崟锛岃烦杞埌绗竴涓瓙鑿滃崟 + + // 鏌ユ壘鏈�娣卞眰鐨勫瓙鑿滃崟骞舵瀯寤鸿矾寰� + const { path, query } = findDeepestPath(firstMenu); + + if (path) { + // 鏈夋渶娣卞眰瀛愯彍鍗曪紝璺宠浆鍒拌鑿滃崟 + if (query) { + next({ path, query, replace: true }); + } else { + next({ path, replace: true }); + } + return; + } else if (firstMenu.children && firstMenu.children.length > 0) { + // 浣跨敤鍘熸湁閫昏緫 const firstChild = firstMenu.children[0] - const path = firstMenu.path.endsWith('/') ? firstMenu.path + firstChild.path : `${firstMenu.path}/${firstChild.path}` - next({ path: path, replace: true }) + const childPath = firstMenu.path.endsWith('/') ? firstMenu.path + firstChild.path : `${firstMenu.path}/${firstChild.path}` + next({ path: childPath, replace: true }) return } else { // 娌℃湁瀛愯彍鍗曪紝鐩存帴璺宠浆 @@ -88,6 +191,26 @@ } } } + + // 鑷姩澶勭悊甯︽湁閲嶅畾鍚戠殑璺敱 + if (to.matched.length > 0 && to.matched[0].path === to.path) { + const currentRouteConfig = router.getRoutes().find(r => r.path === to.path); + + if (currentRouteConfig && currentRouteConfig.children && currentRouteConfig.children.length > 0) { + // 鏈夊瓙璺敱锛岃嚜鍔ㄥ鑸埌鏈�娣卞眰瀛愯彍鍗� + const { path, query } = findDeepestPath(currentRouteConfig); + + if (path && path !== to.path) { + if (query) { + next({ path, query, replace: true }); + } else { + next({ path, replace: true }); + } + return; + } + } + } + next() } } -- Gitblit v1.9.3