verification.js 6.63 KB
/**
 * Created by DEV005 on 2017/10/27.
 */
import React, {Component, PropTypes} from 'react';
import {
    StyleSheet,
    Image,
    TouchableOpacity,
    TouchableHighlight,
    ScrollView,
    Button,
    Text,
    View,
    NativeModules,
    TextInput
} from 'react-native';
import { observable, useStrict, action } from 'mobx';
import { observer } from 'mobx-react';
import {xnToast} from "../../utils/utils";
import AppService from "../../service/AppService";
import mainCss from "../../css/css";

import Loading from '../../components/Loading'


class sendState {
    @observable second = 60;
    @observable isSecond =true;
    @action send = () => {
        this.do && clearInterval(this.do);
        this.second=60;
        this.isSecond=true;
        let vm={
            object: global.mobilePhone,
            tenantNumber: global.tenantId,
            verificationType: "SHORT_MOBILE"
        };

        AppService.createVerificationCode(vm).then(data=>{
            if (data.errors == null || data.errors.length > 0) {
                xnToast(data.errors[0].message);
                return
            }
        });


        this.do = setInterval(() => {
            this.second--;
            if(this.second < 1){
                clearInterval(this.do);
                this.isSecond=false;
            };
        }, 1000)
    };
}

const newState = new sendState();
@observer
export default class setting extends Component {

    constructor(props){
        super(props);
        this.state = {
           str1:"",
           str2:"",
           str3:"",
           str4:"",
           isLoading:false,
           mobilePhoneFront:global.mobilePhone.substring(0,3),
           mobilePhoneLater:global.mobilePhone.substring(9,11)
        };
    }
    componentWillMount(){
        let _this=this;
        newState.send();
        // _this.init();
    };
    init=()=>{
        let vm={
            object: global.mobilePhone,
            tenantNumber: global.tenantId,
            verificationType: "SHORT_MOBILE"
        };

        AppService.createVerificationCode(vm).then(data=>{
            if (data.errors == null || data.errors.length > 0) {
                xnToast(data.errors[0].message);
                return
            }
        });
    };

//  获取焦距
    autoFocus=()=>{
        this.refs.TextInput.focus();
    };
    //改变
    onChangeText=(value)=>{
        let  _this=this;
        this.setState({
            str1:value.substring(0,1),
            str2:value.substring(1,2),
            str3:value.substring(2,3),
            str4:value.substring(3,4),

        });
        if(value.length==4){
            this.setState({
                isLoading:true
            });
            var vm = {
                object: global.mobilePhone,
                code: value,
                type: "SHORT_MOBILE"
            };
            AppService.checkvalification(vm).then(data=>{
                this.setState({
                    isLoading:false
                });
                if (data.errors == null || data.errors.length > 0) {
                    xnToast(data.errors[0].message);
                    return
                }
                if(data.isVerificated){
                    _this.props.callBack(true);
                }else {
                    xnToast("验证码错误,请重新输入!");
                    _this.refs.TextInput.blur();
                }
            });

        }

    };

    render() {
        return (
            <View style={styles.loginLayout}>
                <View  style={styles.tipLayout} >
                    <View  style={styles.tipLine}><Text style={styles.tipText}>我们已发送<Text style={styles.stress}> 验证码 </Text>到您的手机</Text></View>
                    <View  style={styles.tipLine}><Text style={[styles.tipText,styles.stress]}>+86 {this.state.mobilePhoneFront}******{this.state.mobilePhoneLater}</Text></View>
                </View>
                <TouchableOpacity style={styles.loginInput}  activeOpacity={1}  onPress={()=>{this.autoFocus()}}>
                    <View style={[styles.inputItem,styles.inputItemActive]}><Text style={styles.inputText}>{this.state.str1}</Text></View>
                    <View style={[styles.inputItem,this.state.str1?styles.inputItemActive:""]}><Text style={styles.inputText}>{this.state.str2}</Text></View>
                    <View style={[styles.inputItem,this.state.str2?styles.inputItemActive:""]}><Text style={styles.inputText}>{this.state.str3}</Text></View>
                    <View style={[styles.inputItem,this.state.str3?styles.inputItemActive:""]}><Text style={styles.inputText}>{this.state.str4}</Text></View>
                </TouchableOpacity>
                <View  style={styles.timeLayout} >
                    {newState.isSecond&&<Text style={styles.timeText}>{newState.second}S</Text>}
                    {!newState.isSecond&&<TouchableOpacity   onPress={()=>{newState.send()}}><Text style={styles.timeTextSend}>重新发送</Text></TouchableOpacity>}
                </View>
                <TextInput style={styles.hide}   autoFocus={true}  ref="TextInput"
                           onChangeText={(text) => {this.onChangeText(text)}}   keyboardType='numeric'    maxLength = {4} />
                 <Loading  visible={this.state.isLoading} content="验证中" ></Loading>
            </View>
        );
    }

};


const styles = StyleSheet.create({
    loginLayout:{
        flex:1,
        paddingTop:70,
        paddingLeft:25,
        paddingRight:25,
        backgroundColor:"#eee"
    },
    hide:{
        width:0,
        height:0
    },
    tipLayout:{
        marginBottom:20,
    },
    tipLine:{
        justifyContent:"center",
        flexDirection:"row",
        marginBottom:10,
    },
    tipText:{
        fontSize:20,
        color:"#666",
    },
    stress:{
        fontWeight: 'bold',
        color:"#000"
    },
    loginInput:{
        flexDirection:"row",
        paddingTop:10,
        paddingBottom:10,
        paddingLeft:25,
        paddingRight:25,
        height:50,
    },

    inputItem:{
        flex:1,
        flexDirection:"row",
        justifyContent:"center",
        borderColor:"#ddd",
        borderBottomWidth:1,
        marginLeft:10,
        marginRight:10,
        height:30,
    },
    inputItemActive:{
        borderColor:"#39f",
    },
    inputText:{
        color:"#333",
        fontSize:16,
    },
    timeLayout:{
        paddingTop:10,
        flexDirection:"row",
        justifyContent:"center",

    },
    timeText:{
        paddingTop:5,
        paddingBottom:5,
        color:"#999",
        fontSize:14,
    },
    timeTextSend:{
        paddingTop:5,
        paddingBottom:5,
        color:"#39f",
        fontSize:14,
    }

});