fix: 优化登录相关权限问题 添加按键权限判断
This commit is contained in:
+43
-12
@@ -1,14 +1,18 @@
|
||||
// src/hooks/useAuth.ts
|
||||
import { notification } from 'ant-design-vue';
|
||||
import { useState } from './useState';
|
||||
import { useEffect } from './useEffect';
|
||||
import { useMenuStore } from '@/stores/menuStore';
|
||||
import { usePermissionStore } from '@/stores/permissionStore';
|
||||
import { useTabsStore } from '@/stores/tabsStore';
|
||||
import { clearAuthDataCache } from '@/api/menu';
|
||||
import { useUserStore } from '@/stores/userStore';
|
||||
import { clearAuthDataCache } from '@/api/authCache';
|
||||
import router from '@/router';
|
||||
|
||||
const TOKEN_KEY = 'MY_APP_AUTH_TOKEN';
|
||||
|
||||
let isForceLoggingOut = false;
|
||||
|
||||
function createAuth() {
|
||||
const [token, setToken] = useState<string | null>(window.localStorage.getItem(TOKEN_KEY));
|
||||
|
||||
@@ -23,10 +27,26 @@ function createAuth() {
|
||||
|
||||
const isLoggedIn = () => !!token.value;
|
||||
|
||||
const clearSession = () => {
|
||||
setToken(null);
|
||||
|
||||
const { clearUser } = useUserStore();
|
||||
const { clearMenu } = useMenuStore();
|
||||
const { clearPermissions } = usePermissionStore();
|
||||
const { clearTabs } = useTabsStore();
|
||||
|
||||
clearUser();
|
||||
clearTabs();
|
||||
clearMenu();
|
||||
clearPermissions();
|
||||
clearAuthDataCache();
|
||||
};
|
||||
|
||||
/**
|
||||
* 登录:保存 token → 拉取菜单+权限 → 动态注册路由 → 跳转首页
|
||||
*/
|
||||
const login = async (newToken: string, targetPath?: string) => {
|
||||
isForceLoggingOut = false;
|
||||
setToken(newToken);
|
||||
|
||||
const { loadMenu, homePath, isRoutePathAvailable } = useMenuStore();
|
||||
@@ -35,6 +55,9 @@ function createAuth() {
|
||||
// 并行拉取菜单和权限
|
||||
await Promise.all([loadMenu(), loadPermissions()]);
|
||||
|
||||
// 权限获取失败时会清空会话,此时不再跳转首页
|
||||
if (!isLoggedIn()) return;
|
||||
|
||||
const nextPath =
|
||||
targetPath && isRoutePathAvailable(targetPath) ? targetPath : homePath.value || '/404';
|
||||
|
||||
@@ -46,17 +69,7 @@ function createAuth() {
|
||||
* 退出:清除 token → 清除菜单+权限 → 清除动态路由 → 跳转登录页
|
||||
*/
|
||||
const logout = () => {
|
||||
setToken(null);
|
||||
|
||||
const { clearMenu } = useMenuStore();
|
||||
const { clearPermissions } = usePermissionStore();
|
||||
const { clearTabs } = useTabsStore();
|
||||
|
||||
clearTabs();
|
||||
clearMenu();
|
||||
clearPermissions();
|
||||
clearAuthDataCache();
|
||||
|
||||
clearSession();
|
||||
router.push('/login');
|
||||
};
|
||||
|
||||
@@ -65,7 +78,25 @@ function createAuth() {
|
||||
isLoggedIn,
|
||||
login,
|
||||
logout,
|
||||
clearSession,
|
||||
};
|
||||
}
|
||||
|
||||
export const auth = createAuth();
|
||||
|
||||
/**
|
||||
* 强制重新登录:提示错误 → 清空会话 → 跳转登录页
|
||||
* 用于权限获取失败、登录态失效等需要彻底重置会话的场景
|
||||
*/
|
||||
export function forceReLogin(msg = '账户权限获取失败,请重新登录') {
|
||||
if (isForceLoggingOut) return;
|
||||
isForceLoggingOut = true;
|
||||
|
||||
notification.error({
|
||||
message: '提示',
|
||||
description: msg,
|
||||
duration: 2600,
|
||||
});
|
||||
auth.clearSession();
|
||||
router.push('/login');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user