index.js 7.15 KB
Newer Older
王万里's avatar
王万里 committed
1 2 3 4 5 6 7
import React, { forwardRef, useEffect, useRef, useState } from 'react';

import { Modal, Popover } from 'antd';
import { Helmet, HelmetProvider } from 'react-helmet-async';
import { connect } from 'react-redux';
import { useHistory, withRouter } from '@wisdom-utils/runtime';
import { actionCreators } from '@/containers/App/store';
王万里's avatar
王万里 committed
8 9 10
import QRCode from 'qrcode.react';
import classnames from 'classnames';
import moment from 'moment';
王万里's avatar
王万里 committed
11 12 13 14 15
import defaultSetting from '../../../../../../../config/defaultSetting';
import LoginAction from '../../../login';
import styles from './index.less';
import Account from '../../../js/useAccount';
import { defaultApp } from '../../../../../../micro';
王万里's avatar
王万里 committed
16 17
import logo from './images/5.png';
import loginTitlePng from './images/9.png'
王万里's avatar
王万里 committed
18 19 20


const Login = forwardRef((props, _ref) => {
王万里's avatar
王万里 committed
21 22 23 24
  const sliVerify = useRef();
  const loginFormRef = useRef();
  const formRef = useRef(null);
  const [status, setStatus] = useState('normal');
王万里's avatar
王万里 committed
25

王万里's avatar
王万里 committed
26 27 28 29 30 31 32
  const [autoLogin, setAutoLogin] = useState(false);
  const [submitting, setSubmitting] = useState(false);
  const [type, setType] = useState('Account');
  const [visible, setVisible] = useState(false);
  const history = useHistory();
  const [action, setAction] = useState(() => new LoginAction(Object.assign({}, props, { history }), setVisible, false));
  const [dateObj, setDateObj] = useState({});
王万里's avatar
王万里 committed
33

王万里's avatar
王万里 committed
34 35
  const handleSubmit = values => {
    /* eslint-disable */
王万里's avatar
王万里 committed
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
        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(() => {
        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);
                // initMicroApps();
                defaultApp();
            });
        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]);

    useEffect(() => {
        setSubmitting(false);
    }, [visible]);

    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,
            welcome: null,
        };
        return <Account {...params} />;
    };
    /* eslint-disable */

    const handleWeek =() => {
        const weekOfDay = Number(moment().format('E'));
王万里's avatar
王万里 committed
99
        console.log(weekOfDay, "weekOfDay")
王万里's avatar
王万里 committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
        let weekDayName = ''
        switch(weekOfDay){
            case 1:
                weekDayName = '周一'
                break
            case 2:
                weekDayName = '周二'
                break
            case 3:
                weekDayName = '周三'
                break
            case 4:
                weekDayName = '周四'
                break
            case 5:
                weekDayName = '周五'
                break
            case 6:
                weekDayName = '周六'
                break
王万里's avatar
王万里 committed
120
            case 7:
王万里's avatar
王万里 committed
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
                weekDayName = '周日'
                break
            default:
                weekDayName = ''
        }
        return weekDayName
    }

    useEffect(()=>{
        const timer = setInterval(() => {
            setDateObj({
                curTime: moment().format('HH:mm:ss'),
                week: handleWeek(),
                date: moment().format('YYYY/MM/DD')
            });
          }, 1000);
          return () => {
            clearInterval(timer);
          };
    }, [])


    return (
        <HelmetProvider>
            <Helmet>
                <title>{props.global.title || defaultSetting.title}</title>
                <meta name="description" content={props.global.title || defaultSetting.title} />
            </Helmet>
王万里's avatar
王万里 committed
149
            <div className={classnames(styles.xinganlogin)}>
王万里's avatar
王万里 committed
150 151 152 153
                <div className={styles['inner-wrapper']}>
                    {/* <div className={styles['left-imgbox']}></div> */}
                    <div className={styles['inner-center']}>
                        <div className={styles['welcome-title']}>
王万里's avatar
王万里 committed
154
                            <img src={loginTitlePng} alt="login-title"/>
王万里's avatar
王万里 committed
155 156 157 158 159 160 161 162
                        </div>
                        <div className={classnames(styles['inner-bg'], styles['login-part'])} ref={loginFormRef}>
                            {renderPlatform()}
                        </div>
                    </div>
                </div>
                <div className={styles['login-header']}>
                    <div className={styles['left-title']}>
王万里's avatar
王万里 committed
163 164 165
                        <div>
                            <img src={logo} alt="logo"/>
                        </div>
166
                        <div className={styles['cn-title']}>易县水利局供水公司</div>
王万里's avatar
王万里 committed
167 168 169 170 171 172 173
                    </div>
                    <div className={styles['right-timebox']}>
                        <div className={styles['curr-time']}>{dateObj.curTime}</div>
                        <div className={styles['curr-week-date']}>
                            <div className={styles['curr-week']}>{dateObj.week}</div>
                            <div className={styles['curr-date']}>{dateObj.date}</div>
                        </div>
王万里's avatar
王万里 committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
                    </div>
                </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));