add v-permission

木子李·De 3 years ago
parent a73dc0f934
commit 2795564fd5

@ -1,14 +1,10 @@
import store from '@/store'
import { hasPermission } from '@/utils/auth'
export default {
inserted(el, binding, vnode) {
const { value } = binding // v-permission 绑定的权限值
const auths = store.getters && store.getters.authorities // 拥有的权限列表
if (value) {
//auths.indexOf(value) === -1 && el.parentNode && el.parentNode.removeChild(el)
} else {
// 未绑定权限值或者绑定的权限值为 假
return
var checkFlag = hasPermission(value)
if(checkFlag == false && el.parentNode ){
el.parentNode.removeChild(el)
}
},
}

@ -1,4 +1,4 @@
import { getToken, getAccessUser } from '@/utils/auth'
import { getToken, getAccessUser, hasPermission } from '@/utils/auth'
export default {
data () {
return {
@ -34,28 +34,7 @@ export default {
},
methods: {
hasPermission (permissionStr) {
if (permissionStr == null || permissionStr.length == 0) {
return true
}
// 登录用户权限列表
if (this.opAuthorities == null) {
return false
}
// 用户有的全部权限码
var opAuthoritiesStr = JSON.stringify(this.opAuthorities)
// permissionStr可能是authorityManage、authorityManage:insert、authorityManage:insert|authorityManage:update
var needPermissionArray = permissionStr.split('|')
for (var i = 0; i < needPermissionArray.length; i++) {
// 只要有其中的一个权限就返回true
var needPermission = needPermissionArray[i] // authorityManage、authorityManage:insert
needPermission = needPermission.replace(/\ /g, "") // 去除authorityManage : insert中:前后的空格
if(opAuthoritiesStr.indexOf(needPermission)>=0){
return true
}
}
return false
return hasPermission(permissionStr)
},
}
}

@ -22,3 +22,31 @@ export function setAccessUser(accessUser) {
export function delAccessUser() {
return delStorageItem(AccessUserKey)
}
// 获取当前用户的所有的权限码,判断是否有
export function hasPermission(permissionStr) {
// 不需要权限
if (permissionStr == null || permissionStr.length == 0) {
return true
}
// 登录用户的全部权限码
var user = getAccessUser()
if (user == null || user.authorities == null) {
return false
}
var opAuthoritiesStr = JSON.stringify(user.authorities)
// permissionStr可能是authorityManage、authorityManage:insert、authorityManage:insert|authorityManage:update
var needPermissionArray = permissionStr.split('|')
for (var i = 0; i < needPermissionArray.length; i++) {
// 只要有其中的一个权限就返回true
var needPermission = needPermissionArray[i] // authorityManage、authorityManage:insert
needPermission = needPermission.replace(/\ /g, "") // 去除authorityManage : insert中:前后的空格
if(opAuthoritiesStr.indexOf(needPermission)>=0){
return true
}
}
return false
}

Loading…
Cancel
Save