|
|
@@ -16,6 +16,31 @@ import { useAuthStore } from '#/store';
|
|
|
|
|
|
import { generateAccess } from './access';
|
|
|
|
|
|
+type AccessibleMenu = {
|
|
|
+ children?: AccessibleMenu[];
|
|
|
+ path?: string;
|
|
|
+};
|
|
|
+
|
|
|
+/** 取权限菜单中第一个可访问的叶子页面路径 */
|
|
|
+function getFirstAccessiblePath(menus: AccessibleMenu[]): string | undefined {
|
|
|
+ for (const menu of menus) {
|
|
|
+ if (menu.children?.length) {
|
|
|
+ const childPath = getFirstAccessiblePath(menu.children);
|
|
|
+ if (childPath) return childPath;
|
|
|
+ } else if (menu.path) {
|
|
|
+ return menu.path;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function isHomeEntryPath(path: string) {
|
|
|
+ return (
|
|
|
+ path === DEFAULT_PATH ||
|
|
|
+ (!!preferences.app.defaultHomePath &&
|
|
|
+ path === preferences.app.defaultHomePath)
|
|
|
+ );
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
* 通用守卫配置
|
|
|
* @param router
|
|
|
@@ -62,7 +87,8 @@ function setupAccessGuard(router: Router) {
|
|
|
return decodeURIComponent(
|
|
|
(to.query?.redirect as string) ||
|
|
|
userStore.userInfo?.homePath ||
|
|
|
- preferences.app.defaultHomePath,
|
|
|
+ preferences.app.defaultHomePath ||
|
|
|
+ DEFAULT_PATH,
|
|
|
);
|
|
|
}
|
|
|
return true;
|
|
|
@@ -82,7 +108,7 @@ function setupAccessGuard(router: Router) {
|
|
|
path: LOGIN_PATH,
|
|
|
// 如不需要,直接删除 query
|
|
|
query:
|
|
|
- to.fullPath === preferences.app.defaultHomePath
|
|
|
+ isHomeEntryPath(to.fullPath)
|
|
|
? {}
|
|
|
: { redirect: encodeURIComponent(to.fullPath) },
|
|
|
// 携带当前跳转的页面,登录后重新跳转该页面
|
|
|
@@ -122,8 +148,7 @@ function setupAccessGuard(router: Router) {
|
|
|
// 则会在菜单中显示,但是访问会被重定向到403
|
|
|
routes: accessRoutes,
|
|
|
});
|
|
|
- const path =
|
|
|
- to.path === DEFAULT_PATH ? preferences.app.defaultHomePath : to.path;
|
|
|
+ const firstAccessiblePath = getFirstAccessiblePath(accessibleMenus);
|
|
|
const redirect =
|
|
|
decodeURIComponent(<string>from.query.redirect) === DEFAULT_PATH
|
|
|
? void 0
|
|
|
@@ -131,21 +156,22 @@ function setupAccessGuard(router: Router) {
|
|
|
|
|
|
if (!userInfo.homePath) {
|
|
|
userStore.updateHomePath(
|
|
|
- preferences.app.defaultHomePath || accessibleMenus[0]?.path,
|
|
|
+ firstAccessiblePath || preferences.app.defaultHomePath,
|
|
|
);
|
|
|
}
|
|
|
|
|
|
+ const resolvedHomePath =
|
|
|
+ userStore.userInfo?.homePath ||
|
|
|
+ firstAccessiblePath ||
|
|
|
+ preferences.app.defaultHomePath;
|
|
|
+
|
|
|
// 保存菜单信息和路由信息
|
|
|
accessStore.setAccessMenus(accessibleMenus);
|
|
|
accessStore.setAccessRoutes(accessibleRoutes);
|
|
|
accessStore.setIsAccessChecked(true);
|
|
|
setLastCheckedUserId(userInfo.userId);
|
|
|
const redirectPath = (redirect ??
|
|
|
- (path === preferences.app.defaultHomePath
|
|
|
- ? userInfo.homePath ||
|
|
|
- preferences.app.defaultHomePath ||
|
|
|
- accessibleMenus[0]?.path
|
|
|
- : to.fullPath)) as string;
|
|
|
+ (isHomeEntryPath(to.path) ? resolvedHomePath : to.fullPath)) as string;
|
|
|
|
|
|
return {
|
|
|
...router.resolve(decodeURIComponent(redirectPath)),
|