import store from '@/store'
function checkPermission(el,binding) {
  //获取对应的权限
  const { value } = binding;

  //获取当前用户的所有功能权限
  const buttonPermission = store.getters.userInfo?.permission?.buttonPermission;
  //value必须是一个数组
  if (value && value instanceof Array  && buttonPermission.length > 0) {
    //匹配对应的指令
    const hasPermission = buttonPermission.some(item => {
      return value.includes(item)
    })

    //匹配失败
    if (!hasPermission) {
      el.parentNode && el.parentNode.removeChild(el)
    }
  } else {
    throw new ErrorEvent('v-permisss value must 酱紫【"admin" ,"login"】...')
  }
}

export default {
  //在绑定 元素的父组件被挂载之后调用
  mounted (el,binding) {
    checkPermission(el,binding)
  },
  // 在包含组件的VNode 及其子组件的VNode更新后调用
  update (el,binding) {
    checkPermission(el,binding)
  }

}
