index.js 5.28 KB
Newer Older
xuzhenghua committed
1
import React, { Component } from 'react'
zhanghaozhe committed
2
import './binding-tel.scss'
xuzhenghua committed
3 4 5 6 7
import { withFormik, Field, Form } from "formik"
import { http, getParam } from "@/utils"
import { compose } from "redux"
import { connect } from "react-redux"
import { setCurrentUser } from "@/store/userAction"
zhanghaozhe committed
8
import { HeaderBar, CaptchaAli } from '@/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 50 51 52 53 54 55 56 57 58
  render() {
    const {
      values,
      errors,
      country
    } = this.props
    return (
      <>
        <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'}
                    country={country}
                  />
zhanghaozhe committed
59

xuzhenghua committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
                )
              }}
            />
            <Field
              name='veriCode'
              render={({field}) => {
                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
79
                    validationData={this.state.validationData}
xuzhenghua committed
80
                  />
zhanghaozhe committed
81

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

zhanghaozhe committed
95 96

const formikConfig = {
xuzhenghua committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
  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}) {
zhanghaozhe committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    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
135 136
            }
          })
zhanghaozhe committed
137 138
          const {from} = props.location.state || {from: {pathname: '/'}}
          props.history.push(from)
xuzhenghua committed
139
        } else {
zhanghaozhe committed
140
          Toast.info(msg, 2, null, false)
zhanghaozhe committed
141
        }
zhanghaozhe committed
142 143
      })
    } else {
zhanghaozhe committed
144 145
      const uid = getParam('uid')
      const params = {
zhanghaozhe committed
146 147 148 149
        phone_num: values.tel,
        phone_code: values.veriCode,
        mkey: getParam('mkey'),
        area_code: '00' + props.country.num,
zhanghaozhe committed
150 151 152 153 154 155 156
        plat: 5,
        type: uid ? 1 : 2
      }
      if (uid) {
        params.uid = uid
      }
      http.post(`${API['passport-api']}/bind_mobile`, params).then(res => {
zhanghaozhe committed
157 158
        const data = res.data
        if (data.errno == 200) {
zhanghaozhe committed
159
          const {history} = props
zhanghaozhe committed
160 161 162 163 164 165 166 167 168 169 170 171
          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 {
zhanghaozhe committed
172 173 174 175 176 177
            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
178
            }
zhanghaozhe committed
179
          }
zhanghaozhe committed
180 181


zhanghaozhe committed
182 183 184 185 186 187
        } else {
          Toast.info(data.msg, 2, null, false)
        }
      })
    }

xuzhenghua committed
188
  }
zhanghaozhe committed
189 190
}
export default compose(
xuzhenghua committed
191 192 193 194 195 196
  connect(
    state => ({country: state.country}),
    {setCurrentUser}
  ),
  withFormik(formikConfig),
)(BindingTel)