5
5
// ==================
6
6
import React , { useState , useCallback } from "react" ;
7
7
import { useSelector , useDispatch } from "react-redux" ;
8
- import { Route , Redirect } from "react-router-dom" ;
8
+ import { Route , useNavigate , Outlet } from "react-router-dom" ;
9
9
import CacheRoute , { CacheSwitch } from "react-router-cache-route" ;
10
10
import loadable from "@loadable/component" ;
11
11
import { Layout , message } from "antd" ;
@@ -22,107 +22,39 @@ import "./BasicLayout.less";
22
22
import Header from "@/components/Header" ;
23
23
import MenuCom from "@/components/Menu" ;
24
24
import Footer from "@/components/Footer" ;
25
- import Loading from "@/components/Loading" ;
26
- import ErrorBoundary from "@/components/ErrorBoundary" ;
27
25
28
26
import Bread from "@/components/Bread" ;
29
27
//import BreadTab from "@/components/BreadTab"; // Tab方式的导航
30
28
31
29
const { Content } = Layout ;
32
30
33
- // ==================
34
- // 异步加载各路由模块
35
- // ==================
36
- const [ NotFound , NoPower , Home , MenuAdmin , PowerAdmin , RoleAdmin , UserAdmin ] = [
37
- ( ) => import ( "../pages/ErrorPages/404" ) ,
38
- ( ) => import ( "../pages/ErrorPages/401" ) ,
39
- ( ) => import ( "../pages/Home" ) ,
40
- ( ) => import ( "../pages/System/MenuAdmin" ) ,
41
- ( ) => import ( "../pages/System/PowerAdmin" ) ,
42
- ( ) => import ( "../pages/System/RoleAdmin" ) ,
43
- ( ) => import ( "../pages/System/UserAdmin" ) ,
44
- ] . map ( ( item ) => {
45
- return loadable ( item as any , {
46
- fallback : < Loading /> ,
47
- } ) ;
48
- } ) ;
49
-
50
31
// ==================
51
32
// 类型声明
52
33
// ==================
53
34
import type { RootState , Dispatch } from "@/store" ;
54
35
import type { Menu } from "@/models/index.type" ;
55
36
import type { History } from "history" ;
56
- import type { RouteComponentProps } from "react-router-dom" ;
57
-
58
- type Props = {
59
- history : History ;
60
- location : Location ;
61
- } ;
62
37
63
38
// ==================
64
39
// 本组件
65
40
// ==================
66
- function BasicLayoutCom ( props : Props ) : JSX . Element {
41
+ function BasicLayoutCom ( ) : JSX . Element {
67
42
const dispatch = useDispatch < Dispatch > ( ) ;
43
+ const navigate = useNavigate ( ) ;
68
44
const userinfo = useSelector ( ( state : RootState ) => state . app . userinfo ) ;
69
45
const [ collapsed , setCollapsed ] = useState ( false ) ; // 菜单栏是否收起
70
46
71
47
// 退出登录
72
- const onLogout = useCallback ( ( ) => {
48
+ const onLogout = ( ) => {
73
49
dispatch . app . onLogout ( ) . then ( ( ) => {
74
50
message . success ( "退出成功" ) ;
75
- props . history . push ( "/user/login" ) ;
51
+ navigate ( "/user/login" ) ;
76
52
} ) ;
77
- } , [ props , dispatch . app ] ) ;
78
-
79
- /**
80
- * 工具 - 判断当前用户是否有该路由权限,如果没有就跳转至401页
81
- * @param pathname 路由路径
82
- * **/
83
- const checkRouterPower = useCallback (
84
- ( pathname : string ) => {
85
- let menus : Menu [ ] = [ ] ;
86
- if ( userinfo . menus && userinfo . menus . length ) {
87
- menus = userinfo . menus ;
88
- } else if ( sessionStorage . getItem ( "userinfo" ) ) {
89
- menus = JSON . parse (
90
- tools . uncompile ( sessionStorage . getItem ( "userinfo" ) || "[]" )
91
- ) . menus ;
92
- }
93
- const m : string [ ] = menus . map ( ( item ) => item . url ) ; // 当前用户拥有的所有菜单
94
-
95
- if ( m . includes ( pathname ) ) {
96
- return true ;
97
- }
98
- return false ;
99
- } ,
100
- [ userinfo ]
101
- ) ;
102
-
103
- // 切换路由时触发
104
- const onEnter = useCallback (
105
- ( Component , props : RouteComponentProps ) => {
106
- /**
107
- * 检查当前用户是否有该路由页面的权限
108
- * 没有则跳转至401页
109
- * **/
110
- if ( checkRouterPower ( props . location . pathname ) ) {
111
- return < Component { ...props } /> ;
112
- }
113
- return < Redirect to = "/nopower" /> ;
114
- } ,
115
- [ checkRouterPower ]
116
- ) ;
53
+ } ;
117
54
118
55
return (
119
56
< Layout className = "page-basic" hasSider >
120
- < MenuCom
121
- data = { userinfo . menus }
122
- collapsed = { collapsed }
123
- location = { props . location }
124
- history = { props . history }
125
- />
57
+ < MenuCom data = { userinfo . menus } collapsed = { collapsed } />
126
58
127
59
< Layout >
128
60
< Header
@@ -132,48 +64,15 @@ function BasicLayoutCom(props: Props): JSX.Element {
132
64
onLogout = { onLogout }
133
65
/>
134
66
{ /* 普通面包屑导航 */ }
135
- < Bread menus = { userinfo . menus } location = { props . location } />
67
+ < Bread menus = { userinfo . menus } />
136
68
{ /* Tab方式的导航 */ }
137
69
{ /* <BreadTab
138
70
menus={userinfo.menus}
139
71
location={props.location}
140
72
history={props.history}
141
73
/> */ }
142
74
< Content className = "content" >
143
- < ErrorBoundary location = { props . location } >
144
- < CacheSwitch >
145
- < Redirect exact from = "/" to = "/home" />
146
- < Route
147
- exact
148
- path = "/home"
149
- render = { ( props ) => onEnter ( Home , props ) }
150
- />
151
-
152
- < Route
153
- exact
154
- path = "/system/menuadmin"
155
- render = { ( props ) => onEnter ( MenuAdmin , props ) }
156
- />
157
- < Route
158
- exact
159
- path = "/system/poweradmin"
160
- render = { ( props ) => onEnter ( PowerAdmin , props ) }
161
- />
162
- < Route
163
- exact
164
- path = "/system/roleadmin"
165
- render = { ( props ) => onEnter ( RoleAdmin , props ) }
166
- />
167
- { /*<!-- 使用CacheRoute可以缓存该页面,类似Keep-alive -->*/ }
168
- < CacheRoute
169
- exact
170
- path = "/system/useradmin"
171
- render = { ( props ) => onEnter ( UserAdmin , props ) }
172
- />
173
- < Route exact path = "/nopower" component = { NoPower } />
174
- < Route component = { NotFound } />
175
- </ CacheSwitch >
176
- </ ErrorBoundary >
75
+ < Outlet />
177
76
</ Content >
178
77
< Footer />
179
78
</ Layout >
0 commit comments