installmentActions.js 9.53 KB
'use strict';

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

const {

SET_PLATFORM,

INSTALLMENT_PRODUCT_REQUEST,
INSTALLMENT_PRODUCT_SUCCESS,
INSTALLMENT_PRODUCT_FAILURE,

GET_INSTALLMENT_STATUS_REQUEST,
GET_INSTALLMENT_STATUS_SUCCESS,
GET_INSTALLMENT_STATUS_FAILURE,

UPDATE_SHOW_BANK_ALERT_STATUS,
UPDATE_AGREE_PROTOCOL_STATUS,
UPDATE_SNS_CHECK_CODE_ENABLE_STATUS,
UPDATE_CELL_LIST,
UPDATE_NEXT_BTN_ENABLE_STATUS,
UPDATE_NEXT_BTN_TEXT,
UPDATE_SNS_CHECK_CODE_TEXT,

} = require('../../constants/actionTypes').default;
const codeContent = {
    openN: '0876085ff46bed27f1a1eb6ee8b68987',
    openY: 'c233ad4ca91fef45ed7d0b26d9953e61',
    advertisement: 'f3fe0793a7d59479542a14b15359c47d'
};

var Timer = function() {
    this.counter = 0;
    this.countdownTimer = null;
};

export function productListForInstallmentRequest(){
     return {
        type: INSTALLMENT_PRODUCT_REQUEST,
    }
}

export function productListForInstallmentSuccess(json){
     return {
        type: INSTALLMENT_PRODUCT_SUCCESS,
        payload:json,
    }
}

export function productListForInstallmentFailure(){
     return {
        type: INSTALLMENT_PRODUCT_FAILURE,
    }
}

export function featchInstallmentStatusRequest() {
    return {
        type: GET_INSTALLMENT_STATUS_REQUEST,
    }
}

export function featchInstallmentStatusSuccess(status) {
    return {
        type: GET_INSTALLMENT_STATUS_SUCCESS,
        payload: status
    }
}

export function featchInstallmentStatusFailure(error) {
    return {
        type: GET_INSTALLMENT_STATUS_FAILURE,
        payload: error
    }
}

export function updateShowBankAlertStatus(status) {
    return {
        type: UPDATE_SHOW_BANK_ALERT_STATUS,
        payload: status
    }
}

export function updateAgreeProtocolStatus(status) {
    return {
        type: UPDATE_AGREE_PROTOCOL_STATUS,
        payload: status
    }
}

export function updateSnsCheckCodeEnableStatus(status) {
    return {
        type: UPDATE_SNS_CHECK_CODE_ENABLE_STATUS,
        payload: status
    }
}

export function updateCellList(cellList) {
    return {
        type: UPDATE_CELL_LIST,
        payload: cellList
    }
}

export function updateNextBtnEnableStatus(status) {
    return {
        type: UPDATE_NEXT_BTN_ENABLE_STATUS,
        payload: status
    }
}

export function updateNextBtnText(text) {
    return {
        type: UPDATE_NEXT_BTN_TEXT,
        payload: text
    }
}

export function updateSnsCheckCodeText(text) {
    return {
        type: UPDATE_SNS_CHECK_CODE_TEXT,
        payload: text
    }
}

export function productListForInstallment() {
    return (dispatch, getState) => {
        let {app, installment} = getState();
        let {isFetching,open,installmentStatus} = installment;
        if(isFetching){
            return
        }
        dispatch(productListForInstallmentRequest());
        return new InstallmentService(app.host).fetchInstallmentProductList()
        .then(json => {
            let payload = json;//product_list
            if (payload.page > 1) {
                let oldList = open.fetchInstallmentProductList.list.toJS();
                let list = [...oldList, ...payload.list];
                payload.product_list = list;
            }
            dispatch(productListForInstallmentSuccess(payload));
        })
        .catch(error => {
            dispatch(productListForInstallmentFailure());
        });
    }
}

export function onPressOpenInstallment() {
    return (dispatch, getState) => {
        ReactNative.NativeModules.YH_InstallmentHelper.goToOpenInstallment();
    }
}

export function onPressMoreProducts() {
    return (dispatch, getState) => {
        //跳转原生筛选
        let getToInstallFilter = (uid) => {
            let url = `://m.yohobuy.com/home/installment/index?uid=${uid}?openby:yohobuy={"action":"go.instalmentlist","params":{"title":"分期专享"}}`;
            ReactNative.NativeModules.YH_CommonHelper.jumpWithUrl(url);
        }
        ReactNative.NativeModules.YH_CommonHelper.uid()
		.then(uid => {
			getToInstallFilter(uid);
		})
		.catch(error => {
			ReactNative.NativeModules.YH_CommonHelper.login()
			.then(uid => {
				getToInstallFilter(uid);
			})
			.catch(error => {

			});
		});
    }
}

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

        let installmentStatus = (uid) => {
            let {app, installment} = getState();
            dispatch(featchInstallmentStatusRequest());
            return new InstallmentService(app.host).getStauts(uid)
            .then(json => {
                let status = 0;
                if (json.status) {
                    status = json.status;
                }
                dispatch(productListForInstallment());
            })
            .catch(error => {
                dispatch(featchInstallmentStatusFailure(error));
            });
        }

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

			});
		});
    }
}

export function onPressProtocol() {
    return (dispatch, getState) => {
        //跳转原生静态页面
        let url = `://m.yohobuy.com/home/installment/agreement?openby:yohobuy={"action":"go.h5","params":{"url":"http://m.yohobuy.com/home/installment/agreement"}}`;
        ReactNative.NativeModules.YH_CommonHelper.jumpWithUrl(url);
    };
}

export function onPressOpenNext() {
    return (dispatch, getState) => {
        let {app, installment} = getState();
        let {nextBtnEnable} = installment.openPageInfo;
        if (!nextBtnEnable) {
            return;
        }
    };
}

export function onPressCheckbox() {
    return (dispatch, getState) => {
        let {app, installment} = getState();
        let {agreeProtocol} = installment.openPageInfo;
        agreeProtocol = !agreeProtocol;
        dispatch(updateAgreeProtocolStatus(agreeProtocol));
    };
}

export function onPressCardNoQuestion() {
    return (dispatch, getState) => {
        let {app, installment} = getState();
        let {showBankAlert} = installment.openPageInfo;
        showBankAlert = !showBankAlert;
        dispatch(updateShowBankAlertStatus(showBankAlert));
    };
}

export function updateOpenPageCellInfo(cellInfo) {
    return (dispatch, getState) => {
        let {app, installment} = getState();
        let {cellList,snsCheckCodeEnable} = installment.openPageInfo;
        cellList = cellList.toJS();
        let newCellList = cellList;
        let cardNoValid = false;
        let mobileValid = false;
        let nextBtnEnable = true;
        newCellList.map((item, i) => {
            if (item.inputKey == 'cardNo' && item.text.length >=13) {
                cardNoValid = true;
            } else if (item.inputKey == 'mobile' && item.text.length ==11) {
                mobileValid = true;
            }
            if (item.inputKey == cellInfo.inputKey) {
                item.text = cellInfo.text;
            }
            if (item.text == '') {
                nextBtnEnable = false;
            }
            cellList[i] = item;
        });
        if (cardNoValid && mobileValid) {
            snsCheckCodeEnable = true;
        }else {
            snsCheckCodeEnable = false;
        }
        dispatch(updateCellList(cellList));
        dispatch(updateSnsCheckCodeEnableStatus(snsCheckCodeEnable));
        dispatch(updateNextBtnEnableStatus(nextBtnEnable));
    };
}

/**
 *  倒计时
 *
 * @param start 启动回调
 * @param tick 进度回调
 * @param complete 完成回调
 */
Timer.prototype.startCountdown = function(start, tick, complete) {
    var self = this;

    if (this.counter > 0 || this.countdownTimer) {
        return;
    } else {
        this.counter = 59;
    }


    // 启动回调
    if (start) {
        start.call(this);
    }

    if (tick) {
        tick.call(this, this.counter);
    }

    this.complete = complete;


    // 开始计时器
    this.countdownTimer = setInterval(function() {
        self.counter--;

        if (self.counter <= 0) {
            if (complete) {
                clearInterval(self.countdownTimer);

                // 重置计时器
                self.counter = 0;
                self.countdownTimer = null;
                complete.call(self);
            }
        }

        // 完成回调
        if (tick && self.counter > 0) {
            tick.call(self, self.counter);
        }
    }, 1000);

    return this;
};

export function onPressCheckCode() {
    return (dispatch, getState) => {
        let {app, installment} = getState();
        let {cellList,snsCheckCodeEnable,snsCheckCodeText} = installment.openPageInfo;
        if (snsCheckCodeEnable && snsCheckCodeText == '获取验证码') {
            return new InstallmentService(app.host).sendVerifyCode('17705176933')
            .then(json => {
                console.log('json');
                console.log(json);
                let status = 0;
                let snsText = '';
                new Timer().startCountdown(function() {
                }, function(counter) {
                    // 进度回调
                    snsText = counter + 's';
                    dispatch(updateSnsCheckCodeText(snsText));
                }, function() {
                    // 倒计时结束后再次显示 "获取验证码"
                    snsText = '获取验证码';
                    dispatch(updateSnsCheckCodeText(snsText));
                });
                dispatch(updateSnsCheckCodeText(snsText));
            })
            .catch(error => {
            });
        }

    };
}