inleft
2024-01-10 f0beb464c1fd4cf1c6481f86610a6a130b93ead4
commit | author | age
9bcb19 1 import Vue from 'vue'
I 2 import router from './router'
3 import store from './store'
4
5 import NProgress from 'nprogress' // progress bar
6 import '@/components/NProgress/nprogress.less' // progress bar custom style
7 import { setDocumentTitle, domTitle } from '@/utils/domUtil'
8 import { ACCESS_TOKEN, ALL_APPS_MENU } from '@/store/mutation-types'
9
10 import { Modal, notification } from 'ant-design-vue' // NProgress Configuration
11 import { timeFix } from '@/utils/util'/// es/notification
12 NProgress.configure({ showSpinner: false })
13 const whiteList = ['login', 'register', 'registerResult'] // no redirect whitelist
14 // 无默认首页的情况
15 const defaultRoutePath = '/welcome'
16
17 router.beforeEach((to, from, next) => {
18   NProgress.start() // start progress bar
19   to.meta && (typeof to.meta.title !== 'undefined' && setDocumentTitle(`${to.meta.title} - ${domTitle}`))
20   if (Vue.ls.get(ACCESS_TOKEN)) {
21     /* has token */
22     if (to.path === '/user/login') {
23       next({ path: defaultRoutePath })
24       NProgress.done()
25     } else {
26       if (store.getters.roles.length === 0) {
27         store
28           .dispatch('GetInfo')
29           .then(res => {
30             if (res.menus.length < 1) {
31               Modal.error({
32                 title: '提示:',
33                 content: '无菜单权限,请联系管理员',
34                 okText: '确定',
35                 onOk: () => {
36                   store.dispatch('Logout').then(() => {
37                     window.location.reload()
38                   })
39                 }
40               })
41               return
42             }
43             // eslint-disable-next-line camelcase
44             const all_app_menu = Vue.ls.get(ALL_APPS_MENU)
45             let antDesignmenus
46             // eslint-disable-next-line camelcase
47             if (all_app_menu == null) {
48               const applocation = []
49               res.apps.forEach(item => {
50                 const apps = { 'code': '', 'name': '', 'active': '', 'menu': '' }
51                 if (item.active) {
52                   apps.code = item.code
53                   apps.name = item.name
54                   apps.active = item.active
55                   apps.menu = res.menus
56                   antDesignmenus = res.menus
57                 } else {
58                   apps.code = item.code
59                   apps.name = item.name
60                   apps.active = item.active
61                   apps.menu = ''
62                 }
63                 applocation.push(apps)
64               })
65               Vue.ls.set(ALL_APPS_MENU, applocation, 7 * 24 * 60 * 60 * 1000)
66               // 延迟 1 秒显示欢迎信息
67               setTimeout(() => {
68                 notification.success({
69                   message: '欢迎',
70                   description: `${timeFix()},欢迎回来`
71                 })
72               }, 1000)
73             } else {
74               antDesignmenus = Vue.ls.get(ALL_APPS_MENU)[0].menu
75             }
76             store.dispatch('GenerateRoutes', { antDesignmenus }).then(() => {
77               // 动态添加可访问路由表
78               router.addRoutes(store.getters.addRouters)
79               // 请求带有 redirect 重定向时,登录自动重定向到该地址
80               const redirect = decodeURIComponent(from.query.redirect || to.path)
81               if (to.path === redirect) {
82                 next({ path: redirect })
83                 // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
84                 next({ ...to, replace: true })
85               } else {
86                 // 跳转到目的路由
87                 next({ path: redirect })
88               }
89             })
90           })
91           .catch(() => {
92             store.dispatch('Logout').then(() => {
93               next({ path: '/user/login', query: { redirect: to.fullPath } })
94             })
95           })
96       } else {
97         next()
98       }
99     }
100   } else {
101     if (whiteList.includes(to.name)) {
102       // 在免登录白名单,直接进入
103       next()
104     } else {
105       next({ path: '/user/login', query: { redirect: to.fullPath } })
106       NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
107     }
108   }
109 })
110
111 router.afterEach(() => {
112   NProgress.done() // finish progress bar
113 })