feat: enhance menu and routing data handling
This commit is contained in:
12
src/app.tsx
12
src/app.tsx
@ -50,7 +50,8 @@ function TabLayout({
|
||||
const items = Object.entries(keepElements.current).map(
|
||||
([pathname]: any) => {
|
||||
const pathnameLowerCase = pathname.toLowerCase();
|
||||
const label = pathKeyMap.get(pathnameLowerCase)?.name || 'Unknown';
|
||||
const pathKeyMapItem = pathKeyMap.get(pathnameLowerCase);
|
||||
const label = pathKeyMapItem?.name || pathKeyMapItem?.routeName || 'Unknown';
|
||||
const active = pathnameLowerCase === activeKeyLowerCase;
|
||||
return ({ label, key: pathnameLowerCase, closeIcon: <CloseIcon style={{ fontSize: 10, color: active ? '#fff' : '#575B66' }} /> })
|
||||
},
|
||||
@ -61,7 +62,8 @@ function TabLayout({
|
||||
<Tabs
|
||||
hideAdd
|
||||
onChange={(key: string) => {
|
||||
navigate(key);
|
||||
const { pathname, search } = keepElements.current[key].location;
|
||||
navigate(`${pathname}${search}`);
|
||||
}}
|
||||
activeKey={activeKeyLowerCase}
|
||||
type="editable-card"
|
||||
@ -79,12 +81,14 @@ function TabLayout({
|
||||
return
|
||||
}
|
||||
dropByCacheKey(targetKey)
|
||||
if (targetKey === activeKey) {
|
||||
if (targetKey === activeKey?.toLowerCase()) {
|
||||
// 删除当前选中的tab时:
|
||||
// 1.如果当前tab是第一个时自动选中后一个
|
||||
// 2.不是第一个时自动选中前一个
|
||||
const i = pathList.indexOf(targetKey)
|
||||
navigate(pathList[i === 0 ? i + 1 : i - 1])
|
||||
const to = pathList[i === 0 ? i + 1 : i - 1]
|
||||
const { pathname, search } = keepElements.current[to].location;
|
||||
navigate(`${pathname}${search}`)
|
||||
}
|
||||
}}
|
||||
items={items}
|
||||
|
@ -4,7 +4,7 @@ import ProLayout, {
|
||||
Settings,
|
||||
} from '@ant-design/pro-layout';
|
||||
import React, { useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { Link, connect, Dispatch, history, Outlet, useLocation, useKeepOutlets } from '@umijs/max';
|
||||
import { Link, connect, Dispatch, history, useLocation, useKeepOutlets, useAppData } from '@umijs/max';
|
||||
import { Result, Button, message } from 'antd';
|
||||
import Authorized from '@/utils/Authorized';
|
||||
import RightContent from '@/components/GlobalHeader/RightContent';
|
||||
@ -105,6 +105,8 @@ const BasicLayout: React.FC<BasicLayoutProps> = (props) => {
|
||||
const mall3_token: any = sessionStorage.getItem('Authorization');//当前登录token
|
||||
const userData: any = getSessionUserData();//当前登录人信息
|
||||
const children = useKeepOutlets();
|
||||
const routeData = useAppData();
|
||||
|
||||
useEffect(() => {
|
||||
if (getSessionRoleData()?.roleId) {
|
||||
let params = {
|
||||
@ -116,7 +118,10 @@ const BasicLayout: React.FC<BasicLayoutProps> = (props) => {
|
||||
if (dispatch) {
|
||||
dispatch({
|
||||
type: 'user/saveMenuData',
|
||||
payload: res.data || [],
|
||||
payload: {
|
||||
menuData: res.data || [],
|
||||
routeData: routeData.routes,
|
||||
},
|
||||
});
|
||||
}
|
||||
setmenuShow(true)
|
||||
|
@ -16,10 +16,16 @@ export interface CurrentUser {
|
||||
unreadCount?: number;
|
||||
}
|
||||
|
||||
export interface PathKeyMap {
|
||||
name: string;
|
||||
icon?: string;
|
||||
routeName?: string;
|
||||
}
|
||||
|
||||
export interface UserModelState {
|
||||
currentUser?: CurrentUser;
|
||||
menuData?: any[];
|
||||
pathKeyMap?: Map<string, {name: string, icon?: string}>;
|
||||
pathKeyMap?: Map<string, PathKeyMap>;
|
||||
}
|
||||
|
||||
export interface UserModelType {
|
||||
@ -42,7 +48,7 @@ const UserModel: UserModelType = {
|
||||
state: {
|
||||
currentUser: {},
|
||||
menuData: [],
|
||||
pathKeyMap: new Map<string, {name: string, icon?: string}>(),
|
||||
pathKeyMap: new Map<string, PathKeyMap>(),
|
||||
},
|
||||
|
||||
effects: {
|
||||
@ -70,8 +76,9 @@ const UserModel: UserModelType = {
|
||||
};
|
||||
},
|
||||
saveMenuData(state, action) {
|
||||
const menuData = action.payload || [];
|
||||
const pathKeyMap = new Map<string, {name: string, icon?: string}>();
|
||||
const menuData = action.payload.menuData || [];
|
||||
const routeData = action.payload.routeData || [];
|
||||
const pathKeyMap = new Map<string, PathKeyMap>();
|
||||
// 将menuData转换为pathKeyMap 递归处理
|
||||
const convertToPathKeyMap = (menuData: any[]) => {
|
||||
menuData.forEach((item: any) => {
|
||||
@ -83,7 +90,24 @@ const UserModel: UserModelType = {
|
||||
});
|
||||
};
|
||||
convertToPathKeyMap(menuData);
|
||||
pathKeyMap.set('/dashboard', { name: '首页' });
|
||||
let i = 1;
|
||||
while (routeData[i]) {
|
||||
const item = routeData[i];
|
||||
if (item.name) {
|
||||
const pathname = item.path.toLowerCase();
|
||||
if (pathKeyMap.has(pathname)) {
|
||||
const target = pathKeyMap.get(pathname)!;
|
||||
const newItem = {
|
||||
...target,
|
||||
routeName: item.name,
|
||||
}
|
||||
pathKeyMap.set(pathname, newItem);
|
||||
} else {
|
||||
pathKeyMap.set(pathname, { name: item.name, icon: item.icon });
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return {
|
||||
...state,
|
||||
menuData: action.payload || [],
|
||||
|
Reference in New Issue
Block a user