mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 13:09:19 +08:00
243 lines
6.4 KiB
TypeScript
243 lines
6.4 KiB
TypeScript
import type { Recordable, UserInfo } from '@vben/types';
|
|
|
|
import type { AuthApi } from '#/api/core/auth';
|
|
|
|
import { ref } from 'vue';
|
|
import { useRouter } from 'vue-router';
|
|
|
|
import { LOGIN_PATH } from '@vben/constants';
|
|
import { preferences } from '@vben/preferences';
|
|
import { resetAllStores, useAccessStore, useUserStore } from '@vben/stores';
|
|
|
|
import { notification } from 'ant-design-vue';
|
|
import { defineStore } from 'pinia';
|
|
|
|
import {
|
|
getAccessCodesApi,
|
|
getUserInfoApi,
|
|
loginApi,
|
|
registerApi,
|
|
} from '#/api';
|
|
import { $t } from '#/locales';
|
|
import { useAppConfigStore } from '#/store/app-config';
|
|
|
|
export const useAuthStore = defineStore('auth', () => {
|
|
const accessStore = useAccessStore();
|
|
const userStore = useUserStore();
|
|
const appConfigStore = useAppConfigStore();
|
|
const router = useRouter();
|
|
|
|
const loginLoading = ref(false);
|
|
|
|
/**
|
|
* 异步处理登录操作
|
|
* Asynchronously handle the login process
|
|
* @param params 登录表单数据
|
|
*/
|
|
async function authLogin(
|
|
params: Recordable<any>,
|
|
onSuccess?: () => Promise<void> | void,
|
|
) {
|
|
// 异步处理用户登录操作并获取 token
|
|
let userInfo: null | UserInfo = null;
|
|
try {
|
|
loginLoading.value = true;
|
|
// 只提取必要的登录字段
|
|
const loginParams: AuthApi.LoginParams = {
|
|
account: params.account,
|
|
password: params.password,
|
|
};
|
|
const {
|
|
accessToken,
|
|
refreshToken,
|
|
accessTokenExpiresAt,
|
|
refreshTokenExpiresAt,
|
|
} = await loginApi(loginParams);
|
|
|
|
// 如果成功获取到 token
|
|
if (accessToken) {
|
|
accessStore.setTokens(
|
|
accessToken,
|
|
refreshToken,
|
|
accessTokenExpiresAt,
|
|
refreshTokenExpiresAt,
|
|
);
|
|
|
|
// 获取用户信息
|
|
userInfo = await fetchUserInfo();
|
|
|
|
// 系统配置(上传模式等)
|
|
try {
|
|
await appConfigStore.fetchConfig(true);
|
|
} catch (error) {
|
|
console.warn('获取系统配置失败:', error);
|
|
}
|
|
|
|
// 获取用户权限码(可选,如果接口不存在则跳过)
|
|
try {
|
|
const accessCodes = await getAccessCodesApi();
|
|
accessStore.setAccessCodes(accessCodes);
|
|
} catch (error) {
|
|
console.warn('获取用户权限码失败,使用默认权限:', error);
|
|
accessStore.setAccessCodes([]);
|
|
}
|
|
|
|
if (accessStore.loginExpired) {
|
|
accessStore.setLoginExpired(false);
|
|
} else {
|
|
onSuccess
|
|
? await onSuccess?.()
|
|
: await router.push(
|
|
userInfo.homePath || preferences.app.defaultHomePath,
|
|
);
|
|
}
|
|
|
|
if (userInfo?.realName) {
|
|
notification.success({
|
|
description: `${$t('authentication.loginSuccessDesc')}:${userInfo?.realName}`,
|
|
duration: 3,
|
|
message: $t('authentication.loginSuccess'),
|
|
});
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('登录失败:', error);
|
|
// 登录失败时清除可能的token
|
|
accessStore.setAccessToken(null);
|
|
accessStore.setRefreshToken(null);
|
|
throw error;
|
|
} finally {
|
|
loginLoading.value = false;
|
|
}
|
|
|
|
return {
|
|
userInfo,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 异步处理注册操作
|
|
* Asynchronously handle the register process
|
|
* @param params 注册表单数据
|
|
*/
|
|
async function authRegister(
|
|
params: Recordable<any>,
|
|
onSuccess?: () => Promise<void> | void,
|
|
) {
|
|
// 异步处理用户注册操作并获取 token
|
|
let userInfo: null | UserInfo = null;
|
|
try {
|
|
loginLoading.value = true;
|
|
const {
|
|
accessToken,
|
|
refreshToken,
|
|
accessTokenExpiresAt,
|
|
refreshTokenExpiresAt,
|
|
} = await registerApi(params as any);
|
|
|
|
// 如果成功获取到 token
|
|
if (accessToken) {
|
|
accessStore.setTokens(
|
|
accessToken,
|
|
refreshToken,
|
|
accessTokenExpiresAt,
|
|
refreshTokenExpiresAt,
|
|
);
|
|
|
|
// 获取用户信息
|
|
userInfo = await fetchUserInfo();
|
|
|
|
// 系统配置(上传模式等)
|
|
try {
|
|
await appConfigStore.fetchConfig(true);
|
|
} catch (error) {
|
|
console.warn('获取系统配置失败:', error);
|
|
}
|
|
|
|
// 获取用户权限码(可选,如果接口不存在则跳过)
|
|
try {
|
|
const accessCodes = await getAccessCodesApi();
|
|
accessStore.setAccessCodes(accessCodes);
|
|
} catch (error) {
|
|
console.warn('获取用户权限码失败,使用默认权限:', error);
|
|
accessStore.setAccessCodes([]);
|
|
}
|
|
|
|
if (accessStore.loginExpired) {
|
|
accessStore.setLoginExpired(false);
|
|
} else {
|
|
onSuccess
|
|
? await onSuccess?.()
|
|
: await router.push(
|
|
userInfo.homePath || preferences.app.defaultHomePath,
|
|
);
|
|
}
|
|
|
|
if (userInfo?.realName) {
|
|
notification.success({
|
|
description: `${$t('authentication.registerSuccessDesc')}:${userInfo?.realName}`,
|
|
duration: 3,
|
|
message: $t('authentication.registerSuccess'),
|
|
});
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('注册失败:', error);
|
|
// 注册失败时清除可能的token
|
|
accessStore.setAccessToken(null);
|
|
accessStore.setRefreshToken(null);
|
|
throw error;
|
|
} finally {
|
|
loginLoading.value = false;
|
|
}
|
|
|
|
return {
|
|
userInfo,
|
|
};
|
|
}
|
|
|
|
async function logout(redirect: boolean = true) {
|
|
resetAllStores();
|
|
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;
|
|
const user = await getUserInfoApi();
|
|
// 添加兼容性字段
|
|
userInfo = {
|
|
...user,
|
|
userId: user.id.toString(),
|
|
username: user.loginCode,
|
|
desc: user.remark || '',
|
|
homePath: preferences.app.defaultHomePath,
|
|
avatar: user.avatar || '',
|
|
};
|
|
userStore.setUserInfo(userInfo as any);
|
|
return userInfo;
|
|
}
|
|
|
|
function $reset() {
|
|
loginLoading.value = false;
|
|
}
|
|
|
|
return {
|
|
$reset,
|
|
authLogin,
|
|
authRegister,
|
|
fetchUserInfo,
|
|
loginLoading,
|
|
logout,
|
|
};
|
|
});
|