perf: improve the login process
							parent
							
								
									d62a3da009
								
							
						
					
					
						commit
						c4c8103af2
					
				|  | @ -1,7 +1,7 @@ | ||||||
| import { requestClient } from '#/forward'; | import { requestClient } from '#/forward'; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * 模拟人意状态码 |  * 模拟任意状态码 | ||||||
|  */ |  */ | ||||||
| async function getMockStatus(status: string) { | async function getMockStatus(status: string) { | ||||||
|   return requestClient.get('/mock/status', { params: { status } }); |   return requestClient.get('/mock/status', { params: { status } }); | ||||||
|  |  | ||||||
|  | @ -85,7 +85,11 @@ const menus = computed(() => [ | ||||||
| 
 | 
 | ||||||
| const appStore = useAppStore(); | const appStore = useAppStore(); | ||||||
| const accessStore = useAccessStore(); | const accessStore = useAccessStore(); | ||||||
| const { openLoginExpiredModal, userInfo } = toRefs(accessStore); | const { | ||||||
|  |   loading: loginLoading, | ||||||
|  |   openLoginExpiredModal, | ||||||
|  |   userInfo, | ||||||
|  | } = toRefs(accessStore); | ||||||
| const router = useRouter(); | const router = useRouter(); | ||||||
| 
 | 
 | ||||||
| async function handleLogout() { | async function handleLogout() { | ||||||
|  | @ -126,6 +130,7 @@ function handleMakeAll() { | ||||||
|     <template #dialog> |     <template #dialog> | ||||||
|       <AuthenticationLoginExpiredModal |       <AuthenticationLoginExpiredModal | ||||||
|         v-model:open="openLoginExpiredModal" |         v-model:open="openLoginExpiredModal" | ||||||
|  |         :loading="loginLoading" | ||||||
|         password-placeholder="123456" |         password-placeholder="123456" | ||||||
|         username-placeholder="vben" |         username-placeholder="vben" | ||||||
|         @submit="accessStore.authLogin" |         @submit="accessStore.authLogin" | ||||||
|  |  | ||||||
|  | @ -8,13 +8,16 @@ import { useRouter } from 'vue-router'; | ||||||
| import { DEFAULT_HOME_PATH, LOGIN_PATH } from '@vben/constants'; | import { DEFAULT_HOME_PATH, LOGIN_PATH } from '@vben/constants'; | ||||||
| import { useCoreAccessStore } from '@vben-core/stores'; | import { useCoreAccessStore } from '@vben-core/stores'; | ||||||
| 
 | 
 | ||||||
|  | import { notification } from 'ant-design-vue'; | ||||||
| import { defineStore } from 'pinia'; | import { defineStore } from 'pinia'; | ||||||
| 
 | 
 | ||||||
| import { getAccessCodes, getUserInfo, userLogin } from '#/apis'; | import { getAccessCodes, getUserInfo, userLogin } from '#/apis'; | ||||||
|  | import { $t } from '#/locales'; | ||||||
| 
 | 
 | ||||||
| export const useAccessStore = defineStore('access', () => { | export const useAccessStore = defineStore('access', () => { | ||||||
|   const coreStoreAccess = useCoreAccessStore(); |   const coreStoreAccess = useCoreAccessStore(); | ||||||
|   const router = useRouter(); |   const router = useRouter(); | ||||||
|  | 
 | ||||||
|   const loading = ref(false); |   const loading = ref(false); | ||||||
| 
 | 
 | ||||||
|   const openLoginExpiredModal = ref(false); |   const openLoginExpiredModal = ref(false); | ||||||
|  | @ -44,7 +47,7 @@ export const useAccessStore = defineStore('access', () => { | ||||||
|    */ |    */ | ||||||
|   async function authLogin( |   async function authLogin( | ||||||
|     params: LoginAndRegisterParams, |     params: LoginAndRegisterParams, | ||||||
|     onSuccess?: () => Promise<void>, |     onSuccess?: () => Promise<void> | void, | ||||||
|   ) { |   ) { | ||||||
|     // 异步处理用户登录操作并获取 accessToken
 |     // 异步处理用户登录操作并获取 accessToken
 | ||||||
|     let userInfo: UserInfo | null = null; |     let userInfo: UserInfo | null = null; | ||||||
|  | @ -72,10 +75,21 @@ export const useAccessStore = defineStore('access', () => { | ||||||
|         coreStoreAccess.setUserInfo(userInfo); |         coreStoreAccess.setUserInfo(userInfo); | ||||||
|         coreStoreAccess.setAccessCodes(accessCodes); |         coreStoreAccess.setAccessCodes(accessCodes); | ||||||
| 
 | 
 | ||||||
|         openLoginExpiredModal.value = false; |         if (openLoginExpiredModal.value) { | ||||||
|         onSuccess |           openLoginExpiredModal.value = false; | ||||||
|           ? await onSuccess?.() |         } else { | ||||||
|           : await router.push(userInfo.homePath || DEFAULT_HOME_PATH); |           onSuccess | ||||||
|  |             ? await onSuccess?.() | ||||||
|  |             : await router.push(userInfo.homePath || DEFAULT_HOME_PATH); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         if (userInfo?.realName) { | ||||||
|  |           notification.success({ | ||||||
|  |             description: `${$t('authentication.loginSuccessDesc')}:${userInfo?.realName}`, | ||||||
|  |             duration: 3, | ||||||
|  |             message: $t('authentication.loginSuccess'), | ||||||
|  |           }); | ||||||
|  |         } | ||||||
|       } |       } | ||||||
|     } finally { |     } finally { | ||||||
|       loading.value = false; |       loading.value = false; | ||||||
|  |  | ||||||
|  | @ -12,7 +12,7 @@ export const useAppStore = defineStore('app', () => { | ||||||
|    * 重置所有状态 |    * 重置所有状态 | ||||||
|    */ |    */ | ||||||
|   async function resetAppState() { |   async function resetAppState() { | ||||||
|     accessStore.$reset(); |     accessStore.reset(); | ||||||
|     coreTabbarStore.$reset(); |     coreTabbarStore.$reset(); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -1,31 +1,11 @@ | ||||||
| <script lang="ts" setup> | <script lang="ts" setup> | ||||||
| import type { LoginAndRegisterParams } from '@vben/universal-ui'; |  | ||||||
| 
 |  | ||||||
| import { AuthenticationLogin } from '@vben/universal-ui'; | import { AuthenticationLogin } from '@vben/universal-ui'; | ||||||
| 
 | 
 | ||||||
| import { App } from 'ant-design-vue'; |  | ||||||
| 
 |  | ||||||
| import { $t } from '#/locales'; |  | ||||||
| import { useAccessStore } from '#/store'; | import { useAccessStore } from '#/store'; | ||||||
| 
 | 
 | ||||||
| defineOptions({ name: 'Login' }); | defineOptions({ name: 'Login' }); | ||||||
| 
 | 
 | ||||||
| const accessStore = useAccessStore(); | const accessStore = useAccessStore(); | ||||||
| const { notification } = App.useApp(); |  | ||||||
| 
 |  | ||||||
| /** |  | ||||||
|  * @param params 登录表单数据 |  | ||||||
|  */ |  | ||||||
| async function handleLogin(params: LoginAndRegisterParams) { |  | ||||||
|   const { userInfo } = await accessStore.authLogin(params); |  | ||||||
|   if (userInfo?.realName) { |  | ||||||
|     notification.success({ |  | ||||||
|       description: `${$t('authentication.loginSuccessDesc')}:${userInfo?.realName}`, |  | ||||||
|       duration: 3, |  | ||||||
|       message: $t('authentication.loginSuccess'), |  | ||||||
|     }); |  | ||||||
|   } |  | ||||||
| } |  | ||||||
| </script> | </script> | ||||||
| 
 | 
 | ||||||
| <template> | <template> | ||||||
|  | @ -33,6 +13,6 @@ async function handleLogin(params: LoginAndRegisterParams) { | ||||||
|     :loading="accessStore.loading" |     :loading="accessStore.loading" | ||||||
|     password-placeholder="123456" |     password-placeholder="123456" | ||||||
|     username-placeholder="vben" |     username-placeholder="vben" | ||||||
|     @submit="handleLogin" |     @submit="accessStore.authLogin" | ||||||
|   /> |   /> | ||||||
| </template> | </template> | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	 vince
						vince