cardAddActions.js 4.49 KB
'use strict';

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

const {

SET_ERROR,
SHOW_BANK_INFO,
SHOW_RESULT_INFO,
RESET_CARD_ADD_INFO,

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


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) => {
        let url = `http://m.yohobuy.com?openby:yohobuy={"action":"go.instalmentMyCard","params":{}}`;
        ReactNative.NativeModules.YH_CommonHelper.jumpWithUrl(url);
    }
}


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,
    }
}