2024-07-13 17:25:15 +08:00
|
|
|
import type { LoginAndRegisterParams } from '@vben/common-ui';
|
2024-07-30 21:10:28 +08:00
|
|
|
import type { UserInfo } from '@vben/types';
|
2024-07-05 23:15:46 +08:00
|
|
|
|
2024-07-30 21:10:28 +08:00
|
|
|
import { ref } from 'vue';
|
2024-07-05 23:15:46 +08:00
|
|
|
import { useRouter } from 'vue-router';
|
|
|
|
|
2025-05-03 16:03:08 +08:00
|
|
|
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-05 23:15:46 +08:00
|
|
|
|
2024-07-11 21:48:56 +08:00
|
|
|
import { notification } from 'ant-design-vue';
|
2024-07-05 23:15:46 +08:00
|
|
|
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-07-05 23:15:46 +08:00
|
|
|
|
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();
|
2024-07-05 23:15:46 +08:00
|
|
|
const router = useRouter();
|
2024-07-11 21:48:56 +08:00
|
|
|
|
2024-07-30 21:10:28 +08:00
|
|
|
const loginLoading = ref(false);
|
2024-07-05 23:15:46 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 异步处理登录操作
|
|
|
|
* Asynchronously handle the login process
|
|
|
|
* @param params 登录表单数据
|
|
|
|
*/
|
|
|
|
async function authLogin(
|
|
|
|
params: LoginAndRegisterParams,
|
2024-07-11 21:48:56 +08:00
|
|
|
onSuccess?: () => Promise<void> | void,
|
2024-07-05 23:15:46 +08:00
|
|
|
) {
|
|
|
|
// 异步处理用户登录操作并获取 accessToken
|
2024-07-23 00:03:59 +08:00
|
|
|
let userInfo: null | UserInfo = null;
|
2024-07-05 23:15:46 +08:00
|
|
|
try {
|
2024-07-30 21:10:28 +08:00
|
|
|
loginLoading.value = true;
|
2024-08-08 08:03:04 +08:00
|
|
|
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 {
|
2025-05-04 17:23:32 +08:00
|
|
|
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'),
|
|
|
|
});
|
2024-07-05 23:15:46 +08:00
|
|
|
}
|
|
|
|
} finally {
|
2024-07-30 21:10:28 +08:00
|
|
|
loginLoading.value = false;
|
2024-07-05 23:15:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
userInfo,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-08-19 22:59:42 +08:00
|
|
|
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,
|
2024-08-20 08:44:26 +08:00
|
|
|
query: redirect
|
|
|
|
? {
|
|
|
|
redirect: encodeURIComponent(router.currentRoute.value.fullPath),
|
|
|
|
}
|
|
|
|
: {},
|
2024-08-07 08:57:56 +08:00
|
|
|
});
|
|
|
|
}
|
2024-07-11 20:11:11 +08:00
|
|
|
}
|
|
|
|
|
2024-07-05 23:15:46 +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,
|
2025-06-13 15:16:06 +08:00
|
|
|
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();
|
2024-07-05 23:15:46 +08:00
|
|
|
return userInfo;
|
|
|
|
}
|
|
|
|
|
2024-07-23 00:03:59 +08:00
|
|
|
function $reset() {
|
2024-07-30 21:10:28 +08:00
|
|
|
loginLoading.value = false;
|
2024-07-05 23:15:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2024-07-23 00:03:59 +08:00
|
|
|
$reset,
|
2024-07-05 23:15:46 +08:00
|
|
|
authLogin,
|
|
|
|
fetchUserInfo,
|
2024-07-30 21:10:28 +08:00
|
|
|
loginLoading,
|
2024-07-11 20:11:11 +08:00
|
|
|
logout,
|
2024-07-05 23:15:46 +08:00
|
|
|
};
|
|
|
|
});
|