SecurityLayout.jsx 1.3 KB

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