mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 13:09:20 +08:00
fix: 修复登录加载菜单失败问题
This commit is contained in:
@@ -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.setAccessMenus(accessibleMenus);
|
||||||
accessStore.setAccessRoutes(accessibleRoutes);
|
accessStore.setAccessRoutes(accessibleRoutes);
|
||||||
accessStore.setIsAccessChecked(true);
|
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 {
|
return {
|
||||||
...router.resolve(decodeURIComponent(redirectPath)),
|
path: resolved.path,
|
||||||
|
query: resolved.query,
|
||||||
|
hash: resolved.hash,
|
||||||
replace: true,
|
replace: true,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ import {
|
|||||||
getAccessCodesApi,
|
getAccessCodesApi,
|
||||||
getUserInfoApi,
|
getUserInfoApi,
|
||||||
loginApi,
|
loginApi,
|
||||||
logoutApi,
|
|
||||||
registerApi,
|
registerApi,
|
||||||
} from '#/api';
|
} from '#/api';
|
||||||
import { $t } from '#/locales';
|
import { $t } from '#/locales';
|
||||||
@@ -182,11 +181,6 @@ export const useAuthStore = defineStore('auth', () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function logout(redirect: boolean = true) {
|
async function logout(redirect: boolean = true) {
|
||||||
try {
|
|
||||||
await logoutApi();
|
|
||||||
} catch {
|
|
||||||
// 不做任何处理
|
|
||||||
}
|
|
||||||
resetAllStores();
|
resetAllStores();
|
||||||
accessStore.setLoginExpired(false);
|
accessStore.setLoginExpired(false);
|
||||||
|
|
||||||
|
|||||||
@@ -28,43 +28,33 @@ async function generateAccessible(
|
|||||||
// 生成路由
|
// 生成路由
|
||||||
const accessibleRoutes = await generateRoutes(mode, options);
|
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 === '/');
|
||||||
|
|
||||||
// 获取已有的路由名称列表
|
// 动态路由挂到 Root 下。不要 removeRoute(Root) 再整棵加回:
|
||||||
const names = root?.children?.map((item) => item.name) ?? [];
|
// 登录后会进入已是 Root 子路由的 /welcome,卸掉 Root 会取消这次导航,停在「加载菜单中」。
|
||||||
|
|
||||||
// 动态添加到router实例内
|
|
||||||
accessibleRoutes.forEach((route) => {
|
accessibleRoutes.forEach((route) => {
|
||||||
if (root && !route.meta?.noBasicLayout) {
|
if (root && !route.meta?.noBasicLayout) {
|
||||||
// 为了兼容之前的版本用法,如果包含子路由,则将component移除,以免出现多层BasicLayout
|
// 为了兼容之前的版本用法,如果包含子路由,则将component移除,以免出现多层BasicLayout
|
||||||
// 如果你的项目已经跟进了本次修改,移除了所有自定义菜单首级的BasicLayout,可以将这段if代码删除
|
|
||||||
if (route.children && route.children.length > 0) {
|
if (route.children && route.children.length > 0) {
|
||||||
delete route.component;
|
delete route.component;
|
||||||
}
|
}
|
||||||
// 根据router name判断,如果路由已经存在,则不再添加
|
const parentName = root.name;
|
||||||
if (names?.includes(route.name)) {
|
if (parentName) {
|
||||||
// 找到已存在的路由索引并更新,不更新会造成切换用户时,一级目录未更新,homePath 在二级目录导致的404问题
|
if (route.name && router.hasRoute(route.name)) {
|
||||||
const index = root.children?.findIndex(
|
router.removeRoute(route.name);
|
||||||
(item) => item.name === route.name,
|
|
||||||
);
|
|
||||||
if (index !== undefined && index !== -1 && root.children) {
|
|
||||||
root.children[index] = route;
|
|
||||||
}
|
}
|
||||||
|
router.addRoute(parentName, route);
|
||||||
} else {
|
} else {
|
||||||
root.children?.push(route);
|
router.addRoute(route);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
router.addRoute(route);
|
router.addRoute(route);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (root) {
|
|
||||||
if (root.name) {
|
|
||||||
router.removeRoute(root.name);
|
|
||||||
}
|
|
||||||
router.addRoute(root);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成菜单
|
// 生成菜单
|
||||||
const accessibleMenus = generateMenus(accessibleRoutes, options.router);
|
const accessibleMenus = generateMenus(accessibleRoutes, options.router);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user