| | |
| | | socialLogin, |
| | | } from '#/api'; |
| | | import { $t } from '#/locales'; |
| | | import { resetTerminal, safeDestination, terminalHome, terminalType } from '#/mobile/terminal'; |
| | | |
| | | export const useAuthStore = defineStore('auth', () => { |
| | | const accessStore = useAccessStore(); |
| | |
| | | const router = useRouter(); |
| | | |
| | | const loginLoading = ref(false); |
| | | const loadedTerminal = ref<string>(); |
| | | |
| | | /** |
| | | * 异步处理登录操作 |
| | |
| | | |
| | | // 如果成功获取到 accessToken |
| | | if (accessToken) { |
| | | accessStore.setIsAccessChecked(false); |
| | | accessStore.setAccessToken(accessToken); |
| | | accessStore.setRefreshToken(refreshToken); |
| | | |
| | |
| | | onSuccess |
| | | ? await onSuccess?.() |
| | | : await router.push( |
| | | userInfo.homePath || preferences.app.defaultHomePath, |
| | | safeDestination(router.currentRoute.value.query.redirect, |
| | | terminalHome() || userInfo.homePath || preferences.app.defaultHomePath), |
| | | ); |
| | | } |
| | | |
| | | if (userInfo?.nickname) { |
| | | if (userInfo?.nickname && terminalType.value === 'pc') { |
| | | notification.success({ |
| | | description: `${$t('authentication.loginSuccessDesc')}:${userInfo?.nickname}`, |
| | | duration: 3, |
| | |
| | | }; |
| | | } |
| | | |
| | | async function logout(redirect: boolean = true) { |
| | | async function logout(redirect: boolean = true, preserveTerminal: boolean = false) { |
| | | try { |
| | | const accessToken = accessStore.accessToken as string; |
| | | if (accessToken) { |
| | |
| | | // 不做任何处理 |
| | | } |
| | | resetAllStores(); |
| | | if (!preserveTerminal) resetTerminal(); |
| | | loadedTerminal.value = undefined; |
| | | accessStore.setLoginExpired(false); |
| | | |
| | | // 回登录页带上当前路由地址 |
| | |
| | | // 加载 |
| | | // eslint-disable-next-line no-useless-assignment |
| | | let authPermissionInfo: AuthPermissionInfo | null = null; |
| | | authPermissionInfo = await getAuthPermissionInfoApi(); |
| | | authPermissionInfo = await getAuthPermissionInfoApi(terminalType.value); |
| | | // userStore |
| | | userStore.setUserInfo(authPermissionInfo.user); |
| | | userStore.setUserRoles(authPermissionInfo.roles); |
| | | // accessStore |
| | | accessStore.setAccessMenus(authPermissionInfo.menus); |
| | | accessStore.setAccessCodes(authPermissionInfo.permissions); |
| | | loadedTerminal.value = terminalType.value; |
| | | return authPermissionInfo; |
| | | } |
| | | |
| | | function $reset() { |
| | | loginLoading.value = false; |
| | | loadedTerminal.value = undefined; |
| | | } |
| | | |
| | | return { |
| | |
| | | authLogin, |
| | | fetchUserInfo, |
| | | loginLoading, |
| | | loadedTerminal, |
| | | logout, |
| | | }; |
| | | }); |