Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
V
VueElementTemplate
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
黄瑜
VueElementTemplate
Commits
54acf1e0
Commit
54acf1e0
authored
Jan 05, 2018
by
Pan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
perf[permission]:set role => roles
parent
de08e49f
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
28 additions
and
25 deletions
+28
-25
login.js
src/mock/login.js
+2
-2
permission.js
src/permission.js
+13
-10
index.js
src/router/index.js
+4
-4
permission.js
src/store/modules/permission.js
+2
-2
user.js
src/store/modules/user.js
+3
-3
index.vue
src/views/permission/index.vue
+4
-4
No files found.
src/mock/login.js
View file @
54acf1e0
...
@@ -2,14 +2,14 @@ import { param2Obj } from '@/utils'
...
@@ -2,14 +2,14 @@ import { param2Obj } from '@/utils'
const
userMap
=
{
const
userMap
=
{
admin
:
{
admin
:
{
role
:
[
'admin'
],
role
s
:
[
'admin'
],
token
:
'admin'
,
token
:
'admin'
,
introduction
:
'我是超级管理员'
,
introduction
:
'我是超级管理员'
,
avatar
:
'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif'
,
avatar
:
'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif'
,
name
:
'Super Admin'
name
:
'Super Admin'
},
},
editor
:
{
editor
:
{
role
:
[
'editor'
],
role
s
:
[
'editor'
],
token
:
'editor'
,
token
:
'editor'
,
introduction
:
'我是编辑'
,
introduction
:
'我是编辑'
,
avatar
:
'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif'
,
avatar
:
'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif'
,
...
...
src/permission.js
View file @
54acf1e0
import
router
from
'./router'
import
router
from
'./router'
import
store
from
'./store'
import
store
from
'./store'
import
{
Message
}
from
'element-ui'
import
NProgress
from
'nprogress'
// progress bar
import
NProgress
from
'nprogress'
// progress bar
import
'nprogress/nprogress.css'
// progress bar style
import
'nprogress/nprogress.css'
// progress bar style
import
{
getToken
}
from
'@/utils/auth'
// getToken from cookie
import
{
getToken
}
from
'@/utils/auth'
// getToken from cookie
import
{
Message
}
from
'element-ui'
// permissiom judge
NProgress
.
configure
({
showSpinner
:
false
})
// NProgress Configuration
// permissiom judge function
function
hasPermission
(
roles
,
permissionRoles
)
{
function
hasPermission
(
roles
,
permissionRoles
)
{
if
(
roles
.
indexOf
(
'admin'
)
>=
0
)
return
true
// admin permission passed directly
if
(
roles
.
indexOf
(
'admin'
)
>=
0
)
return
true
// admin permission passed directly
if
(
!
permissionRoles
)
return
true
if
(
!
permissionRoles
)
return
true
...
@@ -16,15 +18,16 @@ const whiteList = ['/login', '/authredirect']// no redirect whitelist
...
@@ -16,15 +18,16 @@ const whiteList = ['/login', '/authredirect']// no redirect whitelist
router
.
beforeEach
((
to
,
from
,
next
)
=>
{
router
.
beforeEach
((
to
,
from
,
next
)
=>
{
NProgress
.
start
()
// start progress bar
NProgress
.
start
()
// start progress bar
if
(
getToken
())
{
// 判断是否有token
if
(
getToken
())
{
// determine if there has token
/* has token*/
if
(
to
.
path
===
'/login'
)
{
if
(
to
.
path
===
'/login'
)
{
next
({
path
:
'/'
})
next
({
path
:
'/'
})
NProgress
.
done
()
//
router在hash模式下 手动改变hash 重定向回来 不会触发afterEach 暂时hack方案 ps:history模式下无问题,可删除该行!
NProgress
.
done
()
//
if current page is dashboard will not trigger afterEach hook, so manually handle it
}
else
{
}
else
{
if
(
store
.
getters
.
roles
.
length
===
0
)
{
// 判断当前用户是否已拉取完user_info信息
if
(
store
.
getters
.
roles
.
length
===
0
)
{
// 判断当前用户是否已拉取完user_info信息
store
.
dispatch
(
'GetUserInfo'
).
then
(
res
=>
{
// 拉取user_info
store
.
dispatch
(
'GetUserInfo'
).
then
(
res
=>
{
// 拉取user_info
const
roles
=
res
.
data
.
role
const
roles
=
res
.
data
.
role
s
// note: roles must be a array! such as: ['editor','develop']
store
.
dispatch
(
'GenerateRoutes'
,
{
roles
}).
then
(()
=>
{
// 生成可访问的路由表
store
.
dispatch
(
'GenerateRoutes'
,
{
roles
}).
then
(()
=>
{
//
根据roles权限
生成可访问的路由表
router
.
addRoutes
(
store
.
getters
.
addRouters
)
// 动态添加可访问路由表
router
.
addRoutes
(
store
.
getters
.
addRouters
)
// 动态添加可访问路由表
next
({
...
to
,
replace
:
true
})
// hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
next
({
...
to
,
replace
:
true
})
// hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
})
})
...
@@ -36,21 +39,21 @@ router.beforeEach((to, from, next) => {
...
@@ -36,21 +39,21 @@ router.beforeEach((to, from, next) => {
})
})
}
else
{
}
else
{
// 没有动态改变权限的需求可直接next() 删除下方权限判断 ↓
// 没有动态改变权限的需求可直接next() 删除下方权限判断 ↓
if
(
hasPermission
(
store
.
getters
.
roles
,
to
.
meta
.
role
))
{
if
(
hasPermission
(
store
.
getters
.
roles
,
to
.
meta
.
role
s
))
{
next
()
//
next
()
//
}
else
{
}
else
{
next
({
path
:
'/401'
,
query
:
{
noGoBack
:
true
}})
next
({
path
:
'/401'
,
replace
:
true
,
query
:
{
noGoBack
:
true
}})
NProgress
.
done
()
// router在hash模式下 手动改变hash 重定向回来 不会触发afterEach 暂时hack方案 ps:history模式下无问题,可删除该行!
}
}
// 可删 ↑
// 可删 ↑
}
}
}
}
}
else
{
}
else
{
/* has no token*/
if
(
whiteList
.
indexOf
(
to
.
path
)
!==
-
1
)
{
// 在免登录白名单,直接进入
if
(
whiteList
.
indexOf
(
to
.
path
)
!==
-
1
)
{
// 在免登录白名单,直接进入
next
()
next
()
}
else
{
}
else
{
next
(
'/login'
)
// 否则全部重定向到登录页
next
(
'/login'
)
// 否则全部重定向到登录页
NProgress
.
done
()
//
router在hash模式下 手动改变hash 重定向回来 不会触发afterEach 暂时hack方案 ps:history模式下无问题,可删除该行!
NProgress
.
done
()
//
if current page is login will not trigger afterEach hook, so manually handle it
}
}
}
}
})
})
...
...
src/router/index.js
View file @
54acf1e0
...
@@ -18,7 +18,7 @@ import Layout from '../views/layout/Layout'
...
@@ -18,7 +18,7 @@ import Layout from '../views/layout/Layout'
* redirect: noredirect if `redirect:noredirect` will no redirct in the breadcrumb
* redirect: noredirect if `redirect:noredirect` will no redirct in the breadcrumb
* name:'router-name' the name is used by <keep-alive> (must set!!!)
* name:'router-name' the name is used by <keep-alive> (must set!!!)
* meta : {
* meta : {
role
: ['admin','editor'] will control the page role
(you can set multiple roles)
role
s: ['admin','editor'] will control the page roles
(you can set multiple roles)
title: 'title' the name show in submenu and breadcrumb (recommend set)
title: 'title' the name show in submenu and breadcrumb (recommend set)
icon: 'svg-name' the icon show in the sidebar,
icon: 'svg-name' the icon show in the sidebar,
noCache: true if fasle ,the page will no be cached(default is false)
noCache: true if fasle ,the page will no be cached(default is false)
...
@@ -54,7 +54,7 @@ export const constantRouterMap = [
...
@@ -54,7 +54,7 @@ export const constantRouterMap = [
]
]
export
default
new
Router
({
export
default
new
Router
({
// mode: 'history', //
后端支持可开
// mode: 'history', //
require service support
scrollBehavior
:
()
=>
({
y
:
0
}),
scrollBehavior
:
()
=>
({
y
:
0
}),
routes
:
constantRouterMap
routes
:
constantRouterMap
})
})
...
@@ -64,7 +64,7 @@ export const asyncRouterMap = [
...
@@ -64,7 +64,7 @@ export const asyncRouterMap = [
path
:
'/permission'
,
path
:
'/permission'
,
component
:
Layout
,
component
:
Layout
,
redirect
:
'/permission/index'
,
redirect
:
'/permission/index'
,
meta
:
{
role
:
[
'admin'
]
},
meta
:
{
role
s
:
[
'admin'
]
},
// you can set roles in root nav
children
:
[{
children
:
[{
path
:
'index'
,
path
:
'index'
,
component
:
_import
(
'permission/index'
),
component
:
_import
(
'permission/index'
),
...
@@ -72,7 +72,7 @@ export const asyncRouterMap = [
...
@@ -72,7 +72,7 @@ export const asyncRouterMap = [
meta
:
{
meta
:
{
title
:
'permission'
,
title
:
'permission'
,
icon
:
'lock'
,
icon
:
'lock'
,
role
:
[
'admin'
]
role
s
:
[
'admin'
]
// or you can only set roles in sub nav
}
}
}]
}]
},
},
...
...
src/store/modules/permission.js
View file @
54acf1e0
...
@@ -6,8 +6,8 @@ import { asyncRouterMap, constantRouterMap } from '@/router'
...
@@ -6,8 +6,8 @@ import { asyncRouterMap, constantRouterMap } from '@/router'
* @param route
* @param route
*/
*/
function
hasPermission
(
roles
,
route
)
{
function
hasPermission
(
roles
,
route
)
{
if
(
route
.
meta
&&
route
.
meta
.
role
)
{
if
(
route
.
meta
&&
route
.
meta
.
role
s
)
{
return
roles
.
some
(
role
=>
route
.
meta
.
role
.
indexOf
(
role
)
>=
0
)
return
roles
.
some
(
role
=>
route
.
meta
.
role
s
.
indexOf
(
role
)
>=
0
)
}
else
{
}
else
{
return
true
return
true
}
}
...
...
src/store/modules/user.js
View file @
54acf1e0
...
@@ -67,7 +67,7 @@ const user = {
...
@@ -67,7 +67,7 @@ const user = {
reject
(
'error'
)
reject
(
'error'
)
}
}
const
data
=
response
.
data
const
data
=
response
.
data
commit
(
'SET_ROLES'
,
data
.
role
)
commit
(
'SET_ROLES'
,
data
.
role
s
)
commit
(
'SET_NAME'
,
data
.
name
)
commit
(
'SET_NAME'
,
data
.
name
)
commit
(
'SET_AVATAR'
,
data
.
avatar
)
commit
(
'SET_AVATAR'
,
data
.
avatar
)
commit
(
'SET_INTRODUCTION'
,
data
.
introduction
)
commit
(
'SET_INTRODUCTION'
,
data
.
introduction
)
...
@@ -116,13 +116,13 @@ const user = {
...
@@ -116,13 +116,13 @@ const user = {
},
},
// 动态修改权限
// 动态修改权限
ChangeRole
({
commit
},
role
)
{
ChangeRole
s
({
commit
},
role
)
{
return
new
Promise
(
resolve
=>
{
return
new
Promise
(
resolve
=>
{
commit
(
'SET_TOKEN'
,
role
)
commit
(
'SET_TOKEN'
,
role
)
setToken
(
role
)
setToken
(
role
)
getUserInfo
(
role
).
then
(
response
=>
{
getUserInfo
(
role
).
then
(
response
=>
{
const
data
=
response
.
data
const
data
=
response
.
data
commit
(
'SET_ROLES'
,
data
.
role
)
commit
(
'SET_ROLES'
,
data
.
role
s
)
commit
(
'SET_NAME'
,
data
.
name
)
commit
(
'SET_NAME'
,
data
.
name
)
commit
(
'SET_AVATAR'
,
data
.
avatar
)
commit
(
'SET_AVATAR'
,
data
.
avatar
)
commit
(
'SET_INTRODUCTION'
,
data
.
introduction
)
commit
(
'SET_INTRODUCTION'
,
data
.
introduction
)
...
...
src/views/permission/index.vue
View file @
54acf1e0
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
<div
class=
"app-container"
>
<div
class=
"app-container"
>
<div
style=
"margin-bottom:15px;"
>
{{
$t
(
'permission.roles'
)
}}
:
{{
roles
}}
</div>
<div
style=
"margin-bottom:15px;"
>
{{
$t
(
'permission.roles'
)
}}
:
{{
roles
}}
</div>
{{
$t
(
'permission.switchRoles'
)
}}
:
{{
$t
(
'permission.switchRoles'
)
}}
:
<el-radio-group
v-model=
"
role
"
>
<el-radio-group
v-model=
"
switchRoles
"
>
<el-radio-button
label=
"editor"
></el-radio-button>
<el-radio-button
label=
"editor"
></el-radio-button>
</el-radio-group>
</el-radio-group>
</div>
</div>
...
@@ -15,7 +15,7 @@ export default{
...
@@ -15,7 +15,7 @@ export default{
name
:
'permission'
,
name
:
'permission'
,
data
()
{
data
()
{
return
{
return
{
role
:
''
switchRoles
:
''
}
}
},
},
computed
:
{
computed
:
{
...
@@ -24,8 +24,8 @@ export default{
...
@@ -24,8 +24,8 @@ export default{
])
])
},
},
watch
:
{
watch
:
{
role
(
val
)
{
switchRoles
(
val
)
{
this
.
$store
.
dispatch
(
'ChangeRole'
,
val
).
then
(()
=>
{
this
.
$store
.
dispatch
(
'ChangeRole
s
'
,
val
).
then
(()
=>
{
this
.
$router
.
push
({
path
:
'/permission/index?'
+
+
new
Date
()
})
this
.
$router
.
push
({
path
:
'/permission/index?'
+
+
new
Date
()
})
})
})
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment