index.js 4.24 KB
Newer Older
zhanghaozhe committed
1 2
import React, { Component } from 'react';
import './veri-code-input.scss'
FE committed
3
import { http, validateEmail, validateTel } from '@/utils';
zhanghaozhe committed
4
import { Toast } from "antd-mobile";
zhanghaozhe committed
5
import { has } from 'lodash'
zhanghaozhe committed
6

zhanghaozhe committed
7 8 9 10 11 12

import classnames from 'classnames'

import Input from '../Input'

class VeriCodeInput extends Component {
zhanghaozhe committed
13
    count = 60
zhanghaozhe committed
14 15
    state = {
        counting: false,
zhanghaozhe committed
16 17
        count: this.count,
        isFirst: true
zhanghaozhe committed
18
    }
zhanghaozhe committed
19
    timer = null
zhanghaozhe committed
20

zhanghaozhe committed
21

zhanghaozhe committed
22 23
    countDown = () => {
        let {count} = this.state
zhanghaozhe committed
24
        if (!this.state.isFirst) {
zhanghaozhe committed
25
            Toast.info('请重新进行滑块验证', 2, null, false)
zhanghaozhe committed
26 27 28 29 30 31 32
            this.props.instance.refresh()
            this.setState({
                isFirst: true
            })
            return
        }

zhanghaozhe committed
33
        if (!this.state.counting) {
zhanghaozhe committed
34 35 36
            if (!this.sendCode()) {
                return
            }
zhanghaozhe committed
37
            this.setState({count: count--, counting: true})
zhanghaozhe committed
38 39 40
            this.timer = setInterval(() => {
                if (count <= 0) {
                    clearInterval(this.timer)
zhanghaozhe committed
41
                    this.setState({counting: false, count: this.count})
zhanghaozhe committed
42 43 44 45 46 47 48
                    return
                }
                this.setState({count: count--})
            }, 1000)
        }
    }

zhanghaozhe committed
49
    getType = () => {
zhanghaozhe committed
50 51
        const {account} = this.props
        if (validateEmail(account)) {
zhanghaozhe committed
52 53 54 55 56
            return 'email'
        }
    }


zhanghaozhe committed
57
    sendCode = () => {
zhanghaozhe committed
58
        if (!this.validate()) return
zhanghaozhe committed
59 60 61 62 63
        this.getType() === 'email' ? this.sendEmail() : this.sendSMS();
        return true;
    }

    sendEmail = () => {
zhanghaozhe committed
64
        const {account, challenge} = this.props
zhanghaozhe committed
65
        http.post(`${API['passport-api']}/send_email_code`, {
zhanghaozhe committed
66
            email: account,
zhanghaozhe committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
            challenge
        }).then(res => {
            if (res.data.errno === 0) {
                Toast.info('验证码发送成功', 2, null, false)
            } else {
                Toast.info(res.data.msg, 2, null, false)
            }
            this.setState({
                isFirst: false
            })
        })

    }

    sendSMS = () => {
zhanghaozhe committed
82
        const {action, tel, account, challenge, checking} = this.props
83
        if (!tel && !account) {
zhanghaozhe committed
84 85 86
            Toast.info('请输入手机号或邮箱地址')
            return
        }
zhanghaozhe committed
87
        http.post(`${API['passport-api']}/quick_sms`, {
88
            phone_num: tel || account,
zhanghaozhe committed
89
            action: action || 'login',
zhanghaozhe committed
90 91
            challenge,
            checking
zhanghaozhe committed
92 93 94 95 96 97 98 99 100 101 102
        }).then(res => {
            if (res.data.errno === 0) {
                Toast.info('验证码发送成功', 2, null, false)
            } else {
                Toast.info(res.data.msg, 2, null, false)
            }
            this.setState({
                isFirst: false
            })
        })
    }
zhanghaozhe committed
103

zhanghaozhe committed
104

zhanghaozhe committed
105
    validate = () => {
zhanghaozhe committed
106 107 108
        const {tel, challenge, account} = this.props
        let hasTel = has(this.props, 'tel')
        let content
zhanghaozhe committed
109

zhanghaozhe committed
110
        if (hasTel) {
zhanghaozhe committed
111 112 113
            if (!tel) {
                content = '手机号码不能为空'
            }
zhanghaozhe committed
114
            if (!validateTel(tel)) {
zhanghaozhe committed
115 116
                content = '请输入正确格式的手机号码'
            }
zhanghaozhe committed
117
        } else {
zhanghaozhe committed
118 119 120 121 122 123
            if (!account) {
                content = '手机号或电子邮件不能为空'
            }
            if (!validateTel(account) && !validateEmail(account)) {
                content = '请输入正确格式的手机号或电子邮件'
            }
zhanghaozhe committed
124
        }
zhanghaozhe committed
125 126

        if (content) {
zhanghaozhe committed
127
            Toast.info(content, 2, null, false)
zhanghaozhe committed
128 129
            return false
        }
zhanghaozhe committed
130

zhanghaozhe committed
131

zhanghaozhe committed
132 133 134 135 136
        if (!challenge) {
            Toast.info('请进行滑块验证', 2, null, false)
            return false
        }
        return true
zhanghaozhe committed
137 138 139
    }

    render() {
zhanghaozhe committed
140
        let {className, ...rest} = this.props
zhanghaozhe committed
141 142
        return (
            <Input
zhanghaozhe committed
143
                type={'number'}
144 145
                wrapperClass={className}
                {...rest}
zhanghaozhe committed
146
            >
zhanghaozhe committed
147 148
                <button type='button' className={classnames('verify', {active: !this.state.counting})}
                        onClick={this.countDown}>
zhanghaozhe committed
149 150 151 152 153 154 155 156 157 158 159 160
                    {
                        this.state.counting ?
                            (`重新发送${this.state.count}s`)
                            : '发送验证码'
                    }
                </button>
            </Input>
        );
    }
}

export default VeriCodeInput;