admin-vben5/apps/web-antd/src/store/auth.ts

145 lines
3.6 KiB
TypeScript
Raw Normal View History

import type { LoginAndRegisterParams } from '@vben/common-ui';
2024-07-30 21:10:28 +08:00
import type { UserInfo } from '@vben/types';
2024-07-30 21:10:28 +08:00
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { LOGIN_PATH } from '@vben/constants';
import { preferences } from '@vben/preferences';
2024-07-30 21:10:28 +08:00
import { resetAllStores, useAccessStore, useUserStore } from '@vben/stores';
2024-07-11 21:48:56 +08:00
import { notification } from 'ant-design-vue';
import { defineStore } from 'pinia';
2024-09-12 16:45:12 +08:00
import { doLogout, getUserInfoApi, loginApi, seeConnectionClose } from '#/api';
2024-07-11 21:48:56 +08:00
import { $t } from '#/locales';
2024-10-10 09:23:18 +08:00
import { useDictStore } from './dict';
2024-07-30 21:10:28 +08:00
export const useAuthStore = defineStore('auth', () => {
const accessStore = useAccessStore();
const userStore = useUserStore();
const router = useRouter();
2024-07-11 21:48:56 +08:00
2024-07-30 21:10:28 +08:00
const loginLoading = ref(false);
/**
*
* Asynchronously handle the login process
* @param params
*/
async function authLogin(
params: LoginAndRegisterParams,
2024-07-11 21:48:56 +08:00
onSuccess?: () => Promise<void> | void,
) {
// 异步处理用户登录操作并获取 accessToken
let userInfo: null | UserInfo = null;
try {
2024-07-30 21:10:28 +08:00
loginLoading.value = true;
const { access_token } = await loginApi(params);
2024-08-07 08:57:56 +08:00
// 将 accessToken 存储到 accessStore 中
accessStore.setAccessToken(access_token);
accessStore.setRefreshToken(access_token);
// 获取用户信息并存储到 accessStore 中
userInfo = await fetchUserInfo();
/**
*
*/
userStore.setUserInfo(userInfo);
/**
*
*/
accessStore.setAccessCodes(userInfo.permissions);
if (accessStore.loginExpired) {
accessStore.setLoginExpired(false);
} else {
onSuccess
? await onSuccess?.()
: await router.push(preferences.app.defaultHomePath);
2024-08-07 08:57:56 +08:00
}
if (userInfo?.realName) {
notification.success({
description: `${$t('authentication.loginSuccessDesc')}:${userInfo?.realName}`,
duration: 3,
message: $t('authentication.loginSuccess'),
});
}
} finally {
2024-07-30 21:10:28 +08:00
loginLoading.value = false;
}
return {
userInfo,
};
}
async function logout(redirect: boolean = true) {
2024-08-07 08:57:56 +08:00
try {
2024-09-12 16:45:12 +08:00
await seeConnectionClose();
2024-08-07 08:57:56 +08:00
await doLogout();
} catch (error) {
console.error(error);
} finally {
resetAllStores();
accessStore.setLoginExpired(false);
// 回登陆页带上当前路由地址
await router.replace({
path: LOGIN_PATH,
query: redirect
? {
redirect: encodeURIComponent(router.currentRoute.value.fullPath),
}
: {},
2024-08-07 08:57:56 +08:00
});
}
}
async function fetchUserInfo() {
2024-10-10 08:13:09 +08:00
const backUserInfo = await getUserInfoApi();
/**
*
*/
if (!backUserInfo) {
throw new Error('获取用户信息失败.');
}
const { permissions = [], roles = [], user } = backUserInfo;
2024-08-07 08:57:56 +08:00
/**
* user -> vben user转换
*/
const userInfo: UserInfo = {
avatar: user.avatar ?? '',
permissions,
realName: user.nickName,
roles,
userId: user.userId,
username: user.userName,
email: user.email ?? '',
2024-08-07 08:57:56 +08:00
};
2024-07-30 21:10:28 +08:00
userStore.setUserInfo(userInfo);
2024-10-10 09:23:18 +08:00
/**
*
* 退
*/
const dictStore = useDictStore();
dictStore.resetCache();
return userInfo;
}
function $reset() {
2024-07-30 21:10:28 +08:00
loginLoading.value = false;
}
return {
$reset,
authLogin,
fetchUserInfo,
2024-07-30 21:10:28 +08:00
loginLoading,
logout,
};
});