From fd9103305b61e41f0b5cc7ff1bff89c56871beb5 Mon Sep 17 00:00:00 2001
From: shuishen <1109946754@qq.com>
Date: Wed, 04 Jun 2025 19:17:13 +0800
Subject: [PATCH] feat:不经过首页跳转测试

---
 src/permission copy 2.js |   71 ++++++++++++++
 src/permission.js        |   62 ++++++++++++
 src/permission copy.js   |  133 ++++++++++++++++++++++++++
 3 files changed, 266 insertions(+), 0 deletions(-)

diff --git a/src/permission copy 2.js b/src/permission copy 2.js
new file mode 100644
index 0000000..821f3dc
--- /dev/null
+++ b/src/permission copy 2.js
@@ -0,0 +1,71 @@
+/*
+ * @Author: GuLiMmo 2820890765@qq.com
+ * @Date: 2024-08-23 10:51:53
+ * @LastEditors: GuLiMmo 2820890765@qq.com
+ * @LastEditTime: 2024-08-29 14:10:56
+ * @FilePath: /drone-web-manage/src/permission.js
+ * @Description:
+ * Copyright (c) 2024 by GuLiMmo, All Rights Reserved.
+ */
+import router from './router/'
+import store from './store'
+import { getToken } from '@/utils/auth'
+import { getUrlParams } from './utils/validate'
+import NProgress from 'nprogress' // progress bar
+import 'nprogress/nprogress.css' // progress bar style
+NProgress.configure({ showSpinner: false })
+const lockPage = '/lock' //锁屏页
+const urlParams = getUrlParams(window.location.href)
+
+router.beforeEach((to, from, next) => {
+  const meta = to.meta || {}
+  const isMenu = meta.menu === undefined ? to.query.menu : meta.menu
+  store.commit('SET_IS_MENU', isMenu === undefined)
+  if (getToken()) {
+    if (store.getters.isLock && to.path !== lockPage) {
+      //如果系统激活锁屏,全部跳转到锁屏页
+      next({ path: lockPage })
+    } else if (to.path === '/login') {
+      //如果登录成功访问登录页跳转到主页
+      next({ path: '/' })
+    } else {
+      const systemToken = store.getters.token || urlParams?.token
+      if (systemToken === 0) {
+        store.dispatch('FedLogOut').then(() => {
+          next({ path: '/login' })
+        })
+      } else {
+        const meta = to.meta || {}
+        const query = to.query || {}
+        if (meta.target) {
+          window.open(query.url.replace(/#/g, '&'))
+          return
+        } else if (meta.isTab !== false) {
+          store.commit('ADD_TAG', {
+            name: query.name || to.name,
+            path: to.path,
+            fullPath: to.path,
+            params: to.params,
+            query: to.query,
+            meta: meta,
+          })
+        }
+        next()
+      }
+    }
+  } else {
+    //判断是否需要认证,没有登录访问去登录页
+    if (meta.isAuth === false) {
+      next()
+    } else {
+      next('/login')
+    }
+  }
+})
+
+router.afterEach(to => {
+  NProgress.done()
+  let title = router.$avueRouter.generateTitle(to, { label: 'name' })
+  router.$avueRouter.setTitle(title)
+  store.commit('SET_IS_SEARCH', false)
+})
diff --git a/src/permission copy.js b/src/permission copy.js
new file mode 100644
index 0000000..c244bec
--- /dev/null
+++ b/src/permission copy.js
@@ -0,0 +1,133 @@
+/*
+ * @Author: GuLiMmo 2820890765@qq.com
+ * @Date: 2024-08-23 10:51:53
+ * @LastEditors: GuLiMmo 2820890765@qq.com
+ * @LastEditTime: 2024-08-29 14:10:56
+ * @FilePath: /drone-web-manage/src/permission.js
+ * @Description:
+ * Copyright (c) 2024 by GuLiMmo, All Rights Reserved.
+ */
+
+import { getStore } from '@/utils/store'
+import router from './router/'
+import store from './store'
+import { getToken } from '@/utils/auth'
+import { getUrlParams } from './utils/validate'
+import NProgress from 'nprogress' // progress bar
+import 'nprogress/nprogress.css' // progress bar style
+NProgress.configure({ showSpinner: false })
+const lockPage = '/lock' //锁屏页
+const urlParams = getUrlParams(window.location.href)
+
+function findRouteByPath (routes, targetPath) {
+  // 遍历数组中的每个路由对象
+  for (const route of routes) {
+    // 如果当前路由的path匹配目标路径,直接返回
+    if (route.path === targetPath) {
+      return route
+    }
+
+    // 如果有子路由,递归查找
+    if (route.children && route.children.length > 0) {
+      const foundInChildren = findRouteByPath(route.children, targetPath)
+      if (foundInChildren) {
+        return foundInChildren
+      }
+    }
+  }
+
+  // 遍历完所有路由都没找到,返回null
+  return null
+}
+
+router.beforeEach((to, from, next) => {
+  const meta = to.meta || {}
+  const isMenu = meta.menu === undefined ? to.query.menu : meta.menu
+  store.commit('SET_IS_MENU', isMenu === undefined)
+
+  const menuAll = getStore({ name: 'menuAll' })
+
+  if (!menuAll) {
+    store.dispatch('GetMenu').then(data => {
+      if (data.length !== 0) {
+        router.$avueRouter.formatRoutes(data, true)
+
+        let newMenu = getStore({ name: 'menuAll' })
+
+        let firstMenu = newMenu[0]
+
+        let toMenu = findRouteByPath(newMenu, to.path)
+
+        store.commit('ADD_TAG', {
+          name: firstMenu.name,
+          path: firstMenu.path,
+          fullPath: firstMenu.path,
+          params: firstMenu.params || {},
+          query: firstMenu.query || {},
+          meta: firstMenu.meta || {},
+        })
+
+        store.commit('ADD_TAG', {
+          name: toMenu.name,
+          path: toMenu.path,
+          fullPath: toMenu.path,
+          params: toMenu.params || {},
+          query: toMenu.query || {},
+          meta: toMenu.meta || {},
+        })
+
+        next(to.fullPath)
+      }
+    })
+
+    return
+  }
+
+  if (getToken()) {
+    if (store.getters.isLock && to.path !== lockPage) {
+      //如果系统激活锁屏,全部跳转到锁屏页
+      next({ path: lockPage })
+    } else if (to.path === '/login') {
+      //如果登录成功访问登录页跳转到主页
+      next({ path: '/' })
+    } else {
+      const systemToken = store.getters.token || urlParams?.token
+      if (systemToken === 0) {
+        store.dispatch('FedLogOut').then(() => {
+          next({ path: '/login' })
+        })
+      } else {
+        const meta = to.meta || {}
+        const query = to.query || {}
+        if (meta.target) {
+          window.open(query.url.replace(/#/g, '&'))
+          return
+        } else if (meta.isTab !== false) {
+          store.commit('ADD_TAG', {
+            name: query.name || to.name,
+            path: to.path,
+            fullPath: to.path,
+            params: to.params,
+            query: to.query,
+            meta: meta,
+          })
+        }
+        next()
+      }
+    }
+  } else {
+    //判断是否需要认证,没有登录访问去登录页
+    if (meta.isAuth === false) {
+      next()
+    } else {
+      next('/login')
+    }
+  }
+})
+
+router.afterEach(to => {
+  NProgress.done()
+  let title = router.$avueRouter.generateTitle(to, { label: 'name' })
+  router.$avueRouter.setTitle(title)
+  store.commit('SET_IS_SEARCH', false)
+})
diff --git a/src/permission.js b/src/permission.js
index 821f3dc..c244bec 100644
--- a/src/permission.js
+++ b/src/permission.js
@@ -7,6 +7,8 @@
  * @Description:
  * Copyright (c) 2024 by GuLiMmo, All Rights Reserved.
  */
+
+import { getStore } from '@/utils/store'
 import router from './router/'
 import store from './store'
 import { getToken } from '@/utils/auth'
@@ -17,10 +19,70 @@
 const lockPage = '/lock' //锁屏页
 const urlParams = getUrlParams(window.location.href)
 
+function findRouteByPath (routes, targetPath) {
+  // 遍历数组中的每个路由对象
+  for (const route of routes) {
+    // 如果当前路由的path匹配目标路径,直接返回
+    if (route.path === targetPath) {
+      return route
+    }
+
+    // 如果有子路由,递归查找
+    if (route.children && route.children.length > 0) {
+      const foundInChildren = findRouteByPath(route.children, targetPath)
+      if (foundInChildren) {
+        return foundInChildren
+      }
+    }
+  }
+
+  // 遍历完所有路由都没找到,返回null
+  return null
+}
+
 router.beforeEach((to, from, next) => {
   const meta = to.meta || {}
   const isMenu = meta.menu === undefined ? to.query.menu : meta.menu
   store.commit('SET_IS_MENU', isMenu === undefined)
+
+  const menuAll = getStore({ name: 'menuAll' })
+
+  if (!menuAll) {
+    store.dispatch('GetMenu').then(data => {
+      if (data.length !== 0) {
+        router.$avueRouter.formatRoutes(data, true)
+
+        let newMenu = getStore({ name: 'menuAll' })
+
+        let firstMenu = newMenu[0]
+
+        let toMenu = findRouteByPath(newMenu, to.path)
+
+        store.commit('ADD_TAG', {
+          name: firstMenu.name,
+          path: firstMenu.path,
+          fullPath: firstMenu.path,
+          params: firstMenu.params || {},
+          query: firstMenu.query || {},
+          meta: firstMenu.meta || {},
+        })
+
+        store.commit('ADD_TAG', {
+          name: toMenu.name,
+          path: toMenu.path,
+          fullPath: toMenu.path,
+          params: toMenu.params || {},
+          query: toMenu.query || {},
+          meta: toMenu.meta || {},
+        })
+
+        next(to.fullPath)
+      }
+    })
+
+    return
+  }
+
   if (getToken()) {
     if (store.getters.isLock && to.path !== lockPage) {
       //如果系统激活锁屏,全部跳转到锁屏页

--
Gitblit v1.9.3