@@ -8,13 +8,22 @@ import getPageTitle from '@/utils/get-page-title'
8
8
9
9
NProgress . configure ( { showSpinner : false } ) // NProgress Configuration
10
10
// todo permission,博客相对后台管理系统只需要管理部分的路由即可
11
- const whiteList = [ '/login' , '/article/list' , '/auth-redirect' , '/login/github' ] // no redirect whitelist
11
+ // const whiteList = ['/login', '/article/list', '/login/github', '/article/detail'] // no redirect whitelist
12
+
13
+ function checkPermission ( roles , to ) {
14
+ console . info ( "check permission" )
15
+ const permittedRoles = to . meta . roles
16
+ let accessRoles = roles . filter ( function ( v ) {
17
+ permittedRoles . indexOf ( v ) > - 1
18
+ } )
19
+ return accessRoles . length > 0
20
+ }
12
21
13
22
router . beforeEach ( async ( to , from , next ) => {
14
23
// start progress bar
15
24
NProgress . start ( )
16
- // console.log("before route change")
17
- // console.log(to.path)
25
+ console . log ( "before route change" )
26
+ console . log ( to . path )
18
27
// set page title
19
28
document . title = getPageTitle ( to . meta . title )
20
29
// console.info(to)
@@ -28,83 +37,74 @@ router.beforeEach(async (to, from, next) => {
28
37
delete to . query . code
29
38
delete to . query . thirdPart
30
39
// console.info("begin to social auth login")
31
- await store . dispatch ( 'user/socialLogin' , { 'thirdPart' : thirdPart , 'oauthCode' : oauthCode } )
40
+ await store . dispatch ( 'user/socialLogin' , { 'thirdPart' : thirdPart , 'oauthCode' : oauthCode } )
32
41
// console.info("social login done")
33
42
} catch ( error ) {
34
43
// remove token and go to login page to re-login
35
44
await store . dispatch ( 'user/resetToken' )
36
- ElMessage . error ( 'Error in login by ' , thirdPart )
45
+ ElMessage . error ( 'Error in login by ' , thirdPart )
37
46
next ( { ...to , replace : true } )
38
47
NProgress . done ( )
39
48
}
40
49
41
50
}
42
51
43
52
// determine whether the user has logged in
53
+ console . info ( "check if hastoken" )
54
+ let hasPagePermission = false
44
55
const hasAccessToken = getAccessToken ( )
45
56
if ( hasAccessToken ) {
46
- console . log ( "has access token:" , hasAccessToken )
47
- // TODO 添加对accessToken的过期检查和refresh
48
- // fixme change to check permissions
57
+ console . info ( "hastoken" )
49
58
50
- if ( to . path === '/login' ) {
51
- // if is logged in, redirect to the home page
52
- const next_path = to . query && to . query . redirect ? to . query . redirect : '/'
53
- next ( { path : next_path } )
54
- NProgress . done ( ) // hack: https://github.com/PanJiaChen/vue-element-admin/pull/2939
55
- } else {
56
- // determine whether the user has obtained his permission roles through getInfo
57
- console . log ( "check is has roles" )
58
- // console.log(store.getters.roles)
59
- // note Ctrl + F5强制刷新界面,对store有啥影响吗,为啥会导致没有roles,重新获取Info??
60
- // note: 因为store是存在内存中的,所以每次刷新就会判断为空,需要重新获取数据,而cookie保存在本地,所以刷新不会丢失
61
- const hasRoles = store . getters . roles && store . getters . roles . length > 0
62
- if ( hasRoles ) {
63
- console . log ( "yes has roles" )
64
- next ( )
65
- console . log ( 'next done' )
66
- } else {
67
- console . log ( 'can not get roles from store' )
68
- try {
69
- // get user info
70
- // question: roles must be a object array! such as: ['admin'] or ,['developer','editor']
71
- // question 这里的roles是user/GetInfo如何反馈的,如何只反馈roles的
72
- // note: await 's result and 对象解构赋值
73
- const { roles } = await store . dispatch ( 'user/getInfo' )
74
- console . log ( roles )
75
- // // generate accessible routes map based on roles
76
- // const accessRoutes = await store.dispatch('permission/generateRoutes', roles)
77
- //
78
- // // dynamically add accessible routes
79
- // router.addRoutes(accessRoutes)
59
+ let roles = store . getters . roles && store . getters . roles . length > 0
60
+ if ( roles ) {
61
+ console . info ( "has roles" )
80
62
81
- // hack method to ensure that addRoutes is complete
82
- // set the replace: true, so the navigation will not leave a history record
83
- next ( { ...to , replace : true } )
84
- } catch ( error ) {
85
- // remove token and go to login page to re-login
86
- await store . dispatch ( 'user/resetToken' )
87
- ElMessage . error ( error || 'Has Error' )
88
- next ( `/login?redirect=${ to . path } ` )
89
- NProgress . done ( )
90
- }
63
+ hasPagePermission = checkPermission ( roles , to )
64
+ } else {
65
+ // 对页面进行刷新后重新获取下info
66
+ try {
67
+ // get user info
68
+ // question 这里的roles是user/GetInfo如何反馈的,如何只反馈roles的
69
+ // note: await 's result and 对象解构赋值
70
+ let { roles} = await store . dispatch ( 'user/getInfo' )
71
+ hasPagePermission = checkPermission ( roles , to )
72
+ next ( { ...to , replace : true } )
73
+ } catch ( error ) {
74
+ // remove token and go to login page to re-login
75
+ await store . dispatch ( 'user/resetToken' )
76
+ ElMessage . error ( error || 'Has Error' )
77
+ next ( { ...from , replace : true } )
78
+ // NProgress.done()
91
79
}
92
80
}
81
+
93
82
} else {
94
83
/* has no token*/
84
+ let roles = [ 'anonymous' ]
85
+ hasPagePermission = checkPermission ( roles , to )
86
+
87
+ }
88
+ console . info ( "check permission done" )
89
+ console . log ( hasPagePermission )
90
+ if ( hasPagePermission ) {
91
+ console . info ( "has permission done" )
92
+
93
+ next ( { ...to , replace : true } )
94
+ // NProgress.done()
95
+
96
+ } else {
97
+ ElMessage . error ( "Access denied" )
98
+ console . info ( "access denied permission done" )
99
+
100
+ next ( { ...from , replace : true } )
101
+ // NProgress.done()
95
102
96
- if ( whiteList . indexOf ( to . path ) !== - 1 ) {
97
- // in the free login whitelist, go directly
98
- next ( )
99
- } else {
100
- // other pages that do not have permission to access are redirected to the login page.
101
- next ( `/login?redirect=${ to . path } ` )
102
- NProgress . done ( )
103
- }
104
103
}
105
104
} )
106
105
107
106
router . afterEach ( ( ) => {
108
107
// finish progress bar
109
108
NProgress . done ( )
110
109
} )
110
+
0 commit comments