index.js 3.15 KB
Newer Older
1 2 3 4 5
/*
 * @Title:
 * @Author: hongmye
 * @Date: 2023-01-10 11:18:55
 */
6
import React, { useEffect, useMemo, useState, useRef } from 'react';
崔佳豪's avatar
崔佳豪 committed
7 8
import { Empty, Spin, notification } from 'antd';
import SecurityLayout from '@/layouts/SecurityLayout';
9
import { appService } from '@/api';
10 11 12 13 14 15
import Cookies from 'js-cookie';
import _ from 'lodash';
import { useHistory, useAliveController } from '@wisdom-utils/runtime';
import { connect } from 'react-redux';
import { actionCreators } from '@/containers/App/store';

16 17
import { SERVICE_INTERFACE_SUCCESS_CODE } from '@/constants'; // 系统配置项名称
import { BootPageTemplate } from './template/constants';
18

19
const BootPage = props => {
崔佳豪's avatar
崔佳豪 committed
20 21 22 23 24
  const [info, setInfo] = useState({
    first: true,
    loading: true,
    error: false,
  });
25 26
  const { clear } = useAliveController();
  const history = useHistory();
周宏民's avatar
周宏民 committed
27
  const [template, setTemplate] = useState(window?.globalConfig?.displayMode || 'default');
28
  const RenderComponent = useMemo(() => {
29 30
    if (BootPageTemplate[template]) {
      return BootPageTemplate[template];
31
    }
32 33
    return BootPageTemplate.default;
  }, [template]);
34

35 36 37
  useEffect(() => {
    const tk = Cookies.get('token') || props.global.token;
    const isLogin = tk !== null && tk !== 'undefined' && tk !== void 0;
38
    let client = sessionStorage.getItem('client') || props?.global?.client || null;
39 40 41 42 43 44 45
    client = client !== 'undefined' && !_.isNull(client) && !_.isUndefined(client) ? client : 'city';
    const generateType = props.global && props.global.hasOwnProperty('get') ? props.global.get('generateType') : null;
    if (!isLogin) {
      history.push(`/user/login?client=${client}${generateType || ''}`, { reload: true });
      clear();
      props.logout();
    }
46
  }, [props]);
47
  useEffect(() => {
邓晓峰's avatar
邓晓峰 committed
48
    appService
49
      .GetIntegratedloginSetting({
50
        ignoreSite: true,
邓晓峰's avatar
邓晓峰 committed
51 52
      })
      .then(res => {
崔佳豪's avatar
崔佳豪 committed
53
        const { code, data } = res;
杨思琦's avatar
杨思琦 committed
54
        if (code !== SERVICE_INTERFACE_SUCCESS_CODE) {
崔佳豪's avatar
崔佳豪 committed
55 56 57 58
          notification.error({ message: '提示', duration: 3, description: '系统引导页配置错误' });
          setInfo({ first: false, loading: false, error: true });
          return;
        }
59 60 61 62
        let displayMode = data?.displayMode || 'default';
        if (displayMode === '卡片' || displayMode === '地图') {
          displayMode = 'default';
        }
63
        props.updateIntegratedConfig(data || '');
64
        setTemplate(displayMode);
崔佳豪's avatar
崔佳豪 committed
65 66 67 68
        setInfo({ first: false, loading: false, error: false });
      })
      .catch(err => {
        setInfo({ first: false, loading: false, error: true });
69
        props.updateIntegratedConfig('');
邓晓峰's avatar
邓晓峰 committed
70
      });
崔佳豪's avatar
崔佳豪 committed
71
  }, []);
72

邓晓峰's avatar
邓晓峰 committed
73
  return (
74
    <SecurityLayout>
75
      {info.loading ? <Spin /> : info.error ? <Empty /> : <RenderComponent {...{ ...props }} />}
76
    </SecurityLayout>
邓晓峰's avatar
邓晓峰 committed
77 78
  );
};
79 80 81 82 83 84 85 86
const mapStateToProps = state => ({
  global: state.getIn(['global', 'globalConfig']),
  instance: state.getIn(['global', 'instance']),
});
const mapDispatchToProps = dispatch => ({
  logout() {
    dispatch(actionCreators.logout());
  },
87 88 89
  updateIntegratedConfig(data) {
    dispatch(actionCreators.updateIntegratedConfig(data));
  },
90 91 92 93 94
});
export default connect(
  mapStateToProps,
  mapDispatchToProps,
)(BootPage);