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

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

parent b9f1ac5d
...@@ -12,7 +12,7 @@ export interface Dept { ...@@ -12,7 +12,7 @@ export interface Dept {
// leader: string; // leader: string;
// phone: string; // phone: string;
// email: string; // email: string;
status: string; status: number;
// delFlag: string; // delFlag: string;
parentName?: string; parentName?: string;
children?: Dept[]; children?: Dept[];
......
import type { MenuRecordRaw } from '@/types';
import type { Router } from 'vue-router'; import type { Router } from 'vue-router';
import { LOGIN_PATH } from '@/constants'; import { LOGIN_PATH } from '@/constants';
...@@ -8,6 +10,26 @@ import { startProgress, stopProgress } from '@/utils'; ...@@ -8,6 +10,26 @@ import { startProgress, stopProgress } from '@/utils';
import { generateAccess } from './access'; 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 * @param router
...@@ -51,10 +73,18 @@ function setupAccessGuard(router: Router) { ...@@ -51,10 +73,18 @@ function setupAccessGuard(router: Router) {
// 基本路由,这些路由不需要进入权限拦截 // 基本路由,这些路由不需要进入权限拦截
if (coreRouteNames.includes(to.name as string)) { if (coreRouteNames.includes(to.name as string)) {
if (to.path === LOGIN_PATH && accessStore.accessToken) { if (to.path === LOGIN_PATH && accessStore.accessToken) {
return decodeURIComponent( const firstMenuPath = getFirstVisibleMenuPath(accessStore.accessMenus);
(to.query?.redirect as string) || const redirect = to.query?.redirect
? decodeURIComponent(to.query.redirect as string)
: undefined;
// 当 redirect 为根路径 '/' 时,直接使用菜单路径或默认首页,避免根路由二次重定向
if (redirect && redirect !== '/') {
return redirect;
}
return (
userStore.userInfo?.homePath || userStore.userInfo?.homePath ||
preferences.app.defaultHomePath, firstMenuPath ||
preferences.app.defaultHomePath
); );
} }
return true; return true;
...@@ -108,13 +138,22 @@ function setupAccessGuard(router: Router) { ...@@ -108,13 +138,22 @@ 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 ?? 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 (to.path === preferences.app.defaultHomePath
? userInfo.homePath || preferences.app.defaultHomePath ? userInfo.homePath || firstMenuPath || preferences.app.defaultHomePath
: to.fullPath)) as string; : to.fullPath)
) as string;
return { return {
...router.resolve(decodeURIComponent(redirectPath)), ...router.resolve(redirectPath),
replace: true, replace: true,
}; };
}); });
......
...@@ -10,7 +10,6 @@ import { ...@@ -10,7 +10,6 @@ import {
UnauthorizedException, UnauthorizedException,
} from '@/api/helper'; } from '@/api/helper';
import { LOGIN_PATH } from '@/constants'; import { LOGIN_PATH } from '@/constants';
import { preferences } from '@/core/preferences';
import { $t } from '@/locales'; import { $t } from '@/locales';
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
...@@ -58,9 +57,8 @@ export const useAuthStore = defineStore('auth', () => { ...@@ -58,9 +57,8 @@ export const useAuthStore = defineStore('auth', () => {
if (accessStore.loginExpired) { if (accessStore.loginExpired) {
accessStore.setLoginExpired(false); accessStore.setLoginExpired(false);
} else { } else {
onSuccess // 跳转到根路径,由路由守卫统一重定向到用户有权限的第一个页面
? await onSuccess?.() onSuccess ? await onSuccess?.() : await router.push('/');
: await router.push(preferences.app.defaultHomePath);
} }
if (userInfo?.realName) { if (userInfo?.realName) {
......
...@@ -50,7 +50,7 @@ function getDefaultValues(): FormData { ...@@ -50,7 +50,7 @@ function getDefaultValues(): FormData {
orderNum: 1, orderNum: 1,
parentId: 0, parentId: 0,
// phone: '', // 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