2024-06-30 15:03:37 +08:00
|
|
|
|
<script lang="ts" setup>
|
2024-07-13 17:25:15 +08:00
|
|
|
|
import type { LoginAndRegisterParams } from '@vben/common-ui';
|
2024-06-30 15:03:37 +08:00
|
|
|
|
|
2024-07-06 13:28:08 +08:00
|
|
|
|
import { useRouter } from 'vue-router';
|
|
|
|
|
|
|
|
|
|
import { useAccess } from '@vben/access';
|
|
|
|
|
|
|
|
|
|
import { Button } from 'ant-design-vue';
|
|
|
|
|
|
|
|
|
|
import { useAccessStore, useAppStore } from '#/store';
|
|
|
|
|
|
2024-07-13 11:26:36 +08:00
|
|
|
|
defineOptions({ name: 'Access' });
|
2024-07-06 13:28:08 +08:00
|
|
|
|
|
|
|
|
|
const accounts: Record<string, LoginAndRegisterParams> = {
|
|
|
|
|
admin: {
|
|
|
|
|
password: '123456',
|
|
|
|
|
username: 'admin',
|
|
|
|
|
},
|
|
|
|
|
super: {
|
|
|
|
|
password: '123456',
|
|
|
|
|
username: 'vben',
|
|
|
|
|
},
|
|
|
|
|
user: {
|
|
|
|
|
password: '123456',
|
|
|
|
|
username: 'jack',
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const { accessMode, toggleAccessMode } = useAccess();
|
|
|
|
|
const accessStore = useAccessStore();
|
|
|
|
|
const appStore = useAppStore();
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
|
|
function roleButtonType(role: string) {
|
|
|
|
|
return accessStore.userRoles.includes(role) ? 'primary' : 'default';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function changeAccount(role: string) {
|
|
|
|
|
if (accessStore.userRoles.includes(role)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const account = accounts[role];
|
2024-07-13 11:26:36 +08:00
|
|
|
|
appStore.resetAppState();
|
2024-07-06 13:28:08 +08:00
|
|
|
|
await accessStore.authLogin(account, async () => {
|
|
|
|
|
router.go(0);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function handleToggleAccessMode() {
|
|
|
|
|
await toggleAccessMode();
|
2024-07-13 11:26:36 +08:00
|
|
|
|
appStore.resetAppState();
|
2024-07-06 13:28:08 +08:00
|
|
|
|
await accessStore.authLogin(accounts.super, async () => {
|
|
|
|
|
router.go(0);
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-06-30 15:03:37 +08:00
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
2024-07-06 13:28:08 +08:00
|
|
|
|
<div class="p-5">
|
|
|
|
|
<div class="card-box p-5">
|
2024-07-13 11:26:36 +08:00
|
|
|
|
<h1 class="text-xl font-semibold">
|
|
|
|
|
{{ accessMode === 'frontend' ? '前端' : '后端' }}页面访问权限演示
|
|
|
|
|
</h1>
|
2024-07-06 13:28:08 +08:00
|
|
|
|
<div class="text-foreground/80 mt-2">
|
|
|
|
|
切换不同的账号,观察左侧菜单变化。
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div class="card-box mt-5 p-5 font-semibold">
|
|
|
|
|
<span class="text-lg">当前权限模式:</span>
|
2024-07-13 16:35:47 +08:00
|
|
|
|
<span class="text-primary mx-4">{{
|
|
|
|
|
accessMode === 'frontend' ? '前端权限控制' : '后端权限控制'
|
|
|
|
|
}}</span>
|
2024-07-06 13:28:08 +08:00
|
|
|
|
<Button type="primary" @click="handleToggleAccessMode">
|
|
|
|
|
切换为{{ accessMode === 'frontend' ? '后端' : '前端' }}权限模式
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
2024-07-13 11:26:36 +08:00
|
|
|
|
<div class="card-box mt-5 p-5 font-semibold">
|
|
|
|
|
<div class="mb-3">
|
|
|
|
|
<span class="text-lg">当前账号:</span>
|
2024-07-13 16:35:47 +08:00
|
|
|
|
<span class="text-primary mx-4 text-lg">
|
|
|
|
|
{{ accessStore.userRoles?.[0] }}
|
2024-07-13 11:26:36 +08:00
|
|
|
|
</span>
|
|
|
|
|
</div>
|
2024-07-06 13:28:08 +08:00
|
|
|
|
|
2024-07-13 11:26:36 +08:00
|
|
|
|
<Button :type="roleButtonType('super')" @click="changeAccount('super')">
|
|
|
|
|
切换为 Super 账号
|
|
|
|
|
</Button>
|
2024-07-06 13:28:08 +08:00
|
|
|
|
|
2024-07-13 11:26:36 +08:00
|
|
|
|
<Button
|
|
|
|
|
:type="roleButtonType('admin')"
|
|
|
|
|
class="mx-4"
|
|
|
|
|
@click="changeAccount('admin')"
|
|
|
|
|
>
|
|
|
|
|
切换为 Admin 账号
|
|
|
|
|
</Button>
|
|
|
|
|
<Button :type="roleButtonType('user')" @click="changeAccount('user')">
|
|
|
|
|
切换为 User 账号
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
2024-07-06 13:28:08 +08:00
|
|
|
|
</div>
|
2024-06-30 15:03:37 +08:00
|
|
|
|
</template>
|