index.js 5.04 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 10
import { HeaderBar, Captcha } from "@/common";
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
    state = {
        validate: null,
        captchaInstance: null
zhanghaozhe committed
22 23 24
    }


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

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

}

zhanghaozhe committed
104 105 106

const formikConfig = {
    mapPropsToValues: () => ({
zhanghaozhe committed
107
        tel: '',
zhanghaozhe committed
108 109
        veriCode: ''
    }),
110 111
    validateOnChange: true,
    validateOnBlur: true,
zhanghaozhe committed
112 113
    validate: values => {
        let errors = {}
zhanghaozhe committed
114
        if (!/\d/.test(values.tel)) {
zhanghaozhe committed
115
            errors.tel = '请输入正确的手机号'
zhanghaozhe committed
116
        }
117
        values.veriCode.toString().length !== 6 && (errors.veriCode = '验证码格式不正确')
zhanghaozhe committed
118 119 120
        return errors
    },
    handleSubmit(values, {props}) {
zhanghaozhe committed
121 122 123 124 125
        sessionStorage.setItem('r_type', 'phone')
        sessionStorage.setItem('tel', values.tel)
        http.post(`${API['passport-api']}/check_phone_code`, {
            phone: values.tel,
            code: values.veriCode,
zhanghaozhe committed
126
            area_code: '00'+props.country.num
zhanghaozhe committed
127
        }).then(res => {
128
            if (res.data.errno == 0) {
zhanghaozhe committed
129
                props.history.push('/passport/set-password', {from: props.location})
130
            } else {
xuzhenghua committed
131
                Toast.info(res.data.msg, 2, null, false)
zhanghaozhe committed
132 133 134 135 136 137 138 139
            }
        })
    },

}

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