import React from 'react' ; import { connect } from 'dva' ; import { PageLoading } from '@ant-design/pro-layout' ; import { Redirect } from 'umi' ; import { stringify } from 'querystring' ; import type { ConnectState , ConnectProps } from '@/models/connect' ; import type { CurrentUser } from '@/models/user' ; type SecurityLayoutProps = { loading ? : boolean ; currentUser ? : CurrentUser ; } & ConnectProps ; type SecurityLayoutState = { isReady : boolean ; } ; class SecurityLayout extends React . Component < SecurityLayoutProps , SecurityLayoutState > { state : SecurityLayoutState = { isReady : false , } ; componentDidMount ( ) { this . setState ( { isReady : true , } ) ; const { dispatch } = this . props ; if ( dispatch ) { dispatch ( { type : 'user/fetchCurrent' , } ) ; } } render ( ) { const { isReady } = this . state ; const { children , loading , currentUser } = this . props ; const isLogin = currentUser && currentUser . userid ; const queryString = stringify ( { redirect : window . location . href , } ) ; if ( ( ! isLogin && loading ) || ! isReady ) { return < PageLoading />; } if ( ! isLogin ) { return < Redirect to = { `/user/login?${ queryString }` } />; } return children ; } } export default connect ( ( { user , loading } ) => ( { currentUser : user . currentUser , loading : loading . models . user , } ) ) ( SecurityLayout ) ;