From 862a2858c2807f8b844d8ccd8966ddb4de717375 Mon Sep 17 00:00:00 2001
From: zuozhengqing <a13193816592@163.com>
Date: 星期三, 13 十二月 2023 19:20:55 +0800
Subject: [PATCH] 添加路由拦截 导航重定向

---
 src/components/layout/components/appsidebar/index.vue |    2 +-
 src/router/index.js                                   |   45 +++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 44 insertions(+), 3 deletions(-)

diff --git a/src/components/layout/components/appsidebar/index.vue b/src/components/layout/components/appsidebar/index.vue
index 53edca9..2ba00f9 100644
--- a/src/components/layout/components/appsidebar/index.vue
+++ b/src/components/layout/components/appsidebar/index.vue
@@ -16,7 +16,7 @@
         text-color="#bfcbd9"
         active-text-color="#fff"
       >
-        <el-submenu v-for="item in menus" :key="item.id" :index="item.id">
+        <el-submenu v-for="item in menus" :key="item.id" :index="item.id.toString()">
           <template slot="title">
             <i class="el-icon-money icon"></i>
             <span>{{item.title}}</span>
diff --git a/src/router/index.js b/src/router/index.js
index c98cba4..5b0aac6 100644
--- a/src/router/index.js
+++ b/src/router/index.js
@@ -5,6 +5,7 @@
 import supplierRouter from "./supplier/index.js"
 import purchaseRouter from "./purchase/index.js"
 import productRouter from "./product/index.js"
+import {getMenuTreeByRole} from "@/api/menus/index"
 
 Vue.use(Router)
 const login = (resolve) => require(["@/views/other/login/index"], resolve)
@@ -18,8 +19,8 @@
 export const routes = [
   // 鏃犳暟鎹〉闈�
   {
-    path: "/",
-    name: "NoData",
+    path: "noData",
+    name: "noData",
     meta: {
       title: "",
       auth: true,
@@ -107,6 +108,46 @@
 
 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 === 4);
+    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) => {
+  console.log(to,from,"鐪嬬湅")
+  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.beforeEach((to, from, next) => {
 // must call `next`
 // console.log(to, from)

--
Gitblit v1.8.0