password.js 8.11 KB
/**
 * Created by DEV005 on 2017/10/27.
 */
/**
 * Created by tdzl2003 on 12/18/16.
 */

import React, {Component, PropTypes} from 'react';
import {
    StyleSheet,
    Image,
    TouchableOpacity,
    TouchableHighlight,
    ScrollView,
    Button,
    TextInput,
    Text,
    View,
    Alert,
    NativeModules,
    ListView,
    RefreshControl,
    DeviceEventEmitter,
    AsyncStorage
} from 'react-native';
import MD5 from "md5"

import {xnToast} from "./../utils/utils";
import {xnAlert} from "./../utils/utils";
import AppService from "./../service/AppService";
import mainCss from "./../css/css";

import Verification from "./publish/verification";
import Password from "./publish/password";
import Loading from '../components/Loading'


export default class password extends Component {

    static navigationOptions = ({ navigation, screenProps })=>({
        title: '输入交易密码',
        headerLeft:(<View style={{flexDirection: 'row',flex: 1}}>
            <TouchableOpacity  style={{flexDirection: 'column',justifyContent: 'center',paddingLeft:10}} onPress={navigation.state.params?navigation.state.params._close:null}>
                <Image   style={{ width:20,height:20}}  source={require('../img/back.png')}  resizeMode="contain"/>
            </TouchableOpacity>
        </View>)
    });

    constructor(props){
        super(props);
        this.state = {
            title:"",
            isVerification:true,
            isLoading:false,
        };
    /*    AsyncStorage.setItem("transitNumber","0", function (errs) {
            xnToast("验证通过!");
        });*/
    }


    componentWillMount(){
        let  _this=this;

        //设置头部
        this.props.navigation.setParams({
            _goBack:()=>{
                this.props.navigation.goBack();
            },
            _close:()=>{
                NativeModules.system.navTo("BACK")
            }
        });

        //获取列表
        _this._init();
    }
    _init=()=>{
        let  _this=this;

        let  vm={
            userId:global.userId
        };
        AppService.transactionPasswordExist(vm).then(data=>{
            console.log(data);
            this.setState({
                isLoading:false
            });
            if (data.errors == null || data.errors.length > 0) {
                xnToast(data.errors[0].message);
                return
            }
            if(!data.verifyResult){
                Alert.alert(
                    '',
                    "你未设置过交易密码,请您设置6位数交易密码",
                    [
                        {text: '取消', onPress: () =>{
                            NativeModules.system.navTo("BACK")
                        }},
                        {text: '确定', onPress: () =>{
                            _this.props.navigation.navigate("Setting",{callBack:_this.callBackBarcode});
                       
                        }},
                    ]
                );


            }
        });

        AsyncStorage.getItem("transitNumber", function (errs, result) {
            console.warn('读取');
            console.warn(result);
            if (!errs) {
                if(result==null){
                    AsyncStorage.setItem("transitNumber","0", function (errs) {
                    });
                }else{
                    if(Number(result)>2){
                        _this.setState({
                            isVerification:false
                        })
                    }
                }
            }
        });

    };


    callBackBarcode=()=>{
        let _this=this;
        AsyncStorage.setItem("transitNumber","0", function (errs) {
            _this._init();
        });

    };
    //跳转
    toForget=()=>{
        this.props.navigation.navigate("Forget",{callBack:this.callBackBarcode});
    };

    _password=(password)=>{
        console.log(password);
        let  _this=this;
        let vm={
            transactionPassword: MD5(password),
            userId: global.userId
        };
        this.setState({
            isLoading:true
        });
        AppService.transactionPasswordVerify(vm).then(data=>{
            console.log(data)
            this.setState({
                isLoading:false
            });

            if(data.verifyResult){
                AsyncStorage.setItem("transitNumber","0", function (errs) {
                    NativeModules.system.openRnApp(global.module, global.location)
                });
            }else {
                AsyncStorage.getItem("transitNumber",function (errs, result) {
                    if (!errs) {
                        console.warn(result);

                        var resultNew=Number(result)+1;
                        AsyncStorage.setItem("transitNumber",resultNew+"", function (errs) {
                        });
                        DeviceEventEmitter.emit("initPassword","blur");
                        switch (Number(resultNew)){
                            case 1:
                                Alert.alert(
                                    '',
                                    "交易密码不正确,您还可以输入2次",
                                    [
                                        {text: '忘记密码', onPress: () =>{
                                            _this.toForget();
                                        }},
                                        {text: '重新输入', onPress: () =>{

                                        }},
                                    ]
                                );
                                break;
                            case 2:
                                Alert.alert(
                                    '',
                                    "交易密码不正确,您还可以输入1次",
                                    [
                                        {text: '忘记密码', onPress: () =>{
                                            _this.toForget();
                                        }},
                                        {text: '重新输入', onPress: () =>{
                                            DeviceEventEmitter.emit("initPassword","blur");
                                        }},
                                    ]
                                );
                                break;
                            case 3:
                            default:
                                Alert.alert(
                                    '',
                                    "交易密码输入3次错误",
                                    [
                                        {text: '取消', onPress: () =>{
                                            _this.setState({
                                                isVerification:false
                                            })
                                        }},
                                        {text: '找回密码', onPress: () =>{
                                            _this.toForget();
                                        }},

                                    ]
                                );
                                break;
                        };

                    }

                });
            }


        });

    };

    //跳转
    verification=(status)=>{
        console.log(status);
        let  _this=this;
        if(status){
            _this.setState({
                isVerification:true,
            });
            AsyncStorage.setItem("transitNumber","0", function (errs) {
            });

            // DeviceEventEmitter.emit("initPassword","q");
        }
    };

    render() {
        return (
            <View style={mainCss.body}>
                <View style={mainCss.main}>
                    {!this.state.isVerification&&<Verification toForget={this.toForget}  callBack={this.verification}></Verification>}
                    {this.state.isVerification&&<Password title={this.state.title} toForget={this.toForget} callBack={this._password} ></Password>}
                </View>
                <Loading  visible={this.state.isLoading} content="验证中"  ></Loading>
            </View>
        );
    };
   
}