SecurityLayout.js 4.58 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
import { ValidDefaultPWDContainer } from '@/components/Container';
10
import { logout } from '@/containers/App/store/actions';
杨思琦's avatar
杨思琦 committed
11 12
import { initSaveMicroApps } from '../micro';

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

  componentDidMount() {
    this.setState({
      isReady: true,
邓晓峰's avatar
邓晓峰 committed
21
    });
22
    document.title = this.props.global.title;
23 24
    if (window.share && window.share.event) {
      window.share.event.on('event:logoutPackage', this.handleLogoutPackage);
25
      window.share.event.on("iframe:postMessage", this.handleIframePostMessage)
26
      window.share.event.on("event:isHasFrame", this.handleIsHasFrame)
27 28 29 30 31 32
    }
  }

  componentWillUnmount() {
    if (window.share && window.share.event) {
      window.share.event.removeListener('event:logoutPackage', this.handleLogoutPackage);
33
      window.share.event.removeListener("iframe:postMessage", this.handleIframePostMessage)
34
      window.share.event.removeListener("event:isHasFrame", this.handleIsHasFrame)
35 36 37 38 39 40
    }
  }

  // 退出登录
  handleLogoutPackage() {
    logout && logout();
邓晓峰's avatar
邓晓峰 committed
41 42
  }

43 44 45 46 47 48 49 50 51 52 53 54 55
  //发送消息,因为子包无法通信
  handleIframePostMessage (data) {
    if(!(window.parent && data)) return
    window.parent.postMessage(data, "*")
  }

  //获取当前页面是否嵌套
  handleIsHasFrame() {
    window.share.event.emit("event:getHasFrame", {
      hasFrame : window != window.parent
    })
  }

邓晓峰's avatar
邓晓峰 committed
56 57
  componentWillReceiveProps(nextProps) {
    const products = (this.props.global.products || []).map(item => {
58
      if (item.PackageName === 'civweb4') {
杨思琦's avatar
杨思琦 committed
59
        return 'web4_console';
邓晓峰's avatar
邓晓峰 committed
60 61 62
      }
      return `${item.PackageName}-main`;
    });
杨思琦's avatar
杨思琦 committed
63 64
    const { location } = this.props;
    const { pathname } = location;
邓晓峰's avatar
邓晓峰 committed
65 66 67 68
    const argv = pathname.split('/');

    const nextPathname = nextProps.location.pathname;
    const nextArgv = nextPathname.split('/');
69
    if (argv[2] !== nextArgv[2]) {
邓晓峰's avatar
邓晓峰 committed
70
      Object.keys(products).forEach(item => {
杨思琦's avatar
杨思琦 committed
71 72 73 74
        window[products[item]] &&
          window[products[item]] &&
          window[products[item]].unmount &&
          window[products[item]].unmount({ store });
邓晓峰's avatar
邓晓峰 committed
75 76
      });
    }
77
    if (this.props.location.pathname !== nextProps.location.pathname) {
杨思琦's avatar
杨思琦 committed
78 79 80 81 82 83 84 85 86 87 88 89 90
      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
91 92 93
    }
  }

邓晓峰's avatar
邓晓峰 committed
94
  render() {
邓晓峰's avatar
邓晓峰 committed
95
    const { isReady } = this.state;
邓晓峰's avatar
邓晓峰 committed
96
    const { children, global, loading } = this.props;
97
    const tk = Cookies.get('token') || global.token;
杨思琦's avatar
杨思琦 committed
98
    const isLogin = tk !== null && tk !== 'undefined' && tk !== void 0;
99 100 101 102 103 104
    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
105
      this.props.updateCurrentIndex && this.props.updateCurrentIndex(0);
106
      let client = global.client || sessionStorage.getItem('client');
邓晓峰's avatar
邓晓峰 committed
107
      client = client !== 'undefined' && !_.isNull(client) && !_.isUndefined(client) ? client : 'city';
108
      client = client || 'city';
邓晓峰's avatar
邓晓峰 committed
109
      let { generateType } = global;
杨思琦's avatar
杨思琦 committed
110 111 112 113 114 115
      generateType =
        !_.isNull(generateType) && !_.isUndefined(generateType) && generateType !== 'undefined'
          ? `&generateType=${generateType}`
          : '';
      // eslint-disable-next-line prettier/prettier
      window.qiankunStarted = false;
116
      return (
杨思琦's avatar
杨思琦 committed
117
        <Redirect to={{ pathname: `/user/login?client=${client}${generateType}`, state: { reload: true } }} exact />
118
      );
杨思琦's avatar
杨思琦 committed
119
    }
邓晓峰's avatar
邓晓峰 committed
120 121 122
    if ((!isLogin && loading) || !isReady) {
      return <PageLoading />;
    }
杨思琦's avatar
杨思琦 committed
123
    return <ValidDefaultPWDContainer>{children}</ValidDefaultPWDContainer>;
邓晓峰's avatar
邓晓峰 committed
124 125 126
  }
}

邓晓峰's avatar
邓晓峰 committed
127 128
const mapStateToProps = state => ({
  global: state.getIn(['global', 'globalConfig']),
杨思琦's avatar
杨思琦 committed
129
  location: state.getIn(['router', 'location']).toJS(),
邓晓峰's avatar
邓晓峰 committed
130
});
邓晓峰's avatar
邓晓峰 committed
131 132 133 134
export default connect(
  mapStateToProps,
  null,
)(SecurityLayout);