/*
 * @Description:
 * @version: 1.0
 * @Author: 九哥
 * @Date: 2020-08-08 17:59:25
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-04-12 11:02:14
 */
import { USER_AUTH } from '@/store/mutation-types'

const hasPermission = {
  install (Vue, options) {
    Vue.directive('has', {
      inserted: (el, binding, vnode) => {
        console.log('页面权限控制----')
        // 节点权限处理，如果命中则不进行全局权限处理
        filterGlobalPermission(el, binding, vnode)
      }
    })
  }
}

/**
 * 全局权限控制
 */
export function filterGlobalPermission (el, binding, vnode) {
  const permissionList = []

  const authList = JSON.parse(sessionStorage.getItem(USER_AUTH) || '[]')
  for (const auth of authList) {
      permissionList.push(auth)
  }
  // 设置全局配置是否有命中
  const invalidFlag = false// 无效命中
  if (invalidFlag) {
    return
  }
  if (permissionList === null || permissionList === '' || permissionList === undefined || permissionList.length <= 0) {
    el.parentNode.removeChild(el)
    return
  }
  const permissions = []
  for (const item of permissionList) {
      permissions.push(item.webPerms)
  }
  if (!permissions.includes(binding.value) && binding.value) {
    el.parentNode.removeChild(el)
  }
}

export default hasPermission
