wangyanshen
2023-01-09 695c5a332e4bf10b6b93c9973e1634730b4a60df
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
import Vue from 'vue'
import router from './router'
import store from './store'
import NProgress from 'nprogress' // progress bar
// import '@/components/NProgress/nprogress.less' // progress bar custom style
import { setDocumentTitle, domTitle } from '@/utils/domUtil'
import { ACCESS_TOKEN, ALL_APPS_MENU, DEFAULT_ISLOGIN } from '@/store/mutation-types'
 
import { Modal, notification } from 'ant-design-vue' // NProgress Configuration
import { timeFix } from '@/utils/util'/// es/notification
NProgress.configure({ showSpinner: false })
const whiteList = ['login', 'register', 'registerResult', 'lsproductview'] // no redirect whitelist
// 无默认首页的情况
const defaultRoutePath = '/map/index'
router.beforeEach((to, from, next) => {
  NProgress.start() // start progress bar
 
  to.meta && (typeof to.meta.title !== 'undefined' && setDocumentTitle(`${to.meta.title} - ${domTitle}`))
  let isToken = true;
  if (Vue.ls.get(DEFAULT_ISLOGIN)) {
    isToken = true
  } else {
    isToken = false
  }
  if (isToken) {
    /* has token */
    if (to.path === '/user/login') {
      // next({ path: defaultRoutePath })
      NProgress.done()
    } else {
      if (store.getters.roles.length === 0) {
        store.dispatch('GetInfo').then(res => {
          if (res.menus.length < 1) {
            Modal.error({
              title: '提示:',
              content: '无菜单权限,请联系管理员',
              okText: '确定',
              onOk: () => {
                store.dispatch('Logout').then(() => {
                  window.location.reload()
                })
              }
            })
            return
          }
          // eslint-disable-next-line camelcase
          let antDesignmenus
          // eslint-disable-next-line camelcase
          Vue.ls.set(ALL_APPS_MENU, res.menus)
          // 延迟 1 秒显示欢迎信息
          setTimeout(() => {
            notification.success({
              message: '欢迎',
              description: `${timeFix()},欢迎回来`
            })
          }, 1000)
          antDesignmenus = res.menus || []
          //  console.log("后端返回的路由",antDesignmenus)
          store.dispatch('GenerateRoutes', { antDesignmenus }).then(() => {
            // 动态添加可访问路由表
            for (let x of store.getters.addRouters) {
              router.addRoute(x)
            }
            // console.log(store.getters.addRouters, router.getRoutes(), 65)
            // 请求带有 redirect 重定向时,登录自动重定向到该地址
            const redirect = decodeURIComponent(from.query.redirect || to.path)
            if (to.path === redirect) {
              next({ path: redirect })
              // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
              next({ ...to, replace: true })
            } else {
              // 跳转到目的路由
              next({ path: redirect })
            }
          })
        })
          .catch(() => {
            store.dispatch('Logout').then(() => {
              next({ path: '/user/login', query: { redirect: to.fullPath } })
            })
          })
        //store.dispatch("getNoticReceiveList").then((res)=>{});
      } else {
        next()
      }
    }
  } else {
    if (whiteList.includes(to.name)) {
      // 在免登录白名单,直接进入
      next()
    } else {
      next({ path: '/user/login', query: { redirect: to.fullPath } })
      NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
    }
  }
})
 
router.afterEach(() => {
  NProgress.done() // finish progress bar
})
 
 
/**
 * 删除菜单目录级别
 * @param {*} to 
 */
function deleteToMatched(to) {
  if (to.matched && to.matched.length > 2) {
    for (var i = 0; i < to.matched.length; i++) {
      let x = to.matched[i]
      if (x.components.default.name === "RouteView") {
        to.matched.splice(i, 1)
        deleteToMatched(to)
      }
    }
  }
}