1
This commit is contained in:
17
src/directive/permission/index.js
Normal file
17
src/directive/permission/index.js
Normal file
@ -0,0 +1,17 @@
|
||||
import permission from './permission'
|
||||
import permisaction from './permisaction'
|
||||
|
||||
const install = function(Vue) {
|
||||
Vue.directive('permission', permission)
|
||||
Vue.directive('permisaction', permisaction)
|
||||
}
|
||||
|
||||
if (window.Vue) {
|
||||
window['permission'] = permission
|
||||
window['permisaction'] = permisaction
|
||||
// eslint-disable-next-line no-undef
|
||||
Vue.use(install)
|
||||
}
|
||||
|
||||
permission.install = install
|
||||
export default permission
|
||||
23
src/directive/permission/permisaction.js
Normal file
23
src/directive/permission/permisaction.js
Normal file
@ -0,0 +1,23 @@
|
||||
import store from '@/store'
|
||||
|
||||
export default {
|
||||
inserted(el, binding, vnode) {
|
||||
const { value } = binding
|
||||
const all_permission = '*:*:*'
|
||||
const permissions = store.getters && store.getters.permisaction
|
||||
|
||||
if (value && value instanceof Array && value.length > 0) {
|
||||
const permissionFlag = value
|
||||
|
||||
const hasPermissions = permissions.some(permission => {
|
||||
return all_permission === permission || permissionFlag.includes(permission)
|
||||
})
|
||||
|
||||
if (!hasPermissions) {
|
||||
el.parentNode && el.parentNode.removeChild(el)
|
||||
}
|
||||
} else {
|
||||
throw new Error(`请设置操作权限标签值`)
|
||||
}
|
||||
}
|
||||
}
|
||||
22
src/directive/permission/permission.js
Normal file
22
src/directive/permission/permission.js
Normal file
@ -0,0 +1,22 @@
|
||||
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']"`)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user