index.js 4.71 KB
Newer Older
zhanghaozhe committed
1 2
import React, { Component } from 'react';
import './binding-tel.scss'
zhanghaozhe committed
3 4 5 6 7 8 9
import { withFormik, Field, Form } from "formik";
import { validateTel, http, api, getParam } from "@/utils";
import { compose } from "redux";
import { connect } from "react-redux";
import { setCurrentUser } from "@/store/userAction";
import Captcha from '@/common/Captcha'

zhanghaozhe committed
10 11 12 13

import ClearableInput from '@common/ClearableInput'
import Button from '../common/Button'
import VeriCodeInput from '../common/veriCodeInput'
zhanghaozhe committed
14 15
import { Toast } from "antd-mobile";
import { isEmpty } from "lodash";
zhanghaozhe committed
16 17 18

class BindingTel extends Component {

zhanghaozhe committed
19 20 21 22

    state = {
        validate: null,
        captchaInstance: null
zhanghaozhe committed
23 24
    }

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

zhanghaozhe committed
40 41

    render() {
zhanghaozhe committed
42 43 44 45
        const {
            values,
            errors
        } = this.props
zhanghaozhe committed
46 47 48
        return (
            <div className={'binding-tel'}>
                <p className={'title'}>为提高您的账号安全,请绑定手机号</p>
zhanghaozhe committed
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
                <Form>
                    <Field
                        name='tel'
                        render={({field, form}) => {
                            return (
                                <ClearableInput
                                    {...field}
                                    setFieldValue={form.setFieldValue}
                                    placeholder={'请输入需要绑定的手机号'}
                                    wrapperClass={'tel'}
                                    icon={<i className={'iconfont iconshouji'}
                                             style={{fontSize: '22px', left: '11px'}}
                                    />}
                                />

                            )
                        }}
                    />
                    <Field
                        name='veriCode'
                        render={({field}) => {
                            return (
                                <VeriCodeInput
                                    {...field}
                                    className={'verification'}
                                    icon={<i className={'iconfont iconduanxin'}
                                             style={{fontSize: '20px', left: '12px'}}
                                    />}
                                    account={values.tel}
                                    tel={values.tel}
                                    challenge={this.state.validate}
                                    instance={this.state.captchaInstance}
                                    action={'auth'}
                                />

                            )
                        }}
                    />
                    <Captcha onVerify={this.onVerify} getInstance={this.getCaptchaInstance}/>
                    <Button className={'complete-btn'} active={values.tel && values.veriCode && isEmpty(errors)}>完成</Button>
                </Form>
zhanghaozhe committed
90 91 92 93 94
            </div>
        );
    }
}

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

const formikConfig = {
    mapPropsToValues() {
        return {
            tel: '',
            veriCode: ''
        }
    },
    validateOnChange: true,
    validate(values) {
        let errors = {}
        if (!validateTel(values.tel)) {
            errors.tel = '请输入正确的手机号'
        }
        if (!values.veriCode) {
            errors.veriCode = '请填写验证码'
        }
        return errors
    },
    handleSubmit(values, {props}) {
zhanghaozhe committed
115
        http.post(`${API['passport-api']}/bind_mobile`, {
zhanghaozhe committed
116 117 118 119 120 121 122 123 124
            phone_num: values.tel,
            phone_code: values.veriCode,
            mkey: getParam('mkey'),
            plat: 5
        }).then(res => {
            const data = res.data

            if (data.errno == 200) {

zhanghaozhe committed
125
                if (data.data['is_set_pwd']) {
zhanghaozhe committed
126 127 128 129 130 131 132
                    props.setCurrentUser({
                        hasError: false,
                        data: {
                            uid: data.data.uid
                        },
                        msg: data.data.msg
                    })
zhanghaozhe committed
133
                    props.history.replace(`/passport/set-password`)
zhanghaozhe committed
134
                }else {
zhanghaozhe committed
135
                    location.assign(data.data['jump_url'])
zhanghaozhe committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
                }


            } else {
                Toast.info(data.msg, 2, null, false)
            }

            /*props.setCurrentUser({
                hasError,
                data
            })*/
        })
    }
}
export default compose(
    connect(
        null,
        {setCurrentUser}
    ),
    withFormik(formikConfig),
)(BindingTel);