SecurityLayout.js 3.49 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';
杨思琦's avatar
杨思琦 committed
9 10 11
import { ValidDefaultPWDContainer } from '@/components/Container';
import { initSaveMicroApps } from '../micro';

邓晓峰's avatar
邓晓峰 committed
12 13 14 15 16 17 18 19
class SecurityLayout extends React.Component {
  state = {
    isReady: false,
  };

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

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

    const nextPathname = nextProps.location.pathname;
    const nextArgv = nextPathname.split('/');
37
    if (argv[2] !== nextArgv[2]) {
邓晓峰's avatar
邓晓峰 committed
38
      Object.keys(products).forEach(item => {
杨思琦's avatar
杨思琦 committed
39 40 41 42
        window[products[item]] &&
          window[products[item]] &&
          window[products[item]].unmount &&
          window[products[item]].unmount({ store });
邓晓峰's avatar
邓晓峰 committed
43 44
      });
    }
45
    if (this.props.location.pathname !== nextProps.location.pathname) {
杨思琦's avatar
杨思琦 committed
46 47 48 49 50 51 52 53 54 55 56 57 58
      if (window?.qiankunIsCache) {
        const marks = document.querySelectorAll('#micro-container');
        if (marks && marks.length) {
          const activeAppPath = nextProps.location.pathname.match(/(\w+)/)
            ? nextProps.location.pathname.match(/(\w+)/)[0]
            : null;
          if (activeAppPath) {
            initSaveMicroApps(nextProps.location.pathname, nextProps.global);
          }
        }
      } else {
        store.set('event:globalConfig', { globalConfig: nextProps.global });
      }
邓晓峰's avatar
邓晓峰 committed
59 60 61
    }
  }

邓晓峰's avatar
邓晓峰 committed
62
  render() {
邓晓峰's avatar
邓晓峰 committed
63
    const { isReady } = this.state;
邓晓峰's avatar
邓晓峰 committed
64
    const { children, global, loading } = this.props;
65
    const tk = Cookies.get('token') || global.token;
杨思琦's avatar
杨思琦 committed
66 67 68 69 70 71 72
    const isLogin = tk !== null && tk !== 'undefined' && tk !== void 0;
    const { pathname } = history.location;
    if (
      (!isLogin && window.location.pathname !== '/civbase/user/login') ||
      // eslint-disable-next-line no-prototype-builtins
      (global.hasOwnProperty('size') && global.size === 0)
    ) {
邓晓峰's avatar
邓晓峰 committed
73
      this.props.updateCurrentIndex && this.props.updateCurrentIndex(0);
74
      let client = global.client || sessionStorage.getItem('client');
邓晓峰's avatar
邓晓峰 committed
75
      client = client !== 'undefined' && !_.isNull(client) && !_.isUndefined(client) ? client : 'city';
邓晓峰's avatar
邓晓峰 committed
76
      let { generateType } = global;
杨思琦's avatar
杨思琦 committed
77 78 79 80 81 82
      generateType =
        !_.isNull(generateType) && !_.isUndefined(generateType) && generateType !== 'undefined'
          ? `&generateType=${generateType}`
          : '';
      // eslint-disable-next-line prettier/prettier
      window.qiankunStarted = false;
83
      return (
杨思琦's avatar
杨思琦 committed
84
        <Redirect to={{ pathname: `/user/login?client=${client}${generateType}`, state: { reload: true } }} exact />
85
      );
杨思琦's avatar
杨思琦 committed
86
    }
邓晓峰's avatar
邓晓峰 committed
87 88 89
    if ((!isLogin && loading) || !isReady) {
      return <PageLoading />;
    }
杨思琦's avatar
杨思琦 committed
90
    return <ValidDefaultPWDContainer>{children}</ValidDefaultPWDContainer>;
邓晓峰's avatar
邓晓峰 committed
91 92 93
  }
}

邓晓峰's avatar
邓晓峰 committed
94 95
const mapStateToProps = state => ({
  global: state.getIn(['global', 'globalConfig']),
杨思琦's avatar
杨思琦 committed
96
  location: state.getIn(['router', 'location']).toJS(),
邓晓峰's avatar
邓晓峰 committed
97
});
邓晓峰's avatar
邓晓峰 committed
98 99 100 101
export default connect(
  mapStateToProps,
  null,
)(SecurityLayout);