无人机管理后台前端(已迁走)
张含笑
2025-09-01 2ca94de8ede18ac07ccfd8dec7b6f6a707adde9b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * @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)
const adminUrl = import.meta.env.VITE_APP_DASHBOARD_URL
const env = import.meta.env.VITE_APP_ENV
 
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 (getToken()) {
    if (!menuAll?.length) {
      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 (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' })
        })
        // store.dispatch('FedLogOut').then(() =>  window.location.replace(`${adminUrl}#/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 {
      env === 'development' ? next('/login') : window.location.replace(`${adminUrl}#/login`)
    }
  }
})
 
// router.afterEach(to => {
//   NProgress.done()
//   let title = router.$avueRouter.generateTitle(to, { label: 'name' })
//   router.$avueRouter.setTitle(title)
//   store.commit('SET_IS_SEARCH', false)
// })
router.afterEach(to => {
    NProgress.done()
    const isLoginPage = to.path === '/login'
    // 根据是否登录页设置标题
    const pageTitle = isLoginPage ? '中图智飞低空感知网平台' : '综合管理平台'
    document.title = pageTitle
    store.commit('SET_IS_SEARCH', false)
})