Files
xk-admin/apps/web-antd/src/store/auth.ts

130 lines
3.4 KiB
TypeScript
Raw Normal View History

import type { Recordable, 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';
2025-03-06 19:12:26 +08:00
import { useWatermark } from '@vben/hooks';
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 { getAccessCodesApi, getUserInfoApi, loginApi, logoutApi } from '#/api';
2024-07-11 21:48:56 +08:00
import { $t } from '#/locales';
2025-03-06 19:12:26 +08:00
const { updateWatermark } = useWatermark();
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
2025-03-06 19:12:26 +08:00
* @param onSuccess
*/
async function authLogin(
params: Recordable<any>,
2024-07-11 21:48:56 +08:00
onSuccess?: () => Promise<void> | void,
) {
2025-03-06 19:12:26 +08:00
// 异步处理用户登录操作并获取 token
let userInfo: null | UserInfo = null;
try {
2024-07-30 21:10:28 +08:00
loginLoading.value = true;
2025-03-06 19:12:26 +08:00
const { token } = await loginApi(params);
2025-03-06 19:12:26 +08:00
// 如果成功获取到 token
if (token) {
accessStore.setAccessToken(token);
// 获取用户信息并存储到 accessStore 中
const [fetchUserInfoResult, accessCodes] = await Promise.all([
fetchUserInfo(),
2024-08-07 13:42:33 +08:00
getAccessCodesApi(),
]);
userInfo = fetchUserInfoResult;
2024-07-30 21:10:28 +08:00
userStore.setUserInfo(userInfo);
accessStore.setAccessCodes(accessCodes);
2025-04-01 15:21:20 +08:00
console.log(userInfo.home_path);
console.log(DEFAULT_HOME_PATH);
2025-04-07 17:02:39 +08:00
console.log(userInfo.home_path || DEFAULT_HOME_PATH, 'ssssssssssssssss');
console.log(userInfo.home_path || DEFAULT_HOME_PATH, 'ssssssssssssssss');
2024-07-30 21:10:28 +08:00
if (accessStore.loginExpired) {
accessStore.setLoginExpired(false);
2024-07-11 21:48:56 +08:00
} else {
onSuccess
? await onSuccess?.()
2025-03-06 19:12:26 +08:00
: await router.push(userInfo.home_path || DEFAULT_HOME_PATH);
2024-07-11 21:48:56 +08:00
}
2025-03-06 19:12:26 +08:00
if (userInfo?.nick_name) {
// 这里修改水印内容
await updateWatermark({
contentType: 'multi-line-text',
// 水印内容
content: `${userInfo?.nick_name}\r\n${userInfo?.phone}`,
});
2024-07-11 21:48:56 +08:00
notification.success({
2025-03-06 19:12:26 +08:00
description: `${$t('authentication.loginSuccessDesc')}:${userInfo?.nick_name}`,
2024-07-11 21:48:56 +08:00
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) {
try {
await logoutApi();
} catch {
// 不做任何处理
}
resetAllStores();
2024-07-30 21:10:28 +08:00
accessStore.setLoginExpired(false);
// 回登录页带上当前路由地址
await router.replace({
path: LOGIN_PATH,
query: redirect
? {
redirect: encodeURIComponent(router.currentRoute.value.fullPath),
}
: {},
});
}
async function fetchUserInfo() {
let userInfo: null | UserInfo = null;
2024-08-07 13:42:33 +08:00
userInfo = await getUserInfoApi();
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,
};
});