|
@@ -1,47 +1,38 @@
|
1
|
|
-import React from 'react';
|
|
1
|
+import React, { useState, useEffect } from 'react';
|
2
|
2
|
import { connect } from 'dva';
|
3
|
3
|
import { PageLoading } from '@ant-design/pro-layout';
|
4
|
4
|
import { Redirect } from 'umi';
|
5
|
5
|
import { stringify } from 'querystring';
|
6
|
6
|
|
7
|
|
-class SecurityLayout extends React.Component {
|
8
|
|
- state = {
|
9
|
|
- isReady: false,
|
10
|
|
- };
|
|
7
|
+const SecurityLayout = (props) => {
|
|
8
|
+ const [isReady, setIsReady] = useState(false)
|
11
|
9
|
|
12
|
|
- componentDidMount() {
|
13
|
|
- this.setState({
|
14
|
|
- isReady: true,
|
|
10
|
+ useEffect(() => {
|
|
11
|
+ setIsReady(true)
|
|
12
|
+ props.dispatch({
|
|
13
|
+ type: 'user/fetchCurrent',
|
15
|
14
|
});
|
16
|
|
- const { dispatch } = this.props;
|
|
15
|
+ }, [])
|
17
|
16
|
|
18
|
|
- if (dispatch) {
|
19
|
|
- dispatch({
|
20
|
|
- type: 'user/fetchCurrent',
|
21
|
|
- });
|
22
|
|
- }
|
23
|
|
- }
|
|
17
|
+ const { children, loading, currentUser, history } = props;
|
24
|
18
|
|
25
|
|
- render() {
|
26
|
|
- const { isReady } = this.state;
|
27
|
|
- const { children, loading, currentUser } = this.props; // You can replace it to your authentication rule (such as check token exists)
|
28
|
|
- // 你可以把它替换成你自己的登录认证规则(比如判断 token 是否存在)
|
|
19
|
+ console.log('--------->', history)
|
29
|
20
|
|
30
|
|
- const isLogin = currentUser && currentUser.userId;
|
31
|
|
- const queryString = stringify({
|
32
|
|
- redirect: window.location.href,
|
33
|
|
- });
|
|
21
|
+ const isLogin = currentUser && currentUser.userId;
|
34
|
22
|
|
35
|
|
- if ((!isLogin && loading) || !isReady) {
|
36
|
|
- return <PageLoading />;
|
37
|
|
- }
|
|
23
|
+ const queryString = stringify({
|
|
24
|
+ redirect: window.location.href,
|
|
25
|
+ });
|
38
|
26
|
|
39
|
|
- if (!isLogin && window.location.pathname !== '/user/login') {
|
40
|
|
- return <Redirect to={`/user/login?${queryString}`} />;
|
41
|
|
- }
|
|
27
|
+ if (loading || !isReady) {
|
|
28
|
+ return <PageLoading />;
|
|
29
|
+ }
|
42
|
30
|
|
43
|
|
- return children;
|
|
31
|
+ if (!isLogin) {
|
|
32
|
+ return <Redirect to={`/user/login?${queryString}`} />;
|
44
|
33
|
}
|
|
34
|
+
|
|
35
|
+ return children;
|
45
|
36
|
}
|
46
|
37
|
|
47
|
38
|
export default connect(({ user, loading }) => ({
|