index.js 5.28 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
import { quickLogin } from '@/store/userAction';
zhanghaozhe committed
12
import OnSubmissionError from '../common/OnSubmissionError'
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
    onSubmissionError = () => {
        const errors = Object.values(this.props.errors);
xuzhenghua committed
39
        errors.length && Toast.info(errors[0], 2, null, false)
40
    }
zhanghaozhe committed
41 42

    render() {
zhanghaozhe committed
43 44
        const {
            values,
45
            isValid
zhanghaozhe committed
46
        } = this.props
zhanghaozhe committed
47 48
        return (
            <div className={'forgot-password'}>
xuzhenghua committed
49
                <HeaderBar title='忘记密码' arrow={true}/>
zhanghaozhe committed
50 51 52 53 54 55 56 57 58 59 60
                <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
61
                                        setFieldValue={form.setFieldValue}
zhanghaozhe committed
62 63 64 65 66 67
                                        icon={<i className={'iconfont iconshouji'}
                                                 style={{fontSize: '22px', left: '11px'}}
                                        />}
                                    />)
                            }}
                        />
zhanghaozhe committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
                        {
                            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}
zhanghaozhe committed
83 84
                                            action={'auth'}
                                            checking={1}
zhanghaozhe committed
85 86 87 88 89
                                        />
                                    )
                                }}
                            />
                        }
zhanghaozhe committed
90
                        <OnSubmissionError callback={this.onSubmissionError}/>
zhanghaozhe committed
91
                        <Captcha getInstance={this.getCaptchaInstance} onVerify={this.onVerify}/>
92
                        <Button active={isValid}>下一步</Button>
zhanghaozhe committed
93 94
                    </Form>
                </div>
zhanghaozhe committed
95 96 97 98 99 100
            </div>
        );
    }

}

zhanghaozhe committed
101 102 103 104 105 106

const formikConfig = {
    mapPropsToValues: () => ({
        account: '',
        veriCode: ''
    }),
107 108
    validateOnChange: true,
    validateOnBlur: true,
zhanghaozhe committed
109 110 111 112 113
    validate: values => {
        let errors = {}
        if (!validateTel(values.account) && !validateEmail(values.account)) {
            errors.account = '请输入正确的手机号或邮箱地址'
        }
114
        values.veriCode.toString().length !== 6 && (errors.veriCode = '验证码格式不正确')
zhanghaozhe committed
115 116 117
        return errors
    },
    handleSubmit(values, {props}) {
zhanghaozhe committed
118
        let account, address
119

zhanghaozhe committed
120 121 122
        if (validateEmail(values.account)) {
            account = 'email'
            address = 'check_email_code'
123 124
            sessionStorage.setItem('r_type', 'email')
            sessionStorage.setItem('email', values.account)
zhanghaozhe committed
125
        } else {
xuzhenghua committed
126
            account = 'phone'
zhanghaozhe committed
127
            address = 'check_phone_code'
128 129
            sessionStorage.setItem('r_type', 'phone')
            sessionStorage.setItem('tel', values.account)
zhanghaozhe committed
130 131
        }

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

}

export default compose(
    connect(
        null,
        {quickLogin}
zhanghaozhe committed
150 151
    ),
    withFormik(formikConfig)
zhanghaozhe committed
152
)(ForgotPassword)