ny
昨天 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
1
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
 * Global authority directive
 * Used for fine-grained control of component permissions
 * @Example v-auth="'btn-edit'"
 */
import type { App, Directive, DirectiveBinding } from 'vue';
 
import { useAccessStore } from '@vben/stores';
 
function hasBtnP(modelId, value?: string): boolean {
  if (!value) return true;
  if (!modelId) return false;
  const accessStore = useAccessStore();
  const permissionList = accessStore.permissionList;
  const list = permissionList.filter((o) => o.modelId === modelId);
  if (!list.length) return false;
  const btnList = list[0] && list[0].button ? list[0].button : [];
  if (!btnList.length) return false;
  const hasPermission = btnList.some((btn) => btn.enCode === value);
  if (hasPermission) return true;
  return false;
}
 
function isAuth(el: Element, binding: any, VNode) {
  const value = binding.value;
  const modelId = VNode.ctx.proxy.$route.meta.modelId || '';
  if (!value) return;
  if (!hasBtnP(modelId, value)) {
    el?.remove();
  }
}
 
const mounted = (
  el: Element,
  binding: DirectiveBinding<string | string[]>,
  VNode,
) => {
  isAuth(el, binding, VNode);
};
 
const authDirective: Directive = {
  mounted,
};
 
export function registerAccessDirective(app: App) {
  app.directive('auth', authDirective);
}