Newer
Older
DH_Apicture / src / directive / permission / hasRole.js
@zhangqy zhangqy on 29 Nov 661 bytes first commit
  1. /**
  2. * v-hasRole 角色权限处理
  3. * Copyright (c) 2019 newfiber
  4. */
  5.  
  6. import useUserStore from "@/store/modules/user";
  7.  
  8. export default {
  9. mounted(el, binding, vnode) {
  10. const { value } = binding;
  11. const super_admin = "admin";
  12.  
  13. const roles = useUserStore().roles;
  14.  
  15. if (value && value instanceof Array && value.length > 0) {
  16. const roleFlag = value;
  17.  
  18. const hasRole = roles.some((role) => {
  19. return super_admin === role || roleFlag.includes(role);
  20. });
  21.  
  22. if (!hasRole) {
  23. el.parentNode && el.parentNode.removeChild(el);
  24. }
  25. } else {
  26. throw new Error(`请设置角色权限标签值`);
  27. }
  28. },
  29. };