SecurityLayout.js 2.99 KB
Newer Older
邓晓峰's avatar
邓晓峰 committed
1
import React from 'react';
2
import Cookies from 'js-cookie';
邓晓峰's avatar
邓晓峰 committed
3
import { connect } from 'react-redux';
4
import { Redirect, history } from '@wisdom-utils/runtime';
邓晓峰's avatar
邓晓峰 committed
5
import PageLoading from '@wisdom-utils/components/lib/AppLayout/components/PageLoading';
邓晓峰's avatar
邓晓峰 committed
6
import _ from 'lodash';
7
import { stringify } from 'querystring';
邓晓峰's avatar
邓晓峰 committed
8
import { store, helpers } from '@wisdom-utils/utils';
9
import { ValidDefaultPWDContainer } from '@/components/Container'
邓晓峰's avatar
邓晓峰 committed
10 11 12 13 14 15 16 17
class SecurityLayout extends React.Component {
  state = {
    isReady: false,
  };

  componentDidMount() {
    this.setState({
      isReady: true,
邓晓峰's avatar
邓晓峰 committed
18
    });
19
    document.title = this.props.global.title;
邓晓峰's avatar
邓晓峰 committed
20 21
  }

邓晓峰's avatar
邓晓峰 committed
22 23
  componentWillReceiveProps(nextProps) {
    const products = (this.props.global.products || []).map(item => {
24
      if (item.PackageName === 'civweb4') {
邓晓峰's avatar
邓晓峰 committed
25 26 27 28 29 30 31 32 33 34
        return 'web4_console'
      }
      return `${item.PackageName}-main`;
    });
    const location = this.props.location;
    const pathname = location.pathname;
    const argv = pathname.split('/');

    const nextPathname = nextProps.location.pathname;
    const nextArgv = nextPathname.split('/');
35
    if (argv[2] !== nextArgv[2]) {
邓晓峰's avatar
邓晓峰 committed
36
      Object.keys(products).forEach(item => {
37
        window[products[item]] && window[products[item]] && window[products[item]].unmount && window[products[item]].unmount({ store: store });
邓晓峰's avatar
邓晓峰 committed
38 39
      });
    }
40 41
    if (this.props.location.pathname !== nextProps.location.pathname) {
      store.set('event:globalConfig', { globalConfig: nextProps.global });
邓晓峰's avatar
邓晓峰 committed
42
    }
43

邓晓峰's avatar
邓晓峰 committed
44 45
  }

邓晓峰's avatar
邓晓峰 committed
46
  render() {
47

邓晓峰's avatar
邓晓峰 committed
48
    const { isReady } = this.state;
邓晓峰's avatar
邓晓峰 committed
49
    const { children, global, loading } = this.props;
50 51
    const tk = Cookies.get('token') || global.token;
    const isLogin = tk !== null && tk !== 'undefined' && tk !== (void 0);
邓晓峰's avatar
邓晓峰 committed
52
    
53
    if (!isLogin && window.location.pathname !== '/civbase/user/login' || (global.hasOwnProperty('size') && global.size === 0)) {
54 55
      const { query = {}, search, pathname } = history.location;
      const { redirect } = query;
56 57 58
      // const queryString = stringify({
      //   redirect: pathname + search,
      // });
邓晓峰's avatar
邓晓峰 committed
59
      this.props.updateCurrentIndex && this.props.updateCurrentIndex(0);
60
      let client = global.client || sessionStorage.getItem('client');
邓晓峰's avatar
邓晓峰 committed
61
      client = client !== 'undefined' && !_.isNull(client) && !_.isUndefined(client) ? client : 'city';
邓晓峰's avatar
邓晓峰 committed
62
      let { generateType } = global;
63
      generateType = !_.isNull(generateType) && !_.isUndefined(generateType) && generateType !== 'undefined' ? `&generateType=${generateType}` : '';
64
      return (
邓晓峰's avatar
邓晓峰 committed
65
        <Redirect
邓晓峰's avatar
邓晓峰 committed
66
          to={{pathname: `/user/login?client=${client}${generateType}`, state:{reload: true}}}
邓晓峰's avatar
邓晓峰 committed
67
        />
68
      );
69
    };
邓晓峰's avatar
邓晓峰 committed
70 71 72 73

    if ((!isLogin && loading) || !isReady) {
      return <PageLoading />;
    }
74 75 76 77 78
    return (
      <ValidDefaultPWDContainer>
        {children}
      </ValidDefaultPWDContainer>
    );
邓晓峰's avatar
邓晓峰 committed
79 80 81
  }
}

邓晓峰's avatar
邓晓峰 committed
82 83
const mapStateToProps = state => ({
  global: state.getIn(['global', 'globalConfig']),
邓晓峰's avatar
邓晓峰 committed
84
  location: state.getIn(['router', 'location']).toJS()
邓晓峰's avatar
邓晓峰 committed
85
});
邓晓峰's avatar
邓晓峰 committed
86 87 88 89
export default connect(
  mapStateToProps,
  null,
)(SecurityLayout);