|
1
|
<template>
|
vben
authored
|
2
3
|
<PageWrapper
title="前端权限按钮示例"
|
vben
authored
|
4
|
contentBackground
|
vben
authored
|
5
6
7
|
contentClass="p-4"
content="由于刷新的时候会请求用户信息接口,会根据接口重置角色信息,所以刷新后界面会恢复原样,如果不需要,可以注释 src/layout/default/index内的获取用户信息接口"
>
|
|
8
9
10
|
<CurrentPermissionMode />
<p>
|
Vben
authored
|
11
|
当前角色: <a> {{ userStore.getRoleList }} </a>
|
|
12
13
14
15
16
|
</p>
<Alert class="mt-4" type="info" message="点击后请查看按钮变化" show-icon />
<div class="mt-4">
权限切换(请先切换权限模式为前端角色权限模式):
|
vben
authored
|
17
|
<Space>
|
vben
authored
|
18
|
<a-button @click="changeRole(RoleEnum.SUPER)" :type="isSuper ? 'primary' : 'default'">
|
|
19
20
|
{{ RoleEnum.SUPER }}
</a-button>
|
vben
authored
|
21
|
<a-button @click="changeRole(RoleEnum.TEST)" :type="isTest ? 'primary' : 'default'">
|
|
22
23
|
{{ RoleEnum.TEST }}
</a-button>
|
vben
authored
|
24
|
</Space>
|
|
25
26
27
|
</div>
<Divider>组件方式判断权限(有需要可以自行全局注册)</Divider>
<Authority :value="RoleEnum.SUPER">
|
vben
authored
|
28
|
<a-button type="primary" class="mx-4"> 拥有super角色权限可见 </a-button>
|
|
29
30
31
|
</Authority>
<Authority :value="RoleEnum.TEST">
|
vben
authored
|
32
|
<a-button color="success" class="mx-4"> 拥有test角色权限可见 </a-button>
|
|
33
34
35
|
</Authority>
<Authority :value="[RoleEnum.TEST, RoleEnum.SUPER]">
|
vben
authored
|
36
|
<a-button color="error" class="mx-4"> 拥有[test,super]角色权限可见 </a-button>
|
|
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
</Authority>
<Divider>函数方式方式判断权限(适用于函数内部过滤)</Divider>
<a-button v-if="hasPermission(RoleEnum.SUPER)" type="primary" class="mx-4">
拥有super角色权限可见
</a-button>
<a-button v-if="hasPermission(RoleEnum.TEST)" color="success" class="mx-4">
拥有test角色权限可见
</a-button>
<a-button v-if="hasPermission([RoleEnum.TEST, RoleEnum.SUPER])" color="error" class="mx-4">
拥有[test,super]角色权限可见
</a-button>
<Divider>指令方式方式判断权限(该方式不能动态修改权限.)</Divider>
<a-button v-auth="RoleEnum.SUPER" type="primary" class="mx-4"> 拥有super角色权限可见 </a-button>
<a-button v-auth="RoleEnum.TEST" color="success" class="mx-4"> 拥有test角色权限可见 </a-button>
<a-button v-auth="[RoleEnum.TEST, RoleEnum.SUPER]" color="error" class="mx-4">
拥有[test,super]角色权限可见
</a-button>
|
vben
authored
|
60
|
</PageWrapper>
|
|
61
62
63
|
</template>
<script lang="ts">
import { computed, defineComponent } from 'vue';
|
vben
authored
|
64
|
import { Alert, Divider, Space } from 'ant-design-vue';
|
|
65
|
import CurrentPermissionMode from '../CurrentPermissionMode.vue';
|
Vben
authored
|
66
|
import { useUserStore } from '/@/store/modules/user';
|
|
67
68
|
import { RoleEnum } from '/@/enums/roleEnum';
import { usePermission } from '/@/hooks/web/usePermission';
|
vben
authored
|
69
|
import { Authority } from '/@/components/Authority';
|
vben
authored
|
70
|
import { PageWrapper } from '/@/components/Page';
|
|
71
72
|
export default defineComponent({
|
vben
authored
|
73
|
components: { Alert, PageWrapper, Space, CurrentPermissionMode, Divider, Authority },
|
|
74
75
|
setup() {
const { changeRole, hasPermission } = usePermission();
|
Vben
authored
|
76
77
|
const userStore = useUserStore();
|
|
78
79
80
|
return {
userStore,
RoleEnum,
|
Vben
authored
|
81
82
|
isSuper: computed(() => userStore.getRoleList.includes(RoleEnum.SUPER)),
isTest: computed(() => userStore.getRoleList.includes(RoleEnum.TEST)),
|
|
83
84
85
86
87
88
|
changeRole,
hasPermission,
};
},
});
</script>
|
nebv
authored
|
89
90
|
<style lang="less" scoped>
.demo {
|
Vben
authored
|
91
|
background-color: @component-background;
|
nebv
authored
|
92
93
|
}
</style>
|