2026-07-14 10:31:17 +08:00
|
|
|
// src/hooks/useAuth.ts
|
|
|
|
|
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-04 10:16:32 +08:00
|
|
|
import { clearAuthDataCache } from '@/api/menu';
|
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';
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 登录:保存 token → 拉取菜单+权限 → 动态注册路由 → 跳转首页
|
|
|
|
|
*/
|
2026-07-29 09:32:14 +08:00
|
|
|
const login = async (newToken: string, targetPath?: string) => {
|
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-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 = () => {
|
|
|
|
|
setToken(null);
|
2026-07-23 11:27:33 +08:00
|
|
|
|
|
|
|
|
const { clearMenu } = useMenuStore();
|
|
|
|
|
const { clearPermissions } = usePermissionStore();
|
2026-07-29 09:32:14 +08:00
|
|
|
const { clearTabs } = useTabsStore();
|
2026-07-23 11:27:33 +08:00
|
|
|
|
2026-07-29 09:32:14 +08:00
|
|
|
clearTabs();
|
2026-07-23 11:27:33 +08:00
|
|
|
clearMenu();
|
|
|
|
|
clearPermissions();
|
2026-08-04 10:16:32 +08:00
|
|
|
clearAuthDataCache();
|
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,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const auth = createAuth();
|