From 75f043dfa6660716364e66ee0b3cf99f44255686 Mon Sep 17 00:00:00 2001
From: DYL0109 <dn18191638832@163.com>
Date: 星期三, 16 四月 2025 19:20:36 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/develop1.0' into dyl_dev

---
 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 30b428a..3427d73 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']
+
+/**
+ * 鏌ユ壘鏈�娣卞眰鐨勫瓙鑿滃崟骞舵瀯寤哄畬鏁磋矾寰�
+ */
+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