|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
import { defineComponent, PropType, computed, unref } from 'vue';
import { PermissionModeEnum } from '/@/enums/appEnum';
import { RoleEnum } from '/@/enums/roleEnum';
import { usePermission } from '/@/hooks/web/usePermission';
import { appStore } from '/@/store/modules/app';
import { getSlot } from '/@/utils/helper/tsxHelper';
export default defineComponent({
name: 'Authority',
props: {
// 指定角色可见
value: {
type: [Number, Array, String] as PropType<RoleEnum | RoleEnum[]>,
default: '',
},
},
setup(props, { slots }) {
const getModeRef = computed(() => {
return appStore.getProjectConfig.permissionMode;
});
|
vben
authored
|
22
|
|
|
23
24
25
26
27
28
|
/**
* 渲染角色按钮
*/
function renderRoleAuth() {
const { value } = props;
if (!value) {
|
vben
authored
|
29
|
return getSlot(slots);
|
|
30
31
|
}
const { hasPermission } = usePermission();
|
vben
authored
|
32
|
return hasPermission(value) ? getSlot(slots) : null;
|
|
33
34
35
36
37
38
39
40
41
|
}
/**
* 渲染编码按钮
* 这里只判断是否包含,具体实现可以根据项目自行写逻辑
*/
function renderCodeAuth() {
const { value } = props;
if (!value) {
|
vben
authored
|
42
|
return getSlot(slots);
|
|
43
44
|
}
const { hasPermission } = usePermission();
|
vben
authored
|
45
|
return hasPermission(value) ? getSlot(slots) : null;
|
|
46
|
}
|
vben
authored
|
47
|
|
|
48
49
50
51
52
53
54
55
56
57
|
return () => {
const mode = unref(getModeRef);
// 基于角色渲染
if (mode === PermissionModeEnum.ROLE) {
return renderRoleAuth();
}
// 基于后台编码渲染
if (mode === PermissionModeEnum.BACK) {
return renderCodeAuth();
}
|
vben
authored
|
58
|
return getSlot(slots);
|
|
59
60
61
|
};
},
});
|