chore(release): unify to wwjcloud across backend/frontend; routes, DTO/VO paths, docs/links; remove niucloud; naming fixes
This commit is contained in:
204
admin-vben/apps/web-antd/src/store/auth-migrated.ts
Normal file
204
admin-vben/apps/web-antd/src/store/auth-migrated.ts
Normal file
@@ -0,0 +1,204 @@
|
||||
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 { getAuthMenusApi, getSiteInfoApi, getUserInfoApi, loginApi, logoutApi } from '#/api';
|
||||
import { $t } from '#/locales';
|
||||
|
||||
export const useAuthStore = defineStore('auth', () => {
|
||||
const accessStore = useAccessStore();
|
||||
const userStore = useUserStore();
|
||||
const router = useRouter();
|
||||
|
||||
const loginLoading = ref(false);
|
||||
|
||||
/**
|
||||
* 异步处理登录操作(适配Java admin逻辑)
|
||||
* @param params 登录表单数据 { username, password, loginType }
|
||||
* @param onSuccess 成功之后的回调函数
|
||||
*/
|
||||
async function authLogin(
|
||||
params: Recordable<any>,
|
||||
onSuccess?: () => Promise<void> | void,
|
||||
) {
|
||||
let userInfo: null | UserInfo & { siteInfo?: any; userrole?: any[] } = null;
|
||||
try {
|
||||
loginLoading.value = true;
|
||||
|
||||
// 调用Java admin的登录API
|
||||
const loginResponse = await loginApi(
|
||||
{
|
||||
username: params.username,
|
||||
password: params.password,
|
||||
captcha_code: params.captcha_code,
|
||||
},
|
||||
params.loginType || 'admin',
|
||||
);
|
||||
|
||||
// Java admin返回的数据结构处理
|
||||
const { data } = loginResponse;
|
||||
|
||||
if (data && data.token) {
|
||||
// 设置访问令牌
|
||||
accessStore.setAccessToken(data.token);
|
||||
|
||||
// 获取用户信息和权限信息
|
||||
const [fetchUserInfoResult, authMenus, siteInfo] = await Promise.all([
|
||||
getUserInfoApi(),
|
||||
getAuthMenusApi(),
|
||||
getSiteInfoApi(),
|
||||
]);
|
||||
|
||||
userInfo = {
|
||||
...fetchUserInfoResult,
|
||||
siteInfo: siteInfo.data,
|
||||
userrole: data.userrole || [],
|
||||
};
|
||||
|
||||
// 存储用户信息
|
||||
userStore.setUserInfo(userInfo);
|
||||
|
||||
// 存储权限信息到accessStore
|
||||
if (authMenus.data) {
|
||||
accessStore.setAccessCodes(authMenus.data);
|
||||
}
|
||||
|
||||
// 处理登录过期状态
|
||||
if (accessStore.loginExpired) {
|
||||
accessStore.setLoginExpired(false);
|
||||
} else {
|
||||
// 登录成功后的跳转逻辑
|
||||
if (onSuccess) {
|
||||
await onSuccess?.();
|
||||
} else {
|
||||
// Java admin的跳转逻辑
|
||||
if (params.loginType === 'admin' && (!data.userrole || data.userrole.length === 0)) {
|
||||
// 平台端登录且没有角色,跳转到首页
|
||||
await router.push('/home/index');
|
||||
} else {
|
||||
// 根据登录类型跳转到对应首页
|
||||
const homePath = params.loginType === 'admin' ? '/admin' : '/site';
|
||||
await router.push(homePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 登录成功提示
|
||||
if (userInfo?.realName) {
|
||||
notification.success({
|
||||
description: `${$t('authentication.loginSuccessDesc')}:${userInfo.realName}`,
|
||||
duration: 3,
|
||||
message: $t('authentication.loginSuccess'),
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('登录失败:', error);
|
||||
throw error;
|
||||
} finally {
|
||||
loginLoading.value = false;
|
||||
}
|
||||
|
||||
return {
|
||||
userInfo,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出登录(适配Java admin逻辑)
|
||||
*/
|
||||
async function logout(redirect: boolean = true) {
|
||||
try {
|
||||
await logoutApi();
|
||||
} catch {
|
||||
// 不做任何处理
|
||||
}
|
||||
|
||||
// 重置所有状态
|
||||
resetAllStores();
|
||||
accessStore.setLoginExpired(false);
|
||||
|
||||
// 清除本地存储的Java admin相关数据
|
||||
localStorage.removeItem('admin.token');
|
||||
localStorage.removeItem('admin.userinfo');
|
||||
localStorage.removeItem('admin.siteInfo');
|
||||
localStorage.removeItem('site.token');
|
||||
localStorage.removeItem('site.userinfo');
|
||||
localStorage.removeItem('site.siteInfo');
|
||||
localStorage.removeItem('siteId');
|
||||
localStorage.removeItem('comparisonSiteIdStorage');
|
||||
localStorage.removeItem('comparisonTokenStorage');
|
||||
|
||||
// 回登录页带上当前路由地址
|
||||
await router.replace({
|
||||
path: LOGIN_PATH,
|
||||
query: redirect
|
||||
? {
|
||||
redirect: encodeURIComponent(router.currentRoute.value.fullPath),
|
||||
}
|
||||
: {},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户信息
|
||||
*/
|
||||
async function fetchUserInfo() {
|
||||
let userInfo: null | UserInfo = null;
|
||||
try {
|
||||
userInfo = await getUserInfoApi();
|
||||
userStore.setUserInfo(userInfo);
|
||||
} catch (error) {
|
||||
console.error('获取用户信息失败:', error);
|
||||
}
|
||||
return userInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取权限菜单(适配Java admin逻辑)
|
||||
*/
|
||||
async function fetchAuthMenus() {
|
||||
try {
|
||||
const response = await getAuthMenusApi();
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error('获取权限菜单失败:', error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取站点信息
|
||||
*/
|
||||
async function fetchSiteInfo() {
|
||||
try {
|
||||
const response = await getSiteInfoApi();
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error('获取站点信息失败:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function $reset() {
|
||||
loginLoading.value = false;
|
||||
}
|
||||
|
||||
return {
|
||||
$reset,
|
||||
authLogin,
|
||||
fetchUserInfo,
|
||||
fetchAuthMenus,
|
||||
fetchSiteInfo,
|
||||
loginLoading,
|
||||
logout,
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user