index.vue 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <script lang="ts" setup>
  2. import type { Recordable } from '@vben/types';
  3. import { useRouter } from 'vue-router';
  4. import { useAccess } from '@vben/access';
  5. import { Page } from '@vben/common-ui';
  6. import { resetAllStores, useUserStore } from '@vben/stores';
  7. import { Button, Card } from 'ant-design-vue';
  8. import { useAuthStore } from '#/store';
  9. const accounts: Record<string, Recordable<any>> = {
  10. admin: {
  11. password: '123456',
  12. username: 'admin',
  13. },
  14. super: {
  15. password: '123456',
  16. username: 'vben',
  17. },
  18. user: {
  19. password: '123456',
  20. username: 'jack',
  21. },
  22. };
  23. const { accessMode, toggleAccessMode } = useAccess();
  24. const userStore = useUserStore();
  25. const accessStore = useAuthStore();
  26. const router = useRouter();
  27. function roleButtonType(role: string) {
  28. return userStore.userRoles.includes(role) ? 'primary' : 'default';
  29. }
  30. async function changeAccount(role: string) {
  31. if (userStore.userRoles.includes(role)) {
  32. return;
  33. }
  34. const account = accounts[role];
  35. resetAllStores();
  36. if (account) {
  37. await accessStore.authLogin(account, async () => {
  38. router.go(0);
  39. });
  40. }
  41. }
  42. async function handleToggleAccessMode() {
  43. if (!accounts.super) {
  44. return;
  45. }
  46. await toggleAccessMode();
  47. resetAllStores();
  48. await accessStore.authLogin(accounts.super, async () => {
  49. setTimeout(() => {
  50. router.go(0);
  51. }, 150);
  52. });
  53. }
  54. </script>
  55. <template>
  56. <Page
  57. :title="`${accessMode === 'frontend' ? '前端' : '后端'}页面访问权限演示`"
  58. description="切换不同的账号,观察左侧菜单变化。"
  59. >
  60. <Card class="mb-5" title="权限模式">
  61. <span class="font-semibold">当前权限模式:</span>
  62. <span class="text-primary mx-4">{{
  63. accessMode === 'frontend' ? '前端权限控制' : '后端权限控制'
  64. }}</span>
  65. <Button type="primary" @click="handleToggleAccessMode">
  66. 切换为{{ accessMode === 'frontend' ? '后端' : '前端' }}权限模式
  67. </Button>
  68. </Card>
  69. <Card title="账号切换">
  70. <Button :type="roleButtonType('super')" @click="changeAccount('super')">
  71. 切换为 Super 账号
  72. </Button>
  73. <Button
  74. :type="roleButtonType('admin')"
  75. class="mx-4"
  76. @click="changeAccount('admin')"
  77. >
  78. 切换为 Admin 账号
  79. </Button>
  80. <Button :type="roleButtonType('user')" @click="changeAccount('user')">
  81. 切换为 User 账号
  82. </Button>
  83. </Card>
  84. </Page>
  85. </template>