Blame view

src/views/demo/permission/back/Btn.vue 3.86 KB
陈文彬 authored
1
<template>
2
  <PageWrapper contentBackground title="按钮权限控制" contentClass="p-4">
陈文彬 authored
3
4
    <CurrentPermissionMode />
    <p>
Vben authored
5
      当前拥有的code列表: <a> {{ permissionStore.getPermCodeList }} </a>
陈文彬 authored
6
7
    </p>
    <Divider />
8
9
10
11
12
13
    <Alert
      class="mt-4"
      type="info"
      message="点击后请查看按钮变化(必须处于后台权限模式才可测试此页面所展示的功能)"
      show-icon
    />
陈文彬 authored
14
    <Divider />
15
    <a-button type="primary" class="mr-2" @click="switchToken(2)" :disabled="!isBackPremissionMode">
陈文彬 authored
16
17
      点击切换按钮权限(用户id为2)
    </a-button>
18
    <a-button type="primary" @click="switchToken(1)" :disabled="!isBackPremissionMode">
陈文彬 authored
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>
陈文彬 authored
27
28
29
30
      <Authority :value="'2000'">
        <a-button color="success" class="mx-4"> 拥有code ['2000']权限可见 </a-button>
      </Authority>
陈文彬 authored
31
32
33
34
      <Authority :value="['1000', '2000']">
        <a-button color="error" class="mx-4"> 拥有code ['1000','2000']角色权限可见 </a-button>
      </Authority>
陈文彬 authored
35
36
37
38
39
      <Divider>函数方式方式判断权限</Divider>
      <a-button v-if="hasPermission('1000')" type="primary" class="mx-4">
        拥有code ['1000']权限可见
      </a-button>
陈文彬 authored
40
41
42
43
      <a-button v-if="hasPermission('2000')" color="success" class="mx-4">
        拥有code ['2000']权限可见
      </a-button>
陈文彬 authored
44
45
46
47
      <a-button v-if="hasPermission(['1000', '2000'])" color="error" class="mx-4">
        拥有code ['1000','2000']角色权限可见
      </a-button>
陈文彬 authored
48
49
50
      <Divider>指令方式方式判断权限(该方式不能动态修改权限.)</Divider>
      <a-button v-auth="'1000'" type="primary" class="mx-4"> 拥有code ['1000']权限可见 </a-button>
陈文彬 authored
51
52
      <a-button v-auth="'2000'" color="success" class="mx-4"> 拥有code ['2000']权限可见 </a-button>
陈文彬 authored
53
54
55
56
57
      <a-button v-auth="['1000', '2000']" color="error" class="mx-4">
        拥有code ['1000','2000']角色权限可见
      </a-button>
    </template>
58
  </PageWrapper>
陈文彬 authored
59
60
</template>
<script lang="ts">
61
  import { defineComponent, computed } from 'vue';
陈文彬 authored
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';
陈文彬 authored
67
  import { PermissionModeEnum } from '/@/enums/appEnum';
68
  import { PageWrapper } from '/@/components/Page';
69
70
  import { useAppStore } from '/@/store/modules/app';
  import { useUserStore } from '/@/store/modules/user';
71
陈文彬 authored
72
  export default defineComponent({
73
    components: { Alert, PageWrapper, CurrentPermissionMode, Divider, Authority },
陈文彬 authored
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);
陈文彬 authored
88
89
90
91
        // 重新获取用户信息和菜单
        userStore.getUserInfoAction();
        permissionStore.changePermissionCode();
陈文彬 authored
92
      }
93
陈文彬 authored
94
95
96
      return {
        hasPermission,
        permissionStore,
97
98
        switchToken,
        isBackPremissionMode,
陈文彬 authored
99
100
101
102
      };
    },
  });
</script>
103
104
<style lang="less" scoped>
  .demo {
105
    background-color: @component-background;
106
107
  }
</style>