-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature[permission]: add v-permission (#653)
- Loading branch information
1 parent
1e103cf
commit 597df48
Showing
9 changed files
with
163 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import permission from './permission' | ||
|
||
const install = function(Vue) { | ||
Vue.directive('permission', permission) | ||
} | ||
|
||
if (window.Vue) { | ||
window['permission'] = permission | ||
Vue.use(install); // eslint-disable-line | ||
} | ||
|
||
permission.install = install | ||
export default permission |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
import store from '@/store' | ||
|
||
export default{ | ||
inserted(el, binding, vnode) { | ||
const { value } = binding | ||
const roles = store.getters && store.getters.roles | ||
|
||
if (value && value instanceof Array && value.length > 0) { | ||
const permissionRoles = value | ||
|
||
const hasPermission = roles.some(role => { | ||
return permissionRoles.includes(role) | ||
}) | ||
|
||
if (!hasPermission) { | ||
el.parentNode && el.parentNode.removeChild(el) | ||
} | ||
} else { | ||
throw new Error(`need roles! Like v-permission="['admin','editor']"`) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<template> | ||
<div> | ||
<div style="margin-bottom:15px;">{{$t('permission.roles')}}: {{roles}}</div> | ||
{{$t('permission.switchRoles')}}: | ||
<el-radio-group v-model="switchRoles"> | ||
<el-radio-button label="editor"></el-radio-button> | ||
<el-radio-button label="admin"></el-radio-button> | ||
</el-radio-group> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
computed: { | ||
roles() { | ||
return this.$store.getters.roles | ||
}, | ||
switchRoles: { | ||
get() { | ||
return this.roles[0] | ||
}, | ||
set(val) { | ||
this.$store.dispatch('ChangeRoles', val).then(() => { | ||
this.$emit('change') | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<template> | ||
<div class="app-container"> | ||
<switch-roles @change="handleRolesChange" /> | ||
|
||
<div :key="key" style="margin-top:30px;"> | ||
<span v-permission="['admin']" class="permission-alert"> | ||
Only <el-tag class="permission-tag" size="small">admin</el-tag> can see this | ||
</span> | ||
<span v-permission="['editor']" class="permission-alert"> | ||
Only <el-tag class="permission-tag" size="small">editor</el-tag> can see this | ||
</span> | ||
<span v-permission="[]" class="permission-alert"> | ||
Both <el-tag class="permission-tag" size="small">admin</el-tag> and <el-tag class="permission-tag" size="small">editor</el-tag> can see this | ||
</span> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import permission from '@/directive/permission/index.js' // 权限判断指令 | ||
import SwitchRoles from './components/SwitchRoles' | ||
export default{ | ||
name: 'directivePermission', | ||
components: { SwitchRoles }, | ||
directives: { permission }, | ||
data() { | ||
return { | ||
key: 1 // 为了能每次切换权限的时候重新初始化指令 | ||
} | ||
}, | ||
methods: { | ||
handleRolesChange() { | ||
this.key++ | ||
} | ||
} | ||
} | ||
</script> | ||
|
||
<style rel="stylesheet/scss" lang="scss" scoped> | ||
.app-container { | ||
/deep/ .permission-alert { | ||
width: 320px; | ||
margin-top: 30px; | ||
background-color: #f0f9eb; | ||
color: #67c23a; | ||
padding: 8px 16px; | ||
border-radius: 4px; | ||
display: block; | ||
} | ||
/deep/ .permission-tag{ | ||
background-color: #ecf5ff; | ||
} | ||
} | ||
</style> | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<template> | ||
<div class="app-container"> | ||
<switch-roles @change="handleRolesChange" /> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import SwitchRoles from './components/SwitchRoles' | ||
export default{ | ||
name: 'pagePermission', | ||
components: { SwitchRoles }, | ||
methods: { | ||
handleRolesChange() { | ||
this.$router.push({ path: '/permission/index?' + +new Date() }) | ||
} | ||
} | ||
} | ||
</script> |