Blame view

src/directives/permission.ts 742 Bytes
vben authored
1
2
3
4
5
6
7
/**
 * Global authority directive
 * Used for fine-grained control of component permissions
 * @Example v-auth="RoleEnum.TEST"
 */
import type { App, Directive, DirectiveBinding } from 'vue';
陈文彬 authored
8
9
10
import { usePermission } from '/@/hooks/web/usePermission';

function isAuth(el: Element, binding: any) {
11
12
  const { hasPermission } = usePermission();
陈文彬 authored
13
14
15
  const value = binding.value;
  if (!value) return;
  if (!hasPermission(value)) {
vben authored
16
    el.parentNode?.removeChild(el);
陈文彬 authored
17
18
  }
}
vben authored
19
20
21
22
23
24
25
26
27

const mounted = (el: Element, binding: DirectiveBinding<any>) => {
  isAuth(el, binding);
};

const authDirective: Directive = {
  mounted,
};
陈文彬 authored
28
export function setupPermissionDirective(app: App) {
vben authored
29
  app.directive('auth', authDirective);
陈文彬 authored
30
}
vben authored
31
32

export default authDirective;