index.js 4.21 KB
Newer Older
zhanghaozhe committed
1 2
import React, { Component } from 'react';
import './veri-code-input.scss'
zhanghaozhe committed
3
import { http, api, 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 = () => {
82 83
        const {action, tel, account, challenge} = this.props
        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 90 91 92 93 94 95 96 97 98 99 100 101
            action: action || 'login',
            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
            })
        })
    }
zhanghaozhe committed
102

zhanghaozhe committed
103

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

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

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

zhanghaozhe committed
130

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

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

export default VeriCodeInput;