auth.ts 862 B

12345678910111213141516171819202122232425262728293031323334
  1. /**
  2. * v-auth
  3. * 按钮权限指令
  4. */
  5. import { useUserStore } from '@/stores'
  6. import type { Directive, DirectiveBinding } from 'vue'
  7. const auth: Directive = {
  8. mounted(el: HTMLElement, binding: DirectiveBinding) {
  9. const { value } = binding
  10. const all_permission = '*:*:*'
  11. const permissions = useUserStore().permissionCodes
  12. if (typeof value === 'string') {
  13. if (!permissions.includes(value)) {
  14. el.parentNode && el.parentNode.removeChild(el)
  15. }
  16. return
  17. }
  18. if (value && value instanceof Array && value.length > 0) {
  19. const hasPermissions = value.some(item => {
  20. return permissions.includes(all_permission) || permissions.includes(item)
  21. })
  22. if (!hasPermissions) {
  23. el.parentNode && el.parentNode.removeChild(el)
  24. }
  25. } else {
  26. return false
  27. }
  28. }
  29. }
  30. export default auth