Newer
Older
HuangJiPC / src / utils / hasPermission.js
@zhangdeliang zhangdeliang on 21 Jun 1 KB update
export default (Vue) => {
    /**自定义按钮权限指令 */
    Vue.directive('has', {
        mounted(el, binding) {
            console.log(el, binding)
            //获取按钮权限
            if (!Vue.config.globalProperties.$_has(binding.value)) {
                //移除不匹配的按钮
                el.parentNode.removeChild(el)
            }
        },
    })

    //检查权限方法
    Vue.config.globalProperties.$_has = function (value) {
        let isExist = false

        // var btnPermsArr = ['user:add', 'user:edit', 'user:del'] //获取从服务器请求存储本地的按钮权限
        // 从浏览器缓存中获取权限数组(该数组在登入成功后拉取用户的权限信息时保存在浏览器的缓存中)
        var btnPermsArr = sessionStorage.getItem('powerHandle')
        if (btnPermsArr === undefined || btnPermsArr == null) {
            return false
        }
        if (btnPermsArr.includes(value)) {
            isExist = true
        }
        return isExist
    }
}