Commit 5e5f9568 authored by 刘斌's avatar 刘斌

fix: 去掉默认首页,改为用户权限的第一个页面

parent b9f1ac5d
......@@ -12,7 +12,7 @@ export interface Dept {
// leader: string;
// phone: string;
// email: string;
status: string;
status: number;
// delFlag: string;
parentName?: string;
children?: Dept[];
......
import type { MenuRecordRaw } from '@/types';
import type { Router } from 'vue-router';
import { LOGIN_PATH } from '@/constants';
......@@ -8,6 +10,26 @@ import { startProgress, stopProgress } from '@/utils';
import { generateAccess } from './access';
/**
* 从菜单列表中获取第一个可见的叶子页面路径
*/
function getFirstVisibleMenuPath(menus: MenuRecordRaw[]): string | undefined {
for (const menu of menus) {
if (menu.show === false) {
continue;
}
if (menu.children && menu.children.length > 0) {
const childPath = getFirstVisibleMenuPath(menu.children);
if (childPath) {
return childPath;
}
} else {
return menu.path;
}
}
return undefined;
}
/**
* 通用守卫配置
* @param router
......@@ -51,10 +73,18 @@ function setupAccessGuard(router: Router) {
// 基本路由,这些路由不需要进入权限拦截
if (coreRouteNames.includes(to.name as string)) {
if (to.path === LOGIN_PATH && accessStore.accessToken) {
return decodeURIComponent(
(to.query?.redirect as string) ||
userStore.userInfo?.homePath ||
preferences.app.defaultHomePath,
const firstMenuPath = getFirstVisibleMenuPath(accessStore.accessMenus);
const redirect = to.query?.redirect
? decodeURIComponent(to.query.redirect as string)
: undefined;
// 当 redirect 为根路径 '/' 时,直接使用菜单路径或默认首页,避免根路由二次重定向
if (redirect && redirect !== '/') {
return redirect;
}
return (
userStore.userInfo?.homePath ||
firstMenuPath ||
preferences.app.defaultHomePath
);
}
return true;
......@@ -108,13 +138,22 @@ function setupAccessGuard(router: Router) {
accessStore.setAccessMenus(accessibleMenus);
accessStore.setAccessRoutes(accessibleRoutes);
accessStore.setIsAccessChecked(true);
const redirectPath = (from.query.redirect ??
const firstMenuPath = getFirstVisibleMenuPath(accessibleMenus);
const decodedRedirect = from.query.redirect
? decodeURIComponent(from.query.redirect as string)
: undefined;
// 当 redirect 为根路径 '/' 时,视为无效重定向,使用默认逻辑
const redirectPath = (
(decodedRedirect && decodedRedirect !== '/'
? decodedRedirect
: undefined) ??
(to.path === preferences.app.defaultHomePath
? userInfo.homePath || preferences.app.defaultHomePath
: to.fullPath)) as string;
? userInfo.homePath || firstMenuPath || preferences.app.defaultHomePath
: to.fullPath)
) as string;
return {
...router.resolve(decodeURIComponent(redirectPath)),
...router.resolve(redirectPath),
replace: true,
};
});
......
......@@ -10,7 +10,6 @@ import {
UnauthorizedException,
} from '@/api/helper';
import { LOGIN_PATH } from '@/constants';
import { preferences } from '@/core/preferences';
import { $t } from '@/locales';
import { defineStore } from 'pinia';
......@@ -58,9 +57,8 @@ export const useAuthStore = defineStore('auth', () => {
if (accessStore.loginExpired) {
accessStore.setLoginExpired(false);
} else {
onSuccess
? await onSuccess?.()
: await router.push(preferences.app.defaultHomePath);
// 跳转到根路径,由路由守卫统一重定向到用户有权限的第一个页面
onSuccess ? await onSuccess?.() : await router.push('/');
}
if (userInfo?.realName) {
......
......@@ -50,7 +50,7 @@ function getDefaultValues(): FormData {
orderNum: 1,
parentId: 0,
// phone: '',
status: '0',
status: 1,
};
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment