mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 02:29:19 +08:00
初始化项目
This commit is contained in:
@@ -0,0 +1,215 @@
|
||||
import type { Recordable, UserInfo } from '@vben/types';
|
||||
|
||||
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, logoutApi, registerApi } from '#/api';
|
||||
import { $t } from '#/locales';
|
||||
|
||||
export const useAuthStore = defineStore('auth', () => {
|
||||
const accessStore = useAccessStore();
|
||||
const userStore = useUserStore();
|
||||
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 { token, user } = await loginApi(params as any);
|
||||
|
||||
// 如果成功获取到 token
|
||||
if (token) {
|
||||
accessStore.setAccessToken(token);
|
||||
|
||||
// 设置用户信息,添加兼容性字段
|
||||
userInfo = {
|
||||
...user,
|
||||
userId: user.id.toString(),
|
||||
username: user.loginCode,
|
||||
desc: user.remark || '',
|
||||
homePath: '/dashboard',
|
||||
token: token,
|
||||
avatar: user.avatar || '',
|
||||
};
|
||||
userStore.setUserInfo(userInfo as any);
|
||||
|
||||
// 获取用户权限码(可选,如果接口不存在则跳过)
|
||||
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);
|
||||
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 { token, user } = await registerApi(params as any);
|
||||
|
||||
// 如果成功获取到 token
|
||||
if (token) {
|
||||
accessStore.setAccessToken(token);
|
||||
|
||||
// 设置用户信息,添加兼容性字段
|
||||
userInfo = {
|
||||
...user,
|
||||
userId: user.id.toString(),
|
||||
username: user.loginCode,
|
||||
desc: user.remark || '',
|
||||
homePath: '/dashboard',
|
||||
token: token,
|
||||
avatar: user.avatar || '',
|
||||
};
|
||||
userStore.setUserInfo(userInfo as any);
|
||||
|
||||
// 获取用户权限码(可选,如果接口不存在则跳过)
|
||||
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);
|
||||
throw error;
|
||||
} finally {
|
||||
loginLoading.value = false;
|
||||
}
|
||||
|
||||
return {
|
||||
userInfo,
|
||||
};
|
||||
}
|
||||
|
||||
async function logout(redirect: boolean = true) {
|
||||
try {
|
||||
await logoutApi();
|
||||
} catch {
|
||||
// 不做任何处理
|
||||
}
|
||||
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: '/dashboard',
|
||||
avatar: user.avatar || '',
|
||||
};
|
||||
userStore.setUserInfo(userInfo as any);
|
||||
return userInfo;
|
||||
}
|
||||
|
||||
function $reset() {
|
||||
loginLoading.value = false;
|
||||
}
|
||||
|
||||
return {
|
||||
$reset,
|
||||
authLogin,
|
||||
authRegister,
|
||||
fetchUserInfo,
|
||||
loginLoading,
|
||||
logout,
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user