index.js 10.2 KB
Newer Older
1 2 3 4 5 6 7 8
import '@wisdom-utils/utils/lib/helpers/format';
import React, { forwardRef, useEffect, useRef, useState } from 'react';
import { connect } from 'react-redux';
import { Modal } from 'antd';
import { Helmet, HelmetProvider } from 'react-helmet-async';
import classNames from 'classnames';
import { dom } from '@wisdom-utils/utils/lib/helpers';
import { useHistory, withRouter } from '@wisdom-utils/runtime';
9
import Cookies from 'js-cookie';
10
import { actionCreators } from '@/containers/App/store';
11
import { LOGIN_WAY, LOGIN_DISPLAY } from '@/constants';
12 13 14 15 16 17 18
import defaultSetting from '../../../../../../config/defaultSetting';
import LoginAction from '../../login';
import styles from './index.less';
import useRenderQcode from '../../js/useRenderQcode';
import Account from '../../js/useAccount';
import IotComponent from '../../js/useIOTComponent';
import CloudForm from './cloudForm';
杨思琦's avatar
杨思琦 committed
19
import { defaultApp } from '../../../../../micro';
20 21 22 23 24 25 26 27
import useTime from '../../js/useTime';

const isRQcodeFunc = loginFunc => {
  const rqcodeFuncs = [LOGIN_DISPLAY.WeChart, LOGIN_DISPLAY.WeCom];
  return !!(loginFunc && rqcodeFuncs.indexOf(loginFunc) !== -1);
};
const loginFuncImg = loginFunc => {
  if (isRQcodeFunc(loginFunc)) {
杨思琦's avatar
杨思琦 committed
28
    // eslint-disable-next-line global-require
29
    return require('@/assets/images/login/cloud/func_pwd.png');
30
  }
杨思琦's avatar
杨思琦 committed
31
  // eslint-disable-next-line global-require
32
  return require('@/assets/images/login/cloud/func_rqcode.png');
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
};

const Login = forwardRef((props, _ref) => {
  const videoRef = useRef();
  const loginRef = useRef();
  const timeRef = useRef();
  const titleRef = useRef();
  const sliVerify = useRef();
  const loginFormRef = useRef();
  const formRef = useRef(null);
  const footerRef = useRef();
  const currentDate = useTime(); // 计时时间

  const [status, setStatus] = useState('normal');
  const [autoLogin, setAutoLogin] = useState(false); // 是否记住密码
  const [submitting, setSubmitting] = useState(false); // 提交状态
  const [type, setType] = useState(LOGIN_DISPLAY.Account); // 登录方式,type是LOGIN_DISPLAY的value,也是LOGIN_WAY的key,根据type决定loginModel
  const [visible, setVisible] = useState(false);
  const history = useHistory();
  const [action, setAction] = useState(() => new LoginAction(Object.assign({}, props, { history }), setVisible, true));
杨思琦's avatar
杨思琦 committed
53 54 55 56
  const [showVideo, setShowVideo] = useState(false);
  let { ddCode } = props.global;
  const loginMode = Cookies.get('loginMode') || null;
  if (loginMode && loginMode === 'iotWechat') ddCode = null;
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  const handleSubmit = values => {
    /* eslint-disable */
    action &&
      (type === 'Account'
        ? action.loginHandler(values.userName, values.password, null, autoLogin, sliVerify)
        : type === 'Mobile'
        ? action.phoneLoginFormHandler(values.mobile, values.captcha)
        : null);

    setSubmitting(true);
    props.updateCurrentIndex && props.updateCurrentIndex(-1);
  };

  useEffect(() => {
    // if (props.loginMode === LOGIN_WAY.WeChart) {
    action &&
      action.events.on('loginSuccess', event => {
        setSubmitting(false);
        props.updateCurrentIndex && props.updateCurrentIndex(0);
        props.history.push(`/?client=${props.global.client}`);
        // window.share.event.emit('triggerMicro', props.global);
杨思琦's avatar
杨思琦 committed
78 79
        // initMicroApps();
        defaultApp();
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
      });
    action &&
      action.events.on('loginError', event => {
        setVisible(false);
        setSubmitting(false);
      });
    action &&
      action.events.on('loginVisible', status => {
        setVisible(status);
      });
    // }
    return () => {
      action && action.events && action.events.removeAllListeners('loginSuccess');
      action && action.events && action.events.removeAllListeners('loginError');
      action && action.events && action.events.removeAllListeners('loginVisible');
    };
  }, [props.loginMode]);

  let videoTimeout = null;

  useEffect(() => {
杨思琦's avatar
杨思琦 committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    if(ddCode && loginMode === 'qywx') {
      setShowVideo(false);
    } else {
      setShowVideo(true);
      setTimeout(() => {
        if (videoRef && videoRef.current) {
          videoRef.current.addEventListener('ended', function() {
            dom.removeClass(loginRef.current, styles.caseHide);
            dom.addClass(loginRef.current, styles.loginTimeShow);
            videoTimeout = setTimeout(() => {
              dom.removeClass(timeRef.current, styles.caseHide);
              dom.addClass(timeRef.current, 'animate__fadeIn');
    
              dom.removeClass(loginFormRef.current, styles.caseHide);
              dom.addClass(loginFormRef.current, 'animate__fadeInUp');
    
              dom.removeClass(footerRef.current, styles.caseHide);
              dom.addClass(footerRef.current, 'animate__slideInUp');
              dom.removeClass(titleRef.current, styles.caseHide);
              dom.addClass(titleRef.current, 'animte__fadeInUp');
            }, 500);
          });
        }
124 125 126 127
      });
    }
    return () => {
      videoTimeout && clearTimeout(videoTimeout);
杨思琦's avatar
杨思琦 committed
128
      videoRef.current && videoRef.current.removeEventListener('ended', () => {});
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
    };
  }, [videoRef]);
  useEffect(() => {
    setSubmitting(false);
  }, [visible]);
  const renderAddons = useRenderQcode({ ...props.global, styles });
  const renderPlatform = () => {
    const template = props.global.loginTemplate;
    const params = {
      fromRef: formRef,
      type,
      setType,
      status,
      submitting,
      autoLogin,
      setAutoLogin,
      action,
      onSubmit: handleSubmit,
      loginMode: props.loginMode,
      updateLoginMode: props.updateLoginMode,
    };
    switch (template) {
      case 'DarkCloud.html':
      case 'Dark.html':
        return <CloudForm {...params} />;
      case 'Dark - IOTMultiLogin.html':
        return <IotComponent {...params} />;
      default:
        return <Account {...params} />;
    }
  };
  /* eslint-disable */

  const toggleLoginFunc = e => {
    e && e.stopPropagation && e.stopPropagation();
    setType(isRQcodeFunc(type) ? LOGIN_DISPLAY.Account : LOGIN_DISPLAY.WeCom);
165
    props.updateLoginMode(isRQcodeFunc(type) ? LOGIN_WAY[LOGIN_DISPLAY.Account] : LOGIN_WAY[LOGIN_DISPLAY.WeCom]);
166 167 168 169
    Cookies.set('loginMode', isRQcodeFunc(type) ? LOGIN_WAY[LOGIN_DISPLAY.Account] : LOGIN_WAY[LOGIN_DISPLAY.WeCom], {
      expires: 5 * 60 * 1000,
      path: '/',
    });
170 171 172 173 174 175 176 177 178
  };

  return (
    <HelmetProvider>
      <Helmet>
        <title>{props.global.title || defaultSetting.title}</title>
        <meta name="description" content={props.global.title || defaultSetting.title} />
      </Helmet>
      <div className={styles.main}>
杨思琦's avatar
杨思琦 committed
179 180 181 182 183 184 185 186 187 188 189
        {
          showVideo && <video
            src={require('@/assets/videos/beforPage.mp4')}
            className={styles.videLayer}
            autoPlay="autoPlay"
            muted
            playsInline="playsinline"
            ref={videoRef}
          />
        }
        
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
        {/* main content */}
        <div className={styles.inner}>
          <div className={classNames(styles.loginTime, styles.caseHide)} ref={loginRef}>
            <img
              role="logo"
              src={
                props.global &&
                props.global.transformDevAssetsBaseURL &&
                props.global.transformDevAssetsBaseURL(props.global.logo)
              }
            />
            <div className={classNames(styles.titleCase, styles.caseHide, 'animated')} ref={titleRef}>
              <span className={styles.title}>{props.global.title}</span>
              <span className={styles.subtitle}>{window.globalConfig.subtitle}</span>
            </div>
          </div>
          <div className={classNames(styles.timeCase, styles.caseHide, 'animate__animated')} ref={timeRef}>
            <span className={styles.time}>{currentDate.time}</span>
            <span className={styles.dayofweek}>{currentDate.dayofweek}</span>
            <span className={styles.date}>{currentDate.date}</span>
          </div>
          {/* login container */}
          <div className={classNames(styles['login-block'], styles.caseHide, 'animate__animated')} ref={loginFormRef}>
            <div>
              <img src="https://panda-water.cn/web4/assets/images/login/dark/login.png" />
            </div>
            {/* right form */}
            <div className={styles['login-form']}>
              {/* 登录类型切换,扫码/账号密码 */}
              <div className={styles.loginFunc}>
                <img
                  title={type ? '密码登录' : '扫码登录'}
                  src={loginFuncImg(type)}
                  onClick={toggleLoginFunc}
                />
              </div>
              {renderPlatform()}
            </div>
          </div>
        </div>
        {/* footer */}
        <div className={classNames(styles.footerCase, styles.caseHide, 'animate__animated')} ref={footerRef}>
          <div className={classNames(styles.quickMark)}>{renderAddons}</div>
          <span className={classNames(styles.copyright)}>
            Copyright ©
            <a target="_blank" href="https://panda-water.cn">
              熊猫智慧水务
            </a>
            {new Date().getFullYear()} All Rights Reserved{' '}
            <a target="_blank" id="IndexCaseNumber" href="">
              ICP11036640-1
            </a>
            <span className="addons">
              <span className="split" />
              <a id="qrcode">
                <span className="glyphicon glyphicon-qrcode" role="button" title="手持APP下载" />
              </a>
            </span>
          </span>
        </div>
        <Modal centered visible={visible} width={340} footer={null} closable={false} bodyStyle={{ padding: '15px' }}>
          <div ref={sliVerify} />
        </Modal>
      </div>
    </HelmetProvider>
  );
});

const mapStateToProps = state => ({
  global: state.getIn(['global', 'globalConfig']),
  loginMode: state.getIn(['global', 'loginMode']),
});

const mapDispatchToProps = dispatch => ({
  updateConfig(config) {
    dispatch(actionCreators.getConfig(config));
  },
  createContext(data) {
    dispatch(actionCreators.createContext(data));
  },
  updateLoginMode(mode) {
    dispatch(actionCreators.changeLoginMode(mode));
  },
  updateCurrentIndex(index) {
    dispatch(actionCreators.updateCurrentIndex(index));
  },
});
export default connect(
  mapStateToProps,
  mapDispatchToProps,
)(withRouter(Login));