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/permission.js | 174 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 174 insertions(+), 0 deletions(-) diff --git a/zhitan-vue/src/permission.js b/zhitan-vue/src/permission.js index e7e14db..b83f732 100644 --- a/zhitan-vue/src/permission.js +++ b/zhitan-vue/src/permission.js @@ -8,10 +8,90 @@ import useUserStore from '@/store/modules/user' import useSettingsStore from '@/store/modules/settings' import usePermissionStore from '@/store/modules/permission' +import useTagsViewStore from '@/store/modules/tagsView' NProgress.configure({ showSpinner: false }); const whiteList = ['/login', '/register', '/energy','/social-callback'] + +/** + * 鏌ユ壘鏈�娣卞眰鐨勫瓙鑿滃崟骞舵瀯寤哄畬鏁磋矾寰� + */ +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() @@ -36,6 +116,40 @@ router.addRoute(route) // 鍔ㄦ�佹坊鍔犲彲璁块棶璺敱琛� } }) + + // 濡傛灉鏄椤碉紝鑷姩閲嶅畾鍚戝埌绗竴涓彍鍗� + if (to.path === '/' || to.path === '/index') { + const permissionStore = usePermissionStore() + const topMenus = permissionStore.topbarRouters.filter(menu => !menu.hidden) + if (topMenus.length > 0) { + // 璺宠浆鍒扮涓�涓彍鍗� + const firstMenu = topMenus[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 childPath = firstMenu.path.endsWith('/') ? firstMenu.path + firstChild.path : `${firstMenu.path}/${firstChild.path}` + next({ path: childPath, replace: true }) + return + } else { + // 娌℃湁瀛愯彍鍗曪紝鐩存帴璺宠浆 + next({ path: firstMenu.path, replace: true }) + return + } + } + } + next({ ...to, replace: true }) // hack鏂规硶 纭繚addRoutes宸插畬鎴� }) }).catch(err => { @@ -45,6 +159,58 @@ }) }) } else { + // 濡傛灉鏄椤碉紝鑷姩閲嶅畾鍚戝埌绗竴涓彍鍗� + if (to.path === '/' || to.path === '/index') { + const permissionStore = usePermissionStore() + const topMenus = permissionStore.topbarRouters.filter(menu => !menu.hidden) + if (topMenus.length > 0) { + // 璺宠浆鍒扮涓�涓彍鍗� + const firstMenu = topMenus[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 childPath = firstMenu.path.endsWith('/') ? firstMenu.path + firstChild.path : `${firstMenu.path}/${firstChild.path}` + next({ path: childPath, replace: true }) + return + } else { + // 娌℃湁瀛愯彍鍗曪紝鐩存帴璺宠浆 + next({ path: firstMenu.path, replace: true }) + return + } + } + } + + // 鑷姩澶勭悊甯︽湁閲嶅畾鍚戠殑璺敱 + 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() } } @@ -62,4 +228,12 @@ router.afterEach(() => { NProgress.done() + + // 绉婚櫎鎵�鏈夊彲鑳界殑棣栭〉鏍囩 + const tagsViewStore = useTagsViewStore(); + if (tagsViewStore && tagsViewStore.visitedViews) { + tagsViewStore.visitedViews = tagsViewStore.visitedViews.filter( + tag => tag.path !== '/index' && tag.path !== '/' && tag.name !== 'Index' + ); + } }) -- Gitblit v1.9.3