index.js 6.67 KB
import React, { Component } from 'react'
import { connect } from 'dva'
import styles from './Login.css'
import $ from 'jquery';
import { Toast } from 'antd-mobile';
import shareHide from "../../utils/shareHide";
class Login extends Component {
  constructor(props){
    super(props)
    this.state={
      authenticationFailure:false,
      num :1,
      verificationCode:'',
      mobile:'',
      send:false,
      second:60,
      message:'' ,
      submit:{},
      userOpenId:'',
      publicOpenId:''
    }
  }
  history = ()=>{
    let that = this;
    let mobile = this.state.mobile;
    let verificationCode = this.state.verificationCode;
    if(!mobile){
      this.showToast('请输入正确的手机号!')
      return false;
    }
    if(!verificationCode){
      this.showToast('请输入正确的验证码!')
      return false;
    }
    $.ajax({
      //几个参数需要注意一下
      type: "POST",//方法类型
      url: "https://zmt.ihxlife.com/o2o/authentication/mobile" ,//url
      contentType: "application/x-www-form-urlencoded",
      headers: {
        'deviceId':this.state.userOpenId,     //微信号 userOpenId
        'gzhId':this.state.publicOpenId,   //公众号 publicOpenId
        Authorization:"Basic ZWR3aW5DbGllbnQ6ZWR3aW5TZWNyZXQ="
      },
      data: {"mobile":mobile ,"smsCode":verificationCode},
      success: function (result) {
        let userInfo = result.userInfo
        let storage = window.localStorage;
        storage.setItem("id",userInfo.id)                 //id
        storage.setItem("roleId",userInfo.roleId)         //角色
        storage.setItem("number",userInfo.number)        //手机号
        storage.setItem("name",userInfo.name)             //名字
        storage.setItem("public_id",userInfo.public_id)  //公众号
        storage.setItem("weixin_id",userInfo.weixin_id)   //微信号
        storage.setItem("superiorid",userInfo.superiorid)  //角色
        storage.setItem("orgId", userInfo.orgId ? userInfo.orgId : '') //机构id
        storage.setItem("project", userInfo.project ? userInfo.project:'') //项目id
        storage.setItem("website", userInfo.website ? userInfo.website:'') //网点id
        that.props.history.push('/welcome');
      },
      error : function(result) {
        // console.log(result.responseJSON.content);
        that.showToast('您所填写验证码错误,请重新验证')

      }
    });
  }
  showToast=(val)=>{
    Toast.info(val);
  }
  componentWillUnmount() {
    $('body,#root').removeClass('loginRelease');
  }
  componentDidMount() {
    shareHide();
    $(document).on('blur', 'input,textarea', function () {
      document.activeElement.scrollIntoView();
    })
    $('body,#root').addClass('loginRelease');
    let storage = window.localStorage;
    storage.setItem("id",'')                 //id
    storage.setItem("roleId",'')         //角色
    storage.setItem("number",'')        //手机号
    storage.setItem("name",'')             //名字
    storage.setItem("public_id",'')  //公众号
    storage.setItem("weixin_id",'')   //微信号
    storage.setItem("superiorid",'')  //角色
    let url = window.location.href;
    const matchs = decodeURI(url).match(/userOpenId=(.*)&publicOpenId=(.*)/i);
    if(matchs){
      let userOpenId = matchs[1];
      let publicOpenId = matchs[2];
      this.setState({
        userOpenId,
        publicOpenId
      })
    }
  }
  render() {
    return (
      <div className={styles.box}>
        <div className={styles.logo}>
          <img src={require('../../assets/image/home-logo.png')} alt=""/>
        </div>
        <div className={styles.phone}>
          <img src={require('../../assets/image/home-shadow.png')} alt=""/>
          <input type="number" placeholder="输入手机号码"
            value={this.state.mobile}
            onChange={
            (e)=>{
              this.setState({
                mobile:e.target.value.substr(0,11)
              })
              let storage = window.localStorage;
              storage.setItem("mobile",e.target.value)
            }
          }/>
          {
            !this.state.send && <span onClick={()=>{
              let mobile = this.state.mobile;
              let that = this;
              if(mobile.length !== 11){
                this.showToast('请填写正确的手机号!')
                return false
              }
              that.props.dispatch({
                type:'login/checkUser',
                payload:{
                  "number": mobile
                },
                callback(){
                  $.ajax({url:"https://zmt.ihxlife.com/o2o/code/sms?mobile="+ mobile,
                    type : "get",
                    headers:{"deviceId": that.state.userOpenId},
                    success:function(result){
                      that.showToast('验证码已发送,请注意查收!')
                      that.setState({
                        send:true,
                      })
                      let t1 = window.setInterval(function () {
                        that.setState({
                          second:that.state.second-1
                        })
                        if(that.state.second ===0 ){
                          window.clearInterval(t1);
                          that.setState({
                            send:false,
                            second:60
                          })
                        }
                      },1000)
                    }});
                },
                error(date){
                  that.showToast(date.message)
                }
              })




            }}>获取验证码</span>
          }
          {
            this.state.send &&<span >{this.state.second}</span>
          }
        </div>
        <div className={styles.verification}>
          <img src={require('../../assets/image/home-shadow.png')} alt="">
          </img>
          <input type="text" placeholder="输入验证码" onChange={(e)=>{
            this.setState({
              verificationCode:e.target.value,
            })
          }}/>
        </div>
        <div className={styles.approve} onClick={this.history}>
          <img src={require('../../assets/image/home-approve.png')} alt=""/>
        </div>
        <div className={styles.bottom}><p>该平台仅供内部员工注册使用</p></div>
        {this.state.authenticationFailure  &&<div className={styles.pop}>
          <div className={styles.up}>
            <p>认证失败</p>
            <p>该平台仅供内部员工注册使用!</p>
            <div className={styles.button}>
              <span>取消</span>
              <span>确定</span>
            </div>
          </div>
        </div>}
      </div>
    )
  }
}
export default connect(({ login }) => ({ login }))(Login);