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


class ForgotPassword extends Component {

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


zhanghaozhe committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
  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
41

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

}

zhanghaozhe committed
103 104

const formikConfig = {
zhanghaozhe committed
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
  mapPropsToValues: () => ({
    tel: '',
    veriCode: ''
  }),
  validateOnChange: true,
  validateOnBlur: true,
  validate: values => {
    let errors = {}
    if (!/\d/.test(values.tel)) {
      errors.tel = '请输入正确的手机号'
    }
    values.veriCode.toString().length !== 6 && (errors.veriCode = '验证码格式不正确')
    return errors
  },
  handleSubmit(values, {props}) {
    sessionStorage.setItem('r_type', 'phone')
    sessionStorage.setItem('tel', values.tel)
    http.post(`${API['passport-api']}/check_phone_code`, {
      phone: values.tel,
      code: values.veriCode,
      area_code: '00' + props.country.num
    }).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
134 135 136 137

}

export default compose(
zhanghaozhe committed
138 139 140 141 142
  connect(
    state => ({country: state.country}),
    {quickLogin}
  ),
  withFormik(formikConfig)
zhanghaozhe committed
143
)(ForgotPassword)