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

124 lines
3.0 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 { DEFAULT_HOME_PATH, LOGIN_PATH } from '@vben/constants';
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';
import { doLogout, getUserInfoApi, loginApi } from '#/api';
2024-07-11 21:48:56 +08:00
import { $t } from '#/locales';
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(DEFAULT_HOME_PATH);
}
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() {
2024-08-07 08:57:56 +08:00
try {
await doLogout();
} catch (error) {
console.error(error);
} finally {
resetAllStores();
accessStore.setLoginExpired(false);
// 回登陆页带上当前路由地址
await router.replace({
path: LOGIN_PATH,
query: {
redirect: encodeURIComponent(router.currentRoute.value.fullPath),
},
});
}
}
async function fetchUserInfo() {
const { permissions = [], roles = [], user } = await getUserInfoApi();
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,
};
2024-07-30 21:10:28 +08:00
userStore.setUserInfo(userInfo);
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,
};
});