fix: 修复登录加载菜单失败问题

This commit is contained in:
2026-08-18 11:06:08 +08:00
parent a1c9c47958
commit 8c78f5730e
4 changed files with 34 additions and 42 deletions
@@ -98,15 +98,6 @@ export async function refreshTokenApi() {
);
}
/**
* 退出登录
*/
export async function logoutApi() {
return baseRequestClient.post('/auth/logout', {
withCredentials: true,
});
}
/**
* 获取用户权限码
*/
@@ -107,13 +107,30 @@ function setupAccessGuard(router: Router) {
accessStore.setAccessMenus(accessibleMenus);
accessStore.setAccessRoutes(accessibleRoutes);
accessStore.setIsAccessChecked(true);
const redirectPath = (from.query.redirect ??
(to.path === preferences.app.defaultHomePath
? userInfo.homePath || preferences.app.defaultHomePath
: to.fullPath)) as string;
let redirectPath = to.fullPath;
if (typeof from.query.redirect === 'string' && from.query.redirect) {
const decoded = decodeURIComponent(from.query.redirect);
if (decoded !== LOGIN_PATH && !decoded.startsWith(`${LOGIN_PATH}?`)) {
redirectPath = decoded;
} else {
redirectPath =
userInfo.homePath || preferences.app.defaultHomePath;
}
} else if (to.path === preferences.app.defaultHomePath) {
redirectPath =
userInfo.homePath || preferences.app.defaultHomePath;
}
const resolved = router.resolve(redirectPath);
// 已在目标页则放行;不要展开 resolve(),以免带上过期的 matched(当时可能命中 404)
if (resolved.name && resolved.name === to.name) {
return true;
}
return {
...router.resolve(decodeURIComponent(redirectPath)),
path: resolved.path,
query: resolved.query,
hash: resolved.hash,
replace: true,
};
});
@@ -16,7 +16,6 @@ import {
getAccessCodesApi,
getUserInfoApi,
loginApi,
logoutApi,
registerApi,
} from '#/api';
import { $t } from '#/locales';
@@ -182,11 +181,6 @@ export const useAuthStore = defineStore('auth', () => {
}
async function logout(redirect: boolean = true) {
try {
await logoutApi();
} catch {
// 不做任何处理
}
resetAllStores();
accessStore.setLoginExpired(false);
@@ -28,43 +28,33 @@ async function generateAccessible(
// 生成路由
const accessibleRoutes = await generateRoutes(mode, options);
const root = router.getRoutes().find((item) => item.path === '/');
// 优先按 name 找 Root:getRoutes() 扁平化后 path === '/' 也可能命中别的记录
const root =
router.getRoutes().find((item) => item.name === 'Root') ??
router.getRoutes().find((item) => item.path === '/');
// 获取已有的路由名称列表
const names = root?.children?.map((item) => item.name) ?? [];
// 动态添加到router实例内
// 动态路由挂到 Root 下。不要 removeRoute(Root) 再整棵加回:
// 登录后会进入已是 Root 子路由的 /welcome,卸掉 Root 会取消这次导航,停在「加载菜单中」。
accessibleRoutes.forEach((route) => {
if (root && !route.meta?.noBasicLayout) {
// 为了兼容之前的版本用法,如果包含子路由,则将component移除,以免出现多层BasicLayout
// 如果你的项目已经跟进了本次修改,移除了所有自定义菜单首级的BasicLayout,可以将这段if代码删除
if (route.children && route.children.length > 0) {
delete route.component;
}
// 根据router name判断,如果路由已经存在,则不再添加
if (names?.includes(route.name)) {
// 找到已存在的路由索引并更新,不更新会造成切换用户时,一级目录未更新,homePath 在二级目录导致的404问题
const index = root.children?.findIndex(
(item) => item.name === route.name,
);
if (index !== undefined && index !== -1 && root.children) {
root.children[index] = route;
const parentName = root.name;
if (parentName) {
if (route.name && router.hasRoute(route.name)) {
router.removeRoute(route.name);
}
router.addRoute(parentName, route);
} else {
root.children?.push(route);
router.addRoute(route);
}
} else {
router.addRoute(route);
}
});
if (root) {
if (root.name) {
router.removeRoute(root.name);
}
router.addRoute(root);
}
// 生成菜单
const accessibleMenus = generateMenus(accessibleRoutes, options.router);