index.js 3.99 KB
Newer Older
zhanghaozhe committed
1 2 3 4 5 6 7 8
import React, { Component } from 'react'
import './index.scss'
import VeriCodeInput from '../common/veriCodeInput'
import Button from '../common/Button'
import { withFormik, Form, Field } from 'formik';
import { connect } from 'react-redux';
import { compose } from 'redux';
import { Toast } from "antd-mobile";
zhanghaozhe committed
9 10
import { HeaderBar, ClearableInput, CaptchaAli } from "@/common";
import { validateEmail, http } from "@/utils";
zhanghaozhe committed
11 12 13 14 15 16 17
import { quickLogin } from '@/store/userAction';
import OnSubmissionError from '../common/OnSubmissionError'
import { Link } from "react-router-dom";


class ForgotPassword extends Component {

zhanghaozhe committed
18 19 20 21 22
  state = {
    validate: null,
    captchaInstance: null,
    validationData: null
  }
zhanghaozhe committed
23 24


zhanghaozhe committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
  getCaptchaInstance = instance => {
    this.setState({
      captchaInstance: instance
    })
  }
  onVerify = (data) => {
    this.setState({
      validationData: data,
      validate: true
    })
  }
  onSubmissionError = () => {
    const errors = Object.values(this.props.errors);
    errors.length && Toast.info(errors[0], 2, null, false)
  }
zhanghaozhe committed
40

zhanghaozhe committed
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
  render() {
    const {
      values,
      isValid
    } = this.props
    return (
      <div className={'forgot-password-email'}>
        <HeaderBar title='忘记密码' arrow={true}/>
        <div className="content">
          <Form className='forgot-password-form'>
            <Field
              name={'email'}
              render={({field, form}) => {
                return (
                  <ClearableInput
                    {...field}
                    type={'email'}
                    placeholder={'请输入注册时的邮箱账号'}
                    wrapperClass={'email-input'}
                    setFieldValue={form.setFieldValue}
                  />)
              }}
            />
            {
              this.state.validate &&
              <Field
                name='veriCode'
                render={({field}) => {
                  return (
                    <VeriCodeInput
                      {...field}
                      className={'verify-code'}
                      icon={<i className={'iconfont iconduanxin'}
                               style={{fontSize: '20px', left: '12px'}}
                      />}
                      email={values.email}
                      challenge={this.state.validate}
                      instance={this.state.captchaInstance}
                      validationData={this.state.validationData}
                      action={'auth'}
                      checking={1}
                    />
                  )
                }}
              />
            }
            <OnSubmissionError callback={this.onSubmissionError}/>
            {/*<Captcha getInstance={this.getCaptchaInstance} onVerify={this.onVerify}/>*/}
            <CaptchaAli getInstance={this.getCaptchaInstance} onVerify={this.onVerify}/>
            <Button className={'next_step'} active={isValid}>下一步</Button>
            <Link className={'to-phone'} to={'/passport/forgot-password'} replace>手机号找回</Link>
          </Form>
        </div>
      </div>
    );
  }
zhanghaozhe committed
97 98 99 100 101

}


const formikConfig = {
zhanghaozhe committed
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
  mapPropsToValues: () => ({
    email: '',
    veriCode: ''
  }),
  validateOnChange: true,
  validateOnBlur: true,
  validate: values => {
    let errors = {}
    if (!validateEmail(values.email)) {
      errors.email = '请输入正确的邮箱地址'
    }
    values.veriCode.toString().length !== 6 && (errors.veriCode = '验证码格式不正确')
    return errors
  },
  handleSubmit(values, {props}) {
    sessionStorage.setItem('r_type', 'email')
    sessionStorage.setItem('email', values.email)
    http.post(`${API['passport-api']}/check_email_code`, {
      email: values.email,
      code: values.veriCode
    }).then(res => {
      if (res.data.errno == 0) {
        props.history.push('/passport/set-password', {from: props.location})
      } else {
        Toast.info(res.data.msg, 2, null, false)
      }
    })
  },
zhanghaozhe committed
130 131 132 133

}

export default compose(
zhanghaozhe committed
134 135 136 137 138
  connect(
    null,
    {quickLogin}
  ),
  withFormik(formikConfig)
zhanghaozhe committed
139
)(ForgotPassword)