Skip to content

Commit c4c8103

Browse files
committed
perf: improve the login process
1 parent d62a3da commit c4c8103

File tree

5 files changed

+28
-29
lines changed

5 files changed

+28
-29
lines changed

apps/web-antd/src/apis/modules/mock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { requestClient } from '#/forward';
22

33
/**
4-
* 模拟人意状态码
4+
* 模拟任意状态码
55
*/
66
async function getMockStatus(status: string) {
77
return requestClient.get('/mock/status', { params: { status } });

apps/web-antd/src/layouts/basic.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ const menus = computed(() => [
8585
8686
const appStore = useAppStore();
8787
const accessStore = useAccessStore();
88-
const { openLoginExpiredModal, userInfo } = toRefs(accessStore);
88+
const {
89+
loading: loginLoading,
90+
openLoginExpiredModal,
91+
userInfo,
92+
} = toRefs(accessStore);
8993
const router = useRouter();
9094
9195
async function handleLogout() {
@@ -126,6 +130,7 @@ function handleMakeAll() {
126130
<template #dialog>
127131
<AuthenticationLoginExpiredModal
128132
v-model:open="openLoginExpiredModal"
133+
:loading="loginLoading"
129134
password-placeholder="123456"
130135
username-placeholder="vben"
131136
@submit="accessStore.authLogin"

apps/web-antd/src/store/modules/access.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ import { useRouter } from 'vue-router';
88
import { DEFAULT_HOME_PATH, LOGIN_PATH } from '@vben/constants';
99
import { useCoreAccessStore } from '@vben-core/stores';
1010

11+
import { notification } from 'ant-design-vue';
1112
import { defineStore } from 'pinia';
1213

1314
import { getAccessCodes, getUserInfo, userLogin } from '#/apis';
15+
import { $t } from '#/locales';
1416

1517
export const useAccessStore = defineStore('access', () => {
1618
const coreStoreAccess = useCoreAccessStore();
1719
const router = useRouter();
20+
1821
const loading = ref(false);
1922

2023
const openLoginExpiredModal = ref(false);
@@ -44,7 +47,7 @@ export const useAccessStore = defineStore('access', () => {
4447
*/
4548
async function authLogin(
4649
params: LoginAndRegisterParams,
47-
onSuccess?: () => Promise<void>,
50+
onSuccess?: () => Promise<void> | void,
4851
) {
4952
// 异步处理用户登录操作并获取 accessToken
5053
let userInfo: UserInfo | null = null;
@@ -72,10 +75,21 @@ export const useAccessStore = defineStore('access', () => {
7275
coreStoreAccess.setUserInfo(userInfo);
7376
coreStoreAccess.setAccessCodes(accessCodes);
7477

75-
openLoginExpiredModal.value = false;
76-
onSuccess
77-
? await onSuccess?.()
78-
: await router.push(userInfo.homePath || DEFAULT_HOME_PATH);
78+
if (openLoginExpiredModal.value) {
79+
openLoginExpiredModal.value = false;
80+
} else {
81+
onSuccess
82+
? await onSuccess?.()
83+
: await router.push(userInfo.homePath || DEFAULT_HOME_PATH);
84+
}
85+
86+
if (userInfo?.realName) {
87+
notification.success({
88+
description: `${$t('authentication.loginSuccessDesc')}:${userInfo?.realName}`,
89+
duration: 3,
90+
message: $t('authentication.loginSuccess'),
91+
});
92+
}
7993
}
8094
} finally {
8195
loading.value = false;

apps/web-antd/src/store/modules/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const useAppStore = defineStore('app', () => {
1212
* 重置所有状态
1313
*/
1414
async function resetAppState() {
15-
accessStore.$reset();
15+
accessStore.reset();
1616
coreTabbarStore.$reset();
1717
}
1818

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,18 @@
11
<script lang="ts" setup>
2-
import type { LoginAndRegisterParams } from '@vben/universal-ui';
3-
42
import { AuthenticationLogin } from '@vben/universal-ui';
53
6-
import { App } from 'ant-design-vue';
7-
8-
import { $t } from '#/locales';
94
import { useAccessStore } from '#/store';
105
116
defineOptions({ name: 'Login' });
127
138
const accessStore = useAccessStore();
14-
const { notification } = App.useApp();
15-
16-
/**
17-
* @param params 登录表单数据
18-
*/
19-
async function handleLogin(params: LoginAndRegisterParams) {
20-
const { userInfo } = await accessStore.authLogin(params);
21-
if (userInfo?.realName) {
22-
notification.success({
23-
description: `${$t('authentication.loginSuccessDesc')}:${userInfo?.realName}`,
24-
duration: 3,
25-
message: $t('authentication.loginSuccess'),
26-
});
27-
}
28-
}
299
</script>
3010

3111
<template>
3212
<AuthenticationLogin
3313
:loading="accessStore.loading"
3414
password-placeholder="123456"
3515
username-placeholder="vben"
36-
@submit="handleLogin"
16+
@submit="accessStore.authLogin"
3717
/>
3818
</template>

0 commit comments

Comments
 (0)