vben
authored
5 years ago
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';
Vben
authored
4 years ago
8
import projectSetting from '/@/settings/projectSetting';
9
10
11
12
import { usePermission } from '/@/hooks/web/usePermission';
import { PermissionModeEnum } from '/@/enums/appEnum';
function isAuth(el: Element, binding: any) {
Vben
authored
4 years ago
13
14
const { hasPermission } = usePermission();
15
16
17
const value = binding.value;
if (!value) return;
if (!hasPermission(value)) {
vben
authored
5 years ago
18
el.parentNode?.removeChild(el);
19
20
}
}
vben
authored
5 years ago
21
22
function isBackMode() {
Vben
authored
4 years ago
23
return projectSetting.permissionMode === PermissionModeEnum.BACK;
24
}
vben
authored
5 years ago
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const mounted = (el: Element, binding: DirectiveBinding<any>) => {
if (isBackMode()) return;
isAuth(el, binding);
};
const updated = (el: Element, binding: DirectiveBinding<any>) => {
if (!isBackMode()) return;
isAuth(el, binding);
};
const authDirective: Directive = {
mounted,
updated,
};
41
export function setupPermissionDirective(app: App) {
vben
authored
5 years ago
42
app.directive('auth', authDirective);
43
}
vben
authored
5 years ago
44
45
export default authDirective;