perf: Improve the use of store in the app

This commit is contained in:
vince
2024-07-05 23:15:46 +08:00
parent ad6ae1d21d
commit 8042e2b044
39 changed files with 387 additions and 224 deletions

View File

@@ -1,80 +1,36 @@
<script lang="ts" setup>
import type { LoginAndRegisterParams } from '@vben/universal-ui';
import { computed } from 'vue';
import { useRouter } from 'vue-router';
import { DEFAULT_HOME_PATH } from '@vben/constants';
import { $t } from '@vben/locales';
import { AuthenticationLogin } from '@vben/universal-ui';
import { useRequest } from '@vben-core/request';
import { useAccessStore } from '@vben-core/stores';
import { App } from 'ant-design-vue';
import { getUserInfo, userLogin } from '#/apis';
import { useAccessStore } from '#/store';
defineOptions({ name: 'Login' });
const router = useRouter();
const accessStore = useAccessStore();
const { notification } = App.useApp();
const { loading, runAsync: runUserLogin } = useRequest(userLogin, {
manual: true,
});
const { loading: userInfoLoading, runAsync: runGetUserInfo } = useRequest(
getUserInfo,
{
manual: true,
},
);
/**
* 异步处理登录操作
* Asynchronously handle the login process
* @param values 登录表单数据
* @param params 登录表单数据
*/
async function handleLogin(values: LoginAndRegisterParams) {
// 异步处理用户登录操作并获取 accessToken
// Asynchronously handle the user login operation and obtain the accessToken
const { accessToken, refreshToken } = await runUserLogin(values);
// 如果成功获取到 accessToken
// If accessToken is successfully obtained
if (accessToken) {
// 将 accessToken 存储到 accessStore 中
// Store the accessToken in accessStore
accessStore.setAccessToken(accessToken);
accessStore.setRefreshToken(refreshToken);
// 获取用户信息并存储到 accessStore 中
// Get user information and store it in accessStore
const userInfo = await runGetUserInfo();
accessStore.setUserInfo(userInfo);
// 跳转到用户信息中定义的 homePath 路径
// Redirect to the homePath defined in the user information
await router.push(userInfo.homePath || DEFAULT_HOME_PATH);
async function handleLogin(params: LoginAndRegisterParams) {
const { userInfo } = await accessStore.authLogin(params);
if (userInfo?.realName) {
notification.success({
description: `${$t('authentication.login-success-desc')}:${userInfo.realName}`,
description: `${$t('authentication.login-success-desc')}:${userInfo?.realName}`,
duration: 3,
message: $t('authentication.login-success'),
});
}
}
const loginLoading = computed(() => {
return loading.value || userInfoLoading.value;
});
</script>
<template>
<AuthenticationLogin
:loading="loginLoading"
:loading="accessStore.loading"
password-placeholder="123456"
username-placeholder="vben"
@submit="handleLogin"

View File

@@ -24,7 +24,7 @@ import AnalyticsVisitsSource from '../analytics/analytics-visits-source.vue';
defineOptions({ name: 'Workspace' });
const { userInfo } = useAccessStore();
const accessStore = useAccessStore();
const projectItems: WorkbenchProjectItem[] = [
{
@@ -203,10 +203,10 @@ const trendItems: WorkbenchTrendItem[] = [
<template>
<div class="p-5">
<WorkbenchHeader
:avatar="userInfo?.avatar || preferences.app.defaultAvatar"
:avatar="accessStore.userInfo?.avatar || preferences.app.defaultAvatar"
>
<template #title>
早安, {{ userInfo?.realName }}, 开始您一天的工作吧
早安, {{ accessStore.userInfo?.realName }}, 开始您一天的工作吧
</template>
<template #description> 今日晴20 - 32 </template>
</WorkbenchHeader>

View File

@@ -0,0 +1,13 @@
<script lang="ts" setup>
import { Fallback } from '@vben/universal-ui';
defineOptions({ name: 'AccessFrontendAccessTest1' });
</script>
<template>
<Fallback
description="当前页面仅 Super 角色可见"
status="comming-soon"
title="页面访问测试"
/>
</template>

View File

@@ -1,16 +1,48 @@
<script lang="ts" setup>
import type { LoginAndRegisterParams } from '@vben/universal-ui';
import { useRouter } from 'vue-router';
import { useAccess } from '@vben/access';
import { useAccessStore } from '@vben-core/stores';
import { Button } from 'ant-design-vue';
import { useAccessStore, useAppStore } from '#/store';
defineOptions({ name: 'AccessBackend' });
const { currentAccessMode } = useAccess();
const { accessMode } = useAccess();
const accessStore = useAccessStore();
const appStore = useAppStore();
const router = useRouter();
function roleButtonType(role: string) {
return accessStore.getUserRoles.includes(role) ? 'primary' : 'default';
return accessStore.userRoles.includes(role) ? 'primary' : 'default';
}
async function changeAccount(role: string) {
if (accessStore.userRoles.includes(role)) {
return;
}
const accounts: Record<string, LoginAndRegisterParams> = {
admin: {
password: '123456',
username: 'admin',
},
super: {
password: '123456',
username: 'vben',
},
user: {
password: '123456',
username: 'jack',
},
};
const account = accounts[role];
await appStore.resetAppState();
await accessStore.authLogin(account, async () => {
router.go(0);
});
}
</script>
@@ -19,26 +51,39 @@ function roleButtonType(role: string) {
<div class="card-box p-5">
<h1 class="text-xl font-semibold">前端页面访问演示</h1>
<div class="text-foreground/80 mt-2">
由于刷新的时候会请求用户信息接口会根据接口重置角色信息所以刷新后界面会恢复原样如果不需要可以注释对应的代码
切换不同的账号观察左侧菜单变化
</div>
</div>
<template v-if="currentAccessMode === 'frontend'">
<template v-if="accessMode === 'frontend'">
<div class="card-box mt-5 p-5 font-semibold">
当前权限模式:
<span class="text-primary mx-4">{{ currentAccessMode }}</span>
<span class="text-primary mx-4">{{ accessMode }}</span>
<Button type="primary">切换权限模式</Button>
</div>
<div class="card-box mt-5 p-5 font-semibold">
当前用户角色:
<span class="text-primary mx-4">{{ accessStore.getUserRoles }}</span>
<Button :type="roleButtonType('admin')"> 切换为 Admin 角色 </Button>
<Button :type="roleButtonType('user')" class="mx-4">
切换为 User 角色
<div class="mb-3">
当前账号:
<span class="text-primary mx-4">
{{ accessStore.userRoles }}
</span>
</div>
<Button :type="roleButtonType('super')" @click="changeAccount('super')">
切换为 Super 账号
</Button>
<div class="text-foreground/80 mt-2">角色后请查看左侧菜单变化</div>
<Button
:type="roleButtonType('admin')"
class="mx-4"
@click="changeAccount('admin')"
>
切换为 Admin 账号
</Button>
<Button :type="roleButtonType('user')" @click="changeAccount('user')">
切换为 User 账号
</Button>
</div>
</template>
</div>