Blame view

src/setup/directives/permission.ts 840 Bytes
vben authored
1
import { appStore } from '/@/store/modules/app';
陈文彬 authored
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import type { App } from 'vue';
import { usePermission } from '/@/hooks/web/usePermission';
import { PermissionModeEnum } from '/@/enums/appEnum';
const { hasPermission } = usePermission();

function isAuth(el: Element, binding: any) {
  const value = binding.value;
  if (!value) return;
  if (!hasPermission(value)) {
    if (el.parentNode) {
      el.parentNode.removeChild(el);
    }
  }
}
function isBackMode() {
  return appStore.getProjectConfig.permissionMode === PermissionModeEnum.BACK;
}
export function setupPermissionDirective(app: App) {
  app.directive('auth', {
    mounted(el: Element, binding) {
      if (isBackMode()) return;
      isAuth(el, binding);
    },
    updated(el: Element, binding) {
      if (!isBackMode()) return;
      isAuth(el, binding);
    },
  });
}