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

zhanghaozhe committed
11

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

class BindingTel extends Component {

zhanghaozhe committed
21 22 23 24

    state = {
        validate: null,
        captchaInstance: null
zhanghaozhe committed
25 26
    }

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

zhanghaozhe committed
42 43

    render() {
zhanghaozhe committed
44 45
        const {
            values,
zhanghaozhe committed
46 47
            errors,
            country
zhanghaozhe committed
48
        } = this.props
zhanghaozhe committed
49
        return (
xuzhenghua committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63
            <>
                <HeaderBar title={'绑定手机号'} arrow={true}/>
                <div className={'binding-tel'}>
                    <p className={'title'}>为提高您的账号安全,请绑定手机号</p>
                    <Form>
                        <Field
                            name='tel'
                            render={({field, form}) => {
                                return (
                                    <ClearableInput
                                        {...field}
                                        setFieldValue={form.setFieldValue}
                                        placeholder={'请输入需要绑定的手机号'}
                                        wrapperClass={'tel'}
zhanghaozhe committed
64
                                        country={country}
xuzhenghua committed
65
                                    />
zhanghaozhe committed
66

xuzhenghua committed
67 68 69 70 71 72 73 74 75 76 77 78 79
                                )
                            }}
                        />
                        <Field
                            name='veriCode'
                            render={({field}) => {
                                return (
                                    <VeriCodeInput
                                        {...field}
                                        className={'verification'}
                                        icon={<i className={'iconfont iconduanxin'}
                                                 style={{fontSize: '20px', left: '12px'}}
                                        />}
zhanghaozhe committed
80
                                        placeholder={'验证码'}
xuzhenghua committed
81 82 83 84 85
                                        account={values.tel}
                                        tel={values.tel}
                                        challenge={this.state.validate}
                                        instance={this.state.captchaInstance}
                                        action={'auth'}
zhanghaozhe committed
86
                                        country={country}
xuzhenghua committed
87
                                    />
zhanghaozhe committed
88

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

zhanghaozhe committed
102 103 104 105 106 107 108 109 110 111 112

const formikConfig = {
    mapPropsToValues() {
        return {
            tel: '',
            veriCode: ''
        }
    },
    validateOnChange: true,
    validate(values) {
        let errors = {}
zhanghaozhe committed
113
        if (!/\d/.test(values.tel)) {
zhanghaozhe committed
114 115 116 117 118 119 120 121
            errors.tel = '请输入正确的手机号'
        }
        if (!values.veriCode) {
            errors.veriCode = '请填写验证码'
        }
        return errors
    },
    handleSubmit(values, {props}) {
zhanghaozhe committed
122
        http.post(`${API['passport-api']}/bind_mobile`, {
zhanghaozhe committed
123 124 125
            phone_num: values.tel,
            phone_code: values.veriCode,
            mkey: getParam('mkey'),
zhanghaozhe committed
126
            area_code: '00'+props.country.num,
zhanghaozhe committed
127 128 129 130 131
            plat: 5
        }).then(res => {
            const data = res.data
            if (data.errno == 200) {

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


            } else {
                Toast.info(data.msg, 2, null, false)
            }
        })
    }
}
export default compose(
    connect(
zhanghaozhe committed
155
        state => ({country: state.country}),
zhanghaozhe committed
156 157 158
        {setCurrentUser}
    ),
    withFormik(formikConfig),
FE committed
159
)(BindingTel);