index.js 5.63 KB
Newer Older
zhanghaozhe committed
1 2
import React, { Component } from "react"
import "./binding-tel.scss"
xuzhenghua committed
3
import { withFormik, Field, Form } from "formik"
zhanghaozhe committed
4
import { http, getParam, isValidUrl } from "src/utils"
xuzhenghua committed
5 6
import { compose } from "redux"
import { connect } from "react-redux"
zhanghaozhe committed
7
import { setCurrentUser } from "src/store/userAction"
zhanghaozhe committed
8 9 10 11
import { HeaderBar, CaptchaAli } from "src/common"
import ClearableInput from "../common/clearableInputWithCountryCodes"
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

class BindingTel extends Component {
xuzhenghua committed
16 17
  state = {
    validate: null,
zhanghaozhe committed
18
    captchaInstance: null,
zhanghaozhe committed
19
    validationData: null,
xuzhenghua committed
20
  }
zhanghaozhe committed
21

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

xuzhenghua committed
34
  render() {
zhanghaozhe committed
35
    const { values, errors, country } = this.props
xuzhenghua committed
36 37
    return (
      <>
zhanghaozhe committed
38 39 40
        <HeaderBar title={"绑定手机号"} arrow={true} />
        <div className={"binding-tel"}>
          <p className={"title"}>为提高您的账号安全,请绑定手机号</p>
xuzhenghua committed
41
          <Form>
zhanghaozhe committed
42 43
            <Field name="tel">
              {({ field, form }) => {
xuzhenghua committed
44 45 46 47
                return (
                  <ClearableInput
                    {...field}
                    setFieldValue={form.setFieldValue}
zhanghaozhe committed
48 49
                    placeholder={"请输入需要绑定的手机号"}
                    wrapperClass={"tel"}
xuzhenghua committed
50 51 52 53
                    country={country}
                  />
                )
              }}
zhanghaozhe committed
54
            </Field>
zhanghaozhe committed
55 56
            <Field name="veriCode">
              {({ field }) => {
xuzhenghua committed
57 58 59
                return (
                  <VeriCodeInput
                    {...field}
zhanghaozhe committed
60 61 62 63 64 65 66 67
                    className={"verification"}
                    icon={
                      <i
                        className={"iconfont iconduanxin"}
                        style={{ fontSize: "20px", left: "12px" }}
                      />
                    }
                    placeholder={"验证码"}
xuzhenghua committed
68 69 70
                    account={values.tel}
                    tel={values.tel}
                    instance={this.state.captchaInstance}
zhanghaozhe committed
71
                    action={"auth"}
xuzhenghua committed
72
                    country={country}
zhanghaozhe committed
73
                    validationData={this.state.validationData}
xuzhenghua committed
74 75 76
                  />
                )
              }}
zhanghaozhe committed
77
            </Field>
zhanghaozhe committed
78 79 80 81 82 83 84 85 86 87 88
            <CaptchaAli
              onVerify={this.onVerify}
              getInstance={this.getCaptchaInstance}
              mb={0}
            />
            <Button
              className={"complete-btn"}
              active={values.tel && values.veriCode && isEmpty(errors)}
            >
              完成
            </Button>
xuzhenghua committed
89 90 91 92 93
          </Form>
        </div>
      </>
    )
  }
zhanghaozhe committed
94 95
}

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