From fe87d39adb4e995ccf96a45b142ae026c3cf5eae Mon Sep 17 00:00:00 2001
From: zuozhengqing <a13193816592@163.com>
Date: 星期三, 13 十二月 2023 18:10:09 +0800
Subject: [PATCH] 配置路由守卫导航重定向

---
 src/router/index.js |   46 +++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 43 insertions(+), 3 deletions(-)

diff --git a/src/router/index.js b/src/router/index.js
index f89362d..b128a9b 100644
--- a/src/router/index.js
+++ b/src/router/index.js
@@ -6,6 +6,7 @@
 import operateRouter from "./operate/index.js" // 鎿嶄綔
 import warehouseManageRouter from "./warehouseManage/index.js" // 浠撳簱绠$悊
 import reportRouter from "./report/index.js" // 鎶ヨ〃
+import {getMenuTreeByRole} from "@/api/menus/index"
 
 Vue.use(Router)
 const login = (resolve) => require(["@/views/other/login/index"], resolve)
@@ -19,8 +20,8 @@
 export const routes = [
   // 鏃犳潈闄愭暟鎹〉闈�
   {
-    path: "/",
-    name: "NoData",
+    path: "noData",
+    name: "noData",
     meta: {
       title: "",
       auth: true,
@@ -118,9 +119,48 @@
   })
 
 const router = createRouter()
+ 
+let isSkip = false;
+async function hasPermission(routePath) {
+  isSkip = false;
+  try {
+    const res = await getMenuTreeByRole();
+    const newPath = {
+      path: "/noData"
+    };
+    const foundObject = res.data.list.find(obj => obj.systemType === 3);
+    if (foundObject) {
+      foundObject.menus.forEach(item => {
+        const nextPath = item.children.find(obj => obj.path === routePath);
+        if (nextPath) {
+          newPath.path = nextPath.path;
+          isSkip = true;
+        }
+      });
+    } else {
+      newPath.path = '/noData';
+    }
+    return newPath; 
+  } catch (error) {
+    return { path: "/noData" }; 
+  }
+}
+router.beforeEach(async (to, from, next) => {
+  try {
+    const result = await hasPermission(to.path);
+    console.log(result,"result")
+    next();
+    if (!isSkip) {
+      next('/noData')
+    }
+  } catch (error) {
+    console.error('Error in navigation guard:', error);
+    next(); 
+  }
+});
+
 
 router.afterEach((to, from, next) => {
-  console.log(to, "to")
   if ((to.path === "/overview/overviewList" || to.path === "/productManage/productList") && to.params.name) {
     console.log(from, next)
     to.meta.title = to.params.name

--
Gitblit v1.8.0