2026-07-14 10:31:17 +08:00
|
|
|
// src/hooks/useAuth.ts
|
2026-08-05 15:30:09 +08:00
|
|
|
import { notification } from 'ant-design-vue';
|
2026-07-14 10:31:17 +08:00
|
|
|
import { useState } from './useState';
|
2026-07-27 09:51:30 +08:00
|
|
|
import { useEffect } from './useEffect';
|
2026-07-23 11:27:33 +08:00
|
|
|
import { useMenuStore } from '@/stores/menuStore';
|
|
|
|
|
import { usePermissionStore } from '@/stores/permissionStore';
|
2026-07-29 09:32:14 +08:00
|
|
|
import { useTabsStore } from '@/stores/tabsStore';
|
2026-08-05 15:30:09 +08:00
|
|
|
import { useUserStore } from '@/stores/userStore';
|
|
|
|
|
import { clearAuthDataCache } from '@/api/authCache';
|
2026-07-23 15:00:19 +08:00
|
|
|
import router from '@/router';
|
2026-07-14 10:31:17 +08:00
|
|
|
|
|
|
|
|
const TOKEN_KEY = 'MY_APP_AUTH_TOKEN';
|
|
|
|
|
|
2026-08-05 15:30:09 +08:00
|
|
|
let isForceLoggingOut = false;
|
|
|
|
|
|
2026-07-14 10:31:17 +08:00
|
|
|
function createAuth() {
|
|
|
|
|
const [token, setToken] = useState<string | null>(window.localStorage.getItem(TOKEN_KEY));
|
|
|
|
|
|
2026-07-27 09:51:30 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
const newToken = token.value;
|
2026-07-14 10:31:17 +08:00
|
|
|
if (newToken) {
|
|
|
|
|
window.localStorage.setItem(TOKEN_KEY, newToken);
|
|
|
|
|
} else {
|
|
|
|
|
window.localStorage.removeItem(TOKEN_KEY);
|
|
|
|
|
}
|
2026-07-27 09:51:30 +08:00
|
|
|
}, [token]);
|
2026-07-14 10:31:17 +08:00
|
|
|
|
2026-07-23 11:27:33 +08:00
|
|
|
const isLoggedIn = () => !!token.value;
|
|
|
|
|
|
2026-08-05 15:30:09 +08:00
|
|
|
const clearSession = () => {
|
|
|
|
|
setToken(null);
|
|
|
|
|
|
|
|
|
|
const { clearUser } = useUserStore();
|
|
|
|
|
const { clearMenu } = useMenuStore();
|
|
|
|
|
const { clearPermissions } = usePermissionStore();
|
|
|
|
|
const { clearTabs } = useTabsStore();
|
|
|
|
|
|
|
|
|
|
clearUser();
|
|
|
|
|
clearTabs();
|
|
|
|
|
clearMenu();
|
|
|
|
|
clearPermissions();
|
|
|
|
|
clearAuthDataCache();
|
|
|
|
|
};
|
|
|
|
|
|
2026-07-23 11:27:33 +08:00
|
|
|
/**
|
|
|
|
|
* 登录:保存 token → 拉取菜单+权限 → 动态注册路由 → 跳转首页
|
|
|
|
|
*/
|
2026-07-29 09:32:14 +08:00
|
|
|
const login = async (newToken: string, targetPath?: string) => {
|
2026-08-05 15:30:09 +08:00
|
|
|
isForceLoggingOut = false;
|
2026-07-14 10:31:17 +08:00
|
|
|
setToken(newToken);
|
2026-07-23 11:27:33 +08:00
|
|
|
|
2026-07-29 09:32:14 +08:00
|
|
|
const { loadMenu, homePath, isRoutePathAvailable } = useMenuStore();
|
2026-07-23 11:27:33 +08:00
|
|
|
const { loadPermissions } = usePermissionStore();
|
|
|
|
|
|
|
|
|
|
// 并行拉取菜单和权限
|
|
|
|
|
await Promise.all([loadMenu(), loadPermissions()]);
|
|
|
|
|
|
2026-08-05 15:30:09 +08:00
|
|
|
// 权限获取失败时会清空会话,此时不再跳转首页
|
|
|
|
|
if (!isLoggedIn()) return;
|
|
|
|
|
|
2026-07-29 09:32:14 +08:00
|
|
|
const nextPath =
|
|
|
|
|
targetPath && isRoutePathAvailable(targetPath) ? targetPath : homePath.value || '/404';
|
|
|
|
|
|
|
|
|
|
// 动态路由注册完成后,再跳转到有效目标页
|
|
|
|
|
router.push(nextPath);
|
2026-07-14 10:31:17 +08:00
|
|
|
};
|
|
|
|
|
|
2026-07-23 11:27:33 +08:00
|
|
|
/**
|
|
|
|
|
* 退出:清除 token → 清除菜单+权限 → 清除动态路由 → 跳转登录页
|
|
|
|
|
*/
|
2026-07-14 10:31:17 +08:00
|
|
|
const logout = () => {
|
2026-08-05 15:30:09 +08:00
|
|
|
clearSession();
|
2026-07-23 11:27:33 +08:00
|
|
|
router.push('/login');
|
2026-07-14 10:31:17 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
token,
|
2026-07-23 11:27:33 +08:00
|
|
|
isLoggedIn,
|
2026-07-14 10:31:17 +08:00
|
|
|
login,
|
|
|
|
|
logout,
|
2026-08-05 15:30:09 +08:00
|
|
|
clearSession,
|
2026-07-14 10:31:17 +08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const auth = createAuth();
|
2026-08-05 15:30:09 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 强制重新登录:提示错误 → 清空会话 → 跳转登录页
|
|
|
|
|
* 用于权限获取失败、登录态失效等需要彻底重置会话的场景
|
|
|
|
|
*/
|
|
|
|
|
export function forceReLogin(msg = '账户权限获取失败,请重新登录') {
|
|
|
|
|
if (isForceLoggingOut) return;
|
|
|
|
|
isForceLoggingOut = true;
|
|
|
|
|
|
|
|
|
|
notification.error({
|
|
|
|
|
message: '提示',
|
|
|
|
|
description: msg,
|
|
|
|
|
duration: 2600,
|
|
|
|
|
});
|
|
|
|
|
auth.clearSession();
|
|
|
|
|
router.push('/login');
|
|
|
|
|
}
|