mock平台

AuthenticatedComponent.js 1.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import React from 'react';
  2. import { connect } from 'react-redux';
  3. import PropTypes from 'prop-types';
  4. import { changeMenuItem } from '../reducer/modules/menu';
  5. export function requireAuthentication(Component) {
  6. return @connect(
  7. state => {
  8. return {
  9. isAuthenticated: state.user.isLogin
  10. };
  11. },
  12. {
  13. changeMenuItem
  14. }
  15. )
  16. class AuthenticatedComponent extends React.PureComponent {
  17. constructor(props) {
  18. super(props);
  19. }
  20. static propTypes = {
  21. isAuthenticated: PropTypes.bool,
  22. location: PropTypes.object,
  23. dispatch: PropTypes.func,
  24. history: PropTypes.object,
  25. changeMenuItem: PropTypes.func
  26. };
  27. componentWillMount() {
  28. this.checkAuth();
  29. }
  30. componentWillReceiveProps() {
  31. this.checkAuth();
  32. }
  33. checkAuth() {
  34. if (!this.props.isAuthenticated) {
  35. this.props.history.push('/');
  36. this.props.changeMenuItem('/');
  37. }
  38. }
  39. render() {
  40. return <div>{this.props.isAuthenticated ? <Component {...this.props} /> : null}</div>;
  41. }
  42. };
  43. }