HNLayout.js 12.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
import React, { useEffect, useRef, useState, Suspense } from 'react';

import { Anchor, Popover, Radio, Spin } from 'antd';
import Icon, { LoadingOutlined } from '@ant-design/icons';
import { store, helpers } from '@wisdom-utils/utils';
import classNames from 'classnames';
import Cookies from 'js-cookie';
import { connect } from 'react-redux';
import { useHistory } from '@wisdom-utils/runtime';
import KeepAlive from 'react-activation';
import AMapLoader from '@amap/amap-jsapi-loader';
12
import RightContent from '@/components/GlobalHeader/HNRightContent';
13 14
import defaultSetting from '@wisdom-utils/components/lib/AppLayout/layouts/defaultSettings';
import { actionCreators } from '@/containers/App/store';
15
import BasicLayout from './AppLayout/layouts/BasicLayout';
16 17 18 19 20
import { WEB_GIS_TYPE } from '../constants';
import SecurityLayout from './SecurityLayout';
import Site from './Site';
import layoutStyles from './BasicLayout.less';
import styls from './HN.less';
曾婧's avatar
曾婧 committed
21

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
const ArcgisMap = React.lazy(() => import('../pages/map/arcgis'));
const AMap = React.lazy(() => import('../pages/map/amap'));
const CesiumMap = React.lazy(() => import('../pages/map/cesiumMap'));
const { params } = helpers;
const { getParamsV1 } = params;
const antIcon = <LoadingOutlined style={{ fontSize: 12 }} spin />;
const arrowSvg = ({ fillColor = '#fff' }) => (
  <svg
    t="1543324489942"
    className="icon"
    viewBox="0 0 1024 1024"
    version="1.1"
    xmlns="http://www.w3.org/2000/svg"
    xmlns="http://www.w3.org/1999/xlink"
    width="16"
    height="16"
  >
    <path
      d="M511.700683 639.423111 191.917496 319.596945 319.830771 319.596945 511.700683 511.715521 703.570595 319.596945 831.48387 319.596945Z"
      p-id="8626"
      fill={fillColor}
    />
  </svg>
);
const ArrowIcon = props => <Icon component={arrowSvg} {...props} />;
const optionsWith = [{ label: '按站点', value: 'site' }, { label: '按城市', value: 'city' }];
const HOT = ['HOT', '县', '市', 'New'];
const StationsItem = (item, action, onChangeVisible) => {
  const changeGroup = (event, data) => {
    action.changeGroup && action.changeGroup(event, data, onChangeVisible);
  };
  return (
    <li key={item.groupID} style={{ marginRight: `${item.style.marginRight}px` }}>
      <a
        className={classNames(layoutStyles.city_select, layoutStyles.showTip)}
        title={item.city}
        index={item.promoteIndex}
        style={{ fontSize: '13px' }}
        onClick={event => changeGroup(event, item)}
      >
        {item.groupName}
        {item.promoteTip && HOT.includes(item.promoteTip) && <div title={`${item.promoteTip}`}>{item.promoteTip}</div>}
      </a>
    </li>
  );
};

const Stations = props => {
  const data = props.data.stations;
  const [targetOffset, setTargetOffset] = useState(undefined);
  const cityPane = useRef(null);
  const cityContent = useRef(null);
  const [defaultTab, setDefaultTab] = useState('site');
  useEffect(() => {
    setTargetOffset(cityPane.current.clientHeight / 2);
  }, []);

  const handleClick = (event, link) => {
    event.preventDefault();
  };

  const handleTabChange = event => {
    setDefaultTab(event.target.value);
  };
  return (
    <>
      <div className={layoutStyles.focusStations}>
        <ul>{Array.isArray(data) ? data.map(item => StationsItem(item, props.action, props.setVisible)) : null}</ul>
      </div>
      <Radio.Group
        options={optionsWith}
        optionType="button"
        buttonStyle="solid"
        size="small"
        value={defaultTab}
        onChange={handleTabChange}
        style={{ marginTop: '6px' }}
      />
      <div style={{ maxWidth: '520px', position: 'relative' }}>
        {defaultTab === 'site' ? (
          <div className={classNames(layoutStyles.city_pane, layoutStyles.station_container)} ref={cityPane}>
            <Anchor
              affix={false}
              onClick={handleClick}
              targetOffset={targetOffset}
              getContainer={() => cityContent.current}
            >
              <ul className={layoutStyles.py}>
                {props.data && props.data.siteCityList && props.data.siteCityList.letters}
              </ul>
              <div className={layoutStyles.cityContent} style={{ height: '335px' }} ref={cityContent}>
                {props.data && props.data.siteCityList && props.data.siteCityList.content}
              </div>
            </Anchor>
          </div>
        ) : (
          <div className="city_pane city_container">{props.data.citySelector}</div>
        )}
        <Spin spinning={props.loading} tip="加载中" />
      </div>
    </>
  );
};

const renderSite = ({ data, config, loading, setLoading, action }) => {
  const [visible, setVisible] = useState(false);
  let loaded = !!((data && !data.stations) || (Array.isArray(data.weathers) && data.weathers.length === 0));
  if (config && config.userInfo && config.userInfo.site === '') {
    loaded = false;
  }
  return (
    <>
      <Spin
        indicator={antIcon}
        spinning={loaded}
        size="small"
        style={{ marginLeft: '10px' }}
        tip=""
        wrapperClassName={layoutStyles.spinLoadding}
      >
        <>
          <Popover
            placement="bottomLeft"
            trigger="click"
            visible={visible}
            content={
              <Stations data={data} loading={loading} setLoading={setLoading} setVisible={setVisible} action={action} />
            }
            arrowPointAtCenter
            overlayClassName={classNames(layoutStyles.stationsWrapper, layoutStyles.stationsTop)}
            onVisibleChange={visible => setVisible(visible)}
          >
            {Array.isArray(data.stations) ? (
              <div className={layoutStyles.toggleSite}>
                <img src={require('../assets/basic/site.png')} className={layoutStyles.site} />
                <span className={layoutStyles.name}>{data.currentStationName}</span>
                <ArrowIcon
                  className={layoutStyles.arrow}
                  fillColor="#fff"
                  style={{
                    transform: !visible ? `rotate(0deg)` : `rotate(180deg)`,
                  }}
                />
              </div>
            ) : null}
          </Popover>
        </>
      </Spin>
      {data.weathers && Object.keys(data.weathers).length > 0 ? (
        <span
          className={layoutStyles.weatcher}
          style={{
            borderLeft: data.stations.length === 0 ? '0px' : '1px solid rgba(256, 256, 256, 0.3)',
          }}
        >
          <img src={data.weathers && data.weathers.icon} className={layoutStyles.icon} />
          <span className={layoutStyles.text}>{data.weathers && data.weathers.text}</span>
        </span>
      ) : null}
    </>
  );
};

const Layout = props => {
  const [collapse, setCollapse] = useState(false);
  const [cityData, setCityData] = useState({});
  const [siteLoading, setSiteLoading] = useState(false);
  const [mapMode, setMapMode] = useState(WEB_GIS_TYPE.ARCGIS);
  const [siteAction, setSiteAction] = useState(() => new Site(props, setSiteLoading));
  // const basename = getBaseName();
  const history = useHistory();
  useEffect(() => {
    siteAction.setGlobalConfig(props.global);
崔佳豪's avatar
崔佳豪 committed
195 196 197
    const tk = Cookies.get('token') || props.global.token;
    const isLogin = tk !== null && tk !== 'undefined' && tk !== (void 0);
    if (!isLogin) {
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
      let client = props.global && props.global.hasOwnProperty('client') ? props.global.get('client') : null;
      client = client !== 'undefined' && !_.isNull(client) && !_.isUndefined(client) ? client : 'city';
      let generateType = props.global && props.global.hasOwnProperty('get') ? props.global.get('generateType') : null;
      generateType =
        !_.isNull(generateType) && !_.isUndefined(generateType) && generateType !== 'undefined'
          ? `&generateType=${generateType}`
          : '';
      history.replace(`/user/login?client=${client}${generateType}`);
      props.logout();
      return;
    }
    if (
      props.global &&
      props.global.userInfo &&
      props.global.userInfo.token !== null &&
      props.global.userInfo.loginName &&
      Object.keys(cityData).length === 0
    ) {
      siteAction.getCityStationsForUser().then(res => {
        setCityData(res);
      });
    }
220
  }, [cityData, history, props, props.global, siteAction]);
221 222 223 224 225 226

  const handlerPageChange = () => {
    const params = getParamsV1(props.location.pathname);
    if (params.hasOwnProperty('mapType') && params.mapType === WEB_GIS_TYPE.AMAP) {
      AMapLoader.reset();
      setMapMode(WEB_GIS_TYPE.AMAP);
227 228 229
    } else if (!/civweb4/.test(location.pathname)) {
      AMapLoader.reset();
      setMapMode(WEB_GIS_TYPE.ARCGIS);
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
    }
  };

  useEffect(() => {
    window.share.event.on('updateSite', res => setCityData(res));
    return () => {
      window.share.event.removeAllListeners('updateSite');
    };
  }, []);

  const handleUpdateCurrentIndex = index => {
    props.updateCurrentIndex(index);
    window.share && window.share.event.emit('trigger:updateMenuIndex', index);
    store.set('updateMenuIndex', index);
  };

  const handlerIndustry = event => {
    props.global.get('userInfo.site') ? history.push(`/industry`) : void 0;
  };

  const handlerCollapsed = () => {
    setCollapse(!collapse);
  };

  const logo =
    props.global && _.isFunction(props.global.get) && props.global.get('bannerLogo')
      ? props.global &&
        _.isFunction(props.global.transformDevAssetsBaseURL) &&
        props.global.transformDevAssetsBaseURL(props.global.get('bannerLogo'))
      : defaultSetting.logo;
  return (
    <SecurityLayout loading {...props}>
      <div className={styls.hnlayout}>
        <BasicLayout
          route={props.route}
265
          title={props.global.title}
曾婧's avatar
曾婧 committed
266 267
          siderWidth={140}
          logo={logo}
268
          // logo={require('@/assets/basic/HN-logo.png')}
269 270 271 272 273
          config={props.global}
          currentMenuIndex={props.currentMenuIndex}
          onHeaderLogo={handlerIndustry}
          onPageChange={handlerPageChange}
          onMenuPanelItemClick={handleUpdateCurrentIndex}
曾婧's avatar
曾婧 committed
274
          // subMenuItemRender={()=> <RightContent />}
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
          headerContentRender={() => <RightContent />}
          headerSiteRender={() =>
            renderSite({
              data: cityData,
              config: props.global,
              loading: siteLoading,
              setLoading: setSiteLoading,
              action: siteAction,
            })
          }
        >
          <>
            <Suspense fallback={<></>}>
              {window.location.pathname.startsWith('/civbase/civ_3d') ? (
                <KeepAlive>
                  <CesiumMap />
                </KeepAlive>
              ) : window.location.pathname.startsWith('/civbase/civ_webgis') ? (
                mapMode === 'AMap' ? (
                  <AMap />
                ) : (
                  <ArcgisMap options={{ type: 'ArcgisMap' }} />
                )
              ) : null}
            </Suspense>
            <div id="micro-container" className="subapp-container">
              {props.children}
            </div>
          </>
        </BasicLayout>
      </div>
    </SecurityLayout>
  );
};

const mapStateToProps = state => ({
  global: state.getIn(['global', 'globalConfig']),
  settings: state.getIn(['global', 'defaultSetting']),
  collapsed: state.getIn(['global', 'collapsed']),
  menu: state.getIn(['global', 'menu']),
  currentMenuIndex: state.getIn(['global', 'currentMenuIndex']),
  flatMenu: state.getIn(['global', 'flatMenu']),
  authValidate: state.getIn(['global', 'authValidate']),
  pathname: state.getIn(['global', 'pathname']),
  selectedKeys: state.getIn(['global', 'selectedKeys']),
  openKeys: state.getIn(['global', 'openKeys']),
  complexConfig: state.getIn(['global', 'complexConfig']),
  complexPathName: state.getIn(['global', 'complexPathName']),
  microMounted: state.getIn(['global', 'microMounted']),
});
const mapDispatchToProps = dispatch => ({
  updageSetting(setting) {
    dispatch(actionCreators.updageSetting(setting));
  },
  updateConfig(config) {
    dispatch(actionCreators.getConfig(config));
  },
  updateCurrentIndex(index) {
    dispatch(actionCreators.updateCurrentIndex(index));
  },
  updateCollapsed(collapsed) {
    dispatch(actionCreators.updateCollapsed(collapsed));
  },
  updateAuthValidate(auth) {
    dispatch(actionCreators.updateAuthValidate(auth));
  },
  updatePathname(pathname) {
    dispatch(actionCreators.updatePathname(pathname));
  },
  updateSelectedKeys(keys) {
    dispatch(actionCreators.updateSelectedKeys(keys));
  },
  updateOpenKeys(keys) {
    dispatch(actionCreators.updateOpenKeys(keys));
  },
  updateComplexConfig(config) {
    dispatch(actionCreators.updateComplexConfig(config));
  },
  updateComplexPathName(pathname) {
    dispatch(actionCreators.updateComplexPathName(pathname));
  },
  logout() {
    dispatch(actionCreators.logout());
  },
});
export default connect(
  mapStateToProps,
  mapDispatchToProps,
)(Layout);