index.js 4.26 KB
Newer Older
zhanghaozhe committed
1 2 3 4 5 6 7 8 9 10 11 12 13
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"
import { HeaderBar, ClearableInput, CaptchaAli } from "src/common"
import { validateEmail, http } from "src/utils"
import { quickLogin } from "src/store/userAction"
import OnSubmissionError from "../common/OnSubmissionError"
import { Link } from "react-router-dom"
zhanghaozhe committed
14 15

class ForgotPassword extends Component {
zhanghaozhe committed
16 17 18
  state = {
    validate: null,
    captchaInstance: null,
zhanghaozhe committed
19
    validationData: null,
zhanghaozhe committed
20
  }
zhanghaozhe committed
21

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

zhanghaozhe committed
38
  render() {
zhanghaozhe committed
39
    const { values, isValid } = this.props
zhanghaozhe committed
40
    return (
zhanghaozhe committed
41 42
      <div className={"forgot-password-email"}>
        <HeaderBar title="忘记密码" arrow={true} />
zhanghaozhe committed
43
        <div className="content">
zhanghaozhe committed
44
          <Form className="forgot-password-form">
zhanghaozhe committed
45
            <Field
zhanghaozhe committed
46 47
              name={"email"}
              render={({ field, form }) => {
zhanghaozhe committed
48 49 50
                return (
                  <ClearableInput
                    {...field}
zhanghaozhe committed
51 52 53
                    type={"email"}
                    placeholder={"请输入注册时的邮箱账号"}
                    wrapperClass={"email-input"}
zhanghaozhe committed
54
                    setFieldValue={form.setFieldValue}
zhanghaozhe committed
55 56
                  />
                )
zhanghaozhe committed
57 58
              }}
            />
zhanghaozhe committed
59
            {this.state.validate && (
zhanghaozhe committed
60
              <Field
zhanghaozhe committed
61 62
                name="veriCode"
                render={({ field }) => {
zhanghaozhe committed
63 64 65
                  return (
                    <VeriCodeInput
                      {...field}
zhanghaozhe committed
66 67 68 69 70 71 72
                      className={"verify-code"}
                      icon={
                        <i
                          className={"iconfont iconduanxin"}
                          style={{ fontSize: "20px", left: "12px" }}
                        />
                      }
zhanghaozhe committed
73 74 75 76
                      email={values.email}
                      challenge={this.state.validate}
                      instance={this.state.captchaInstance}
                      validationData={this.state.validationData}
zhanghaozhe committed
77
                      action={"auth"}
zhanghaozhe committed
78 79 80 81 82
                      checking={1}
                    />
                  )
                }}
              />
zhanghaozhe committed
83 84
            )}
            <OnSubmissionError callback={this.onSubmissionError} />
zhanghaozhe committed
85
            {/*<Captcha getInstance={this.getCaptchaInstance} onVerify={this.onVerify}/>*/}
zhanghaozhe committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99
            <CaptchaAli
              getInstance={this.getCaptchaInstance}
              onVerify={this.onVerify}
            />
            <Button className={"next_step"} active={isValid}>
              下一步
            </Button>
            <Link
              className={"to-phone"}
              to={"/passport/forgot-password"}
              replace
            >
              手机号找回
            </Link>
zhanghaozhe committed
100 101 102
          </Form>
        </div>
      </div>
zhanghaozhe committed
103
    )
zhanghaozhe committed
104
  }
zhanghaozhe committed
105 106 107
}

const formikConfig = {
zhanghaozhe committed
108
  mapPropsToValues: () => ({
zhanghaozhe committed
109 110
    email: "",
    veriCode: "",
zhanghaozhe committed
111 112 113
  }),
  validateOnChange: true,
  validateOnBlur: true,
zhanghaozhe committed
114
  validate: (values) => {
zhanghaozhe committed
115 116
    let errors = {}
    if (!validateEmail(values.email)) {
zhanghaozhe committed
117
      errors.email = "请输入正确的邮箱地址"
zhanghaozhe committed
118
    }
zhanghaozhe committed
119 120
    values.veriCode.toString().length !== 6 &&
      (errors.veriCode = "验证码格式不正确")
zhanghaozhe committed
121 122
    return errors
  },
zhanghaozhe committed
123 124 125 126 127 128 129 130 131
  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) => {
zhanghaozhe committed
132
        if (res.data.errno === 0) {
zhanghaozhe committed
133 134 135 136 137 138 139
          props.history.push("/passport/set-password", {
            from: props.location,
          })
        } else {
          Toast.info(res.data.msg, 2, null, false)
        }
      })
zhanghaozhe committed
140
  },
zhanghaozhe committed
141 142 143
}

export default compose(
zhanghaozhe committed
144
  connect(null, { quickLogin }),
zhanghaozhe committed
145
  withFormik(formikConfig)
zhanghaozhe committed
146
)(ForgotPassword)