|
1
|
<template>
|
vben
authored
|
2
|
<PageWrapper contentBackground title="按钮权限控制" contentClass="p-4">
|
|
3
4
|
<CurrentPermissionMode />
<p>
|
Vben
authored
|
5
|
当前拥有的code列表: <a> {{ permissionStore.getPermCodeList }} </a>
|
|
6
7
|
</p>
<Divider />
|
|
8
9
10
11
12
13
|
<Alert
class="mt-4"
type="info"
message="点击后请查看按钮变化(必须处于后台权限模式才可测试此页面所展示的功能)"
show-icon
/>
|
|
14
|
<Divider />
|
|
15
|
<a-button type="primary" class="mr-2" @click="switchToken(2)" :disabled="!isBackPremissionMode">
|
|
16
17
|
点击切换按钮权限(用户id为2)
</a-button>
|
|
18
|
<a-button type="primary" @click="switchToken(1)" :disabled="!isBackPremissionMode">
|
|
19
20
21
|
点击切换按钮权限(用户id为1,默认)
</a-button>
|
|
22
23
24
25
26
|
<template v-if="isBackPremissionMode">
<Divider>组件方式判断权限</Divider>
<Authority :value="'1000'">
<a-button type="primary" class="mx-4"> 拥有code ['1000']权限可见 </a-button>
</Authority>
|
|
27
|
|
|
28
29
30
|
<Authority :value="'2000'">
<a-button color="success" class="mx-4"> 拥有code ['2000']权限可见 </a-button>
</Authority>
|
|
31
|
|
|
32
33
34
|
<Authority :value="['1000', '2000']">
<a-button color="error" class="mx-4"> 拥有code ['1000','2000']角色权限可见 </a-button>
</Authority>
|
|
35
|
|
|
36
37
38
39
|
<Divider>函数方式方式判断权限</Divider>
<a-button v-if="hasPermission('1000')" type="primary" class="mx-4">
拥有code ['1000']权限可见
</a-button>
|
|
40
|
|
|
41
42
43
|
<a-button v-if="hasPermission('2000')" color="success" class="mx-4">
拥有code ['2000']权限可见
</a-button>
|
|
44
|
|
|
45
46
47
|
<a-button v-if="hasPermission(['1000', '2000'])" color="error" class="mx-4">
拥有code ['1000','2000']角色权限可见
</a-button>
|
|
48
|
|
|
49
50
|
<Divider>指令方式方式判断权限(该方式不能动态修改权限.)</Divider>
<a-button v-auth="'1000'" type="primary" class="mx-4"> 拥有code ['1000']权限可见 </a-button>
|
|
51
|
|
|
52
|
<a-button v-auth="'2000'" color="success" class="mx-4"> 拥有code ['2000']权限可见 </a-button>
|
|
53
|
|
|
54
55
56
57
|
<a-button v-auth="['1000', '2000']" color="error" class="mx-4">
拥有code ['1000','2000']角色权限可见
</a-button>
</template>
|
vben
authored
|
58
|
</PageWrapper>
|
|
59
60
|
</template>
<script lang="ts">
|
|
61
|
import { defineComponent, computed } from 'vue';
|
|
62
63
64
|
import { Alert, Divider } from 'ant-design-vue';
import CurrentPermissionMode from '../CurrentPermissionMode.vue';
import { usePermission } from '/@/hooks/web/usePermission';
|
vben
authored
|
65
|
import { Authority } from '/@/components/Authority';
|
Vben
authored
|
66
|
import { usePermissionStore } from '/@/store/modules/permission';
|
|
67
|
import { PermissionModeEnum } from '/@/enums/appEnum';
|
vben
authored
|
68
|
import { PageWrapper } from '/@/components/Page';
|
|
69
70
|
import { useAppStore } from '/@/store/modules/app';
import { useUserStore } from '/@/store/modules/user';
|
vben
authored
|
71
|
|
|
72
|
export default defineComponent({
|
vben
authored
|
73
|
components: { Alert, PageWrapper, CurrentPermissionMode, Divider, Authority },
|
|
74
75
|
setup() {
const { hasPermission } = usePermission();
|
Vben
authored
|
76
|
const permissionStore = usePermissionStore();
|
|
77
78
79
80
|
const appStore = useAppStore();
const userStore = useUserStore();
const isBackPremissionMode = computed(
|
vben
authored
|
81
|
() => appStore.getProjectConfig.permissionMode === PermissionModeEnum.BACK,
|
|
82
83
84
85
86
87
|
);
async function switchToken(userId: number) {
// 本函数切换用户登录Token的部分仅用于演示,实际生产时切换身份应当重新登录
const token = 'fakeToken' + userId;
userStore.setToken(token);
|
|
88
|
|
|
89
90
91
|
// 重新获取用户信息和菜单
userStore.getUserInfoAction();
permissionStore.changePermissionCode();
|
|
92
|
}
|
Vben
authored
|
93
|
|
|
94
95
96
|
return {
hasPermission,
permissionStore,
|
|
97
98
|
switchToken,
isBackPremissionMode,
|
|
99
100
101
102
|
};
},
});
</script>
|
nebv
authored
|
103
104
|
<style lang="less" scoped>
.demo {
|
Vben
authored
|
105
|
background-color: @component-background;
|
nebv
authored
|
106
107
|
}
</style>
|