index.js 4.81 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 9
import { withFormik, Form, Field } from 'formik';
import { connect } from 'react-redux';
import { compose } from 'redux';
import { Toast } from "antd-mobile";
import { HeaderBar, Captcha, ClearableInput } from "@/common";
zhanghaozhe committed
10
import { validateTel, validateEmail, http, api } from "@/utils";
zhanghaozhe committed
11 12
import { quickLogin } from '@/store/userAction';
import { isEmpty } from "lodash";
zhanghaozhe committed
13 14 15 16


class ForgotPassword extends Component {

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


zhanghaozhe committed
23 24 25 26 27 28 29 30 31 32 33 34 35
    getCaptchaInstance = instance => {
        this.setState({
            captchaInstance: instance
        })
    }
    onVerify = (err, data) => {
        if (err) {
            console.log(err)
        } else {
            this.setState({
                validate: data.validate
            })
        }
zhanghaozhe committed
36 37 38 39
    }


    render() {
zhanghaozhe committed
40 41 42 43
        const {
            values,
            errors
        } = this.props
zhanghaozhe committed
44 45
        return (
            <div className={'forgot-password'}>
zhanghaozhe committed
46 47 48 49 50 51 52 53 54 55 56 57
                <HeaderBar title='忘记密码'/>
                <div className="content">
                    <Form className='forgot-password-form'>
                        <Field
                            name={'account'}
                            render={({field, form}) => {
                                return (
                                    <ClearableInput
                                        {...field}
                                        type={'tel'}
                                        placeholder={'请输入注册时的邮箱账号或手机号'}
                                        wrapperClass={'tel-input'}
zhanghaozhe committed
58
                                        setFieldValue={form.setFieldValue}
zhanghaozhe committed
59 60 61 62 63 64
                                        icon={<i className={'iconfont iconshouji'}
                                                 style={{fontSize: '22px', left: '11px'}}
                                        />}
                                    />)
                            }}
                        />
zhanghaozhe committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
                        {
                            this.state.validate &&
                            <Field
                                name='veriCode'
                                render={({field}) => {
                                    return (
                                        <VeriCodeInput
                                            {...field}
                                            className={'verify-code'}
                                            icon={<i className={'iconfont iconduanxin'}
                                                     style={{fontSize: '20px', left: '12px'}}
                                            />}
                                            account={values.account}
                                            challenge={this.state.validate}
                                            instance={this.state.captchaInstance}
                                        />
                                    )
                                }}
                            />
                        }
                        <Captcha getInstance={this.getCaptchaInstance} onVerify={this.onVerify}/>
                        <Button active={values.account && values.veriCode && isEmpty(errors)}>下一步</Button>
zhanghaozhe committed
87 88
                    </Form>
                </div>
zhanghaozhe committed
89 90 91 92 93 94
            </div>
        );
    }

}

zhanghaozhe committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

const formikConfig = {
    mapPropsToValues: () => ({
        account: '',
        veriCode: ''
    }),
    validateOnchange: true,
    validate: values => {
        let errors = {}
        if (!validateTel(values.account) && !validateEmail(values.account)) {
            errors.account = '请输入正确的手机号或邮箱地址'
        }
        return errors
    },
    handleSubmit(values, {props}) {

zhanghaozhe committed
111 112

        let account, address
113

zhanghaozhe committed
114 115 116
        if (validateEmail(values.account)) {
            account = 'email'
            address = 'check_email_code'
117 118
            sessionStorage.setItem('r_type', 'email')
            sessionStorage.setItem('email', values.account)
zhanghaozhe committed
119 120 121
        } else {
            account = 'phone_num'
            address = 'check_phone_code'
122 123
            sessionStorage.setItem('r_type', 'phone')
            sessionStorage.setItem('tel', values.account)
zhanghaozhe committed
124 125 126 127 128 129
        }

        http.post(`${api['passport-api']}/${address}`, {
            [account]: values.account,
            code: values.veriCode
        }).then(res => {
130
            if (res.data.errno == 0) {
zhanghaozhe committed
131
                props.history.push('/passport/set-password', {from: props.location})
132
            } else {
zhanghaozhe committed
133
                Toast.info(res.data.msg)
zhanghaozhe committed
134 135 136 137 138 139 140 141 142 143
            }
        })
    },

}

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