cardAddActions.js 5.64 KB
'use strict';

import ReactNative from 'react-native';
import InstallmentService from '../../services/InstallmentService';
import {DeviceEventEmitter} from 'react-native';

const {

SET_ERROR,
SHOW_BANK_INFO,
SHOW_RESULT_INFO,
RESET_CARD_ADD_INFO,
SET_USER_NAME,

} = require('../../constants/actionTypes').default;


/**
 *  获取用户姓名,因为没有接口提供用户名,只能通过获取银行卡列表然后取第一张银行卡用户名称
 **/
export function getUserName() { 
    return (dispatch, getState) => {
        let {app} = getState();
        
        let queryUserName = (uid) => {
            return new InstallmentService(app.host).getBankCards(uid)
            .then(json => {
                let name = "";
                if (json && json.length > 0) {
                    name = json[0].userName
                }
                dispatch(setUserName(name));
            })
            .catch(error => {
                dispatch(setUserName(""));
            });
        };

        ReactNative.NativeModules.YH_CommonHelper.uid()
        .then(uid => {
            queryUserName(uid);
        })
        .catch(error => {
            ReactNative.NativeModules.YH_CommonHelper.login()
            .then(uid => {
                queryUserName(uid);
            })
            .catch(error => {

            });
        });
    };
}

export function setUserName(name) {
    return {
        type: SET_USER_NAME,
        payload: name,
    }
}


export function getBankInfo(cardNo) {

    return (dispatch, getState) => {

        let bankInfo = (uid) => {
            let {app} = getState();
            let info = {
                bankCode: '',
                bankName: '',
                cardNO: '',
                bankSupport: false,
                showBankInfo: false,
            };
            return new InstallmentService(app.host).fentchBankInfo(cardNo,uid)
            .then(json => {
                if (json.businessSupport == '0' && json.bankCode && json.bankCode != '') {
                    info.bankCode = json.bankCode;
                    info.bankName = json.bankName;
                    info.cardNO = json.cardNO;
                    info.bankSupport = true;
                    info.showBankInfo = true;
                }else {
                    info.bankCode = '';
                    info.bankName = '';
                    info.bankSupport = false;
                    info.showBankInfo = true;
                }
                dispatch(showBankInfo(info));
            })
            .catch(error => {
                info.bankCode = '';
                info.bankName = '';
                info.bankSupport = false;
                info.showBankInfo = true;
                dispatch(showBankInfo(info));
            });
        }

        ReactNative.NativeModules.YH_CommonHelper.uid()
        .then(uid => {
            bankInfo(uid);
        })
        .catch(error => {
            ReactNative.NativeModules.YH_CommonHelper.login()
            .then(uid => {
                bankInfo(uid);
            })
            .catch(error => {

            });
        });
    }
}

export function showBankInfo(info) {
    return {
        type: SHOW_BANK_INFO,
        payload: info,
    }
}


export function bindingCard(cardNo, mobile, bankName, bankCode) {

    return (dispatch, getState) => {

        let bindCards = (uid) => {
            let {app} = getState();
            let result = {
                showResult: false,
                result: '',
                message: '',
                description: '',
                buttonText: '',
            };
            
            return new InstallmentService(app.host).bindingCards(uid, cardNo, mobile, bankName, bankCode)
            .then(json => {
                result.showResult = true;
                result.result = 'success';
                result.message = '恭喜您,新增还款银行卡成功!';
                result.description = '';
                result.buttonText = '确认';
                dispatch(showResultInfo(result));
            })
            .catch(error => {
                result.showResult = true;
                result.result = 'fail';
                result.message = '对不起,绑定银行卡失败!';
                result.description = '失败的原因可能是:银行卡户名、银行卡、预留的手机号不一致';
                result.buttonText = '重新绑定银行卡';
                dispatch(showResultInfo(result));
            });
        }

        ReactNative.NativeModules.YH_CommonHelper.uid()
        .then(uid => {
            bindCards(uid);
        })
        .catch(error => {
            ReactNative.NativeModules.YH_CommonHelper.login()
            .then(uid => {
                bindCards(uid);
            })
            .catch(error => {

            });
        });
    }
}


export function showResultInfo(result) {
    return {
        type: SHOW_RESULT_INFO,
        payload: result,
    }
}


export function backToMyCardList() {
    return (dispatch, getState) => {
        ReactNative.NativeModules.YH_CommonHelper.goBack();
        DeviceEventEmitter.emit('BankCardChangeEvent');
    }
}


export function backToMyCardAdd() {
    return (dispatch, getState) => {

        let reset = {
            bankCode: '',
            bankName: '',
            cardNO: '',
            bankSupport: false,
            showBankInfo: false,
            showResult: false,
            result: '',
            message: '',
            description: '',
            buttonText: '',
        };

        dispatch(resetStateInfo(reset));
    }
}

export function resetStateInfo(state) {
    return {
        type: RESET_CARD_ADD_INFO,
        payload: state,
    }
}