index.js 5.36 KB
Newer Older
xuzhenghua committed
1
import React, { Component } from 'react'
zhanghaozhe committed
2
import './binding-tel.scss'
xuzhenghua committed
3
import { withFormik, Field, Form } from "formik"
zhanghaozhe committed
4
import { http, getParam } from "src/utils"
xuzhenghua committed
5 6
import { compose } from "redux"
import { connect } from "react-redux"
zhanghaozhe committed
7 8
import { setCurrentUser } from "src/store/userAction"
import { HeaderBar, CaptchaAli } from 'src/common'
zhanghaozhe committed
9
import ClearableInput from '../common/clearableInputWithCountryCodes'
zhanghaozhe committed
10 11
import Button from '../common/Button'
import VeriCodeInput from '../common/veriCodeInput'
xuzhenghua committed
12 13
import { Toast } from "antd-mobile"
import { isEmpty } from "lodash"
zhanghaozhe committed
14 15 16

class BindingTel extends Component {

zhanghaozhe committed
17

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

xuzhenghua committed
24 25 26 27 28
  getCaptchaInstance = instance => {
    this.setState({
      captchaInstance: instance
    })
  }
zhanghaozhe committed
29 30 31 32 33
  onVerify = (data) => {
    this.setState({
      validate: true,
      validationData: data
    })
xuzhenghua committed
34
  }
zhanghaozhe committed
35

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

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

xuzhenghua committed
84 85
                )
              }}
zhanghaozhe committed
86
            </Field>
zhanghaozhe committed
87
            <CaptchaAli onVerify={this.onVerify} getInstance={this.getCaptchaInstance} mb={0}/>
xuzhenghua committed
88 89 90 91 92 93 94
            <Button className={'complete-btn'}
                    active={values.tel && values.veriCode && isEmpty(errors)}>完成</Button>
          </Form>
        </div>
      </>
    )
  }
zhanghaozhe committed
95 96
}

zhanghaozhe committed
97 98

const formikConfig = {
xuzhenghua committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
  mapPropsToValues() {
    return {
      tel: '',
      veriCode: ''
    }
  },
  validateOnChange: true,
  validate(values) {
    let errors = {}
    if (!/\d/.test(values.tel)) {
      errors.tel = '请输入正确的手机号'
    }
    if (!values.veriCode) {
      errors.veriCode = '请填写验证码'
    }
    return errors
  },
  handleSubmit(values, {props}) {
wangshuo committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    const username = getParam('username')
    if (username) {
      //老账号绑定手机号
      http.post(`${API["passport-api"]}/m/personal/bindMobile`, {
        phone_num: values.tel,
        code: values.veriCode,
        type: 1,
        area_code: '00' + props.country.num,
        user_name: username
      }).then(res => {
        const {data, errno, msg} = res.data
        if (errno === 200) {
          props.setCurrentUser({
            hasError: false,
            data: {
              username: data.user_name,
              avatar: data.avatar_file,
              isVip: parseInt(data.vip),
              token: data.access_token,
              uid: data.uid
xuzhenghua committed
137 138
            }
          })
wangshuo committed
139 140
          const {from} = props.location.state || {from: {pathname: '/'}}
          props.history.push(from)
xuzhenghua committed
141
        } else {
wangshuo committed
142
          Toast.info(msg, 2, null, false)
zhanghaozhe committed
143
        }
wangshuo committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
      })
    } else {
      const uid = getParam('uid')
      const params = {
        phone_num: values.tel,
        phone_code: values.veriCode,
        mkey: getParam('mkey'),
        area_code: '00' + props.country.num,
        plat: 5,
        type: uid ? 1 : 2
      }
      if (uid) {
        params.uid = uid
      }
      http.post(`${API['passport-api']}/bind_mobile`, params).then(res => {
        const data = res.data
        if (data.errno == 200) {
          const {history} = props
          if (data.data['is_set_pwd']) {
            history.replace(`/passport/set-password`, {
              user: {
                hasError: false,
                data: {
                  uid: data.data.uid
                },
                msg: data.data.msg,
                stage: 'binding'
              }
            })
          } else {
            if (uid) {
              const redirect = localStorage.getItem('binding_redirect')
              if (redirect) {
                localStorage.removeItem('binding_redirect')
                history.replace(JSON.parse(redirect))
              } else {
                location.assign(data.data['jump_url'])
              }
            }
          }
zhanghaozhe committed
184 185


wangshuo committed
186 187 188 189 190 191
        } else {
          Toast.info(data.msg, 2, null, false)
        }
      })
    }

xuzhenghua committed
192
  }
zhanghaozhe committed
193 194
}
export default compose(
xuzhenghua committed
195 196 197 198 199 200
  connect(
    state => ({country: state.country}),
    {setCurrentUser}
  ),
  withFormik(formikConfig),
)(BindingTel)