repayDetailActions.js 5.02 KB
'use strict';

import ReactNative from 'react-native';
import InstallmentService from '../../services/InstallmentService';
import * as _ from 'lodash';

const {

SET_PLATFORM,
SET_REPAY_ORDER_NO,
SET_TIP_MESSAGE,
SET_ERROR,

QUERY_REPAY_RECORD_LIST_REQUEST,
QUERY_REPAY_RECORD_LIST_FAILURE,
QUERY_REPAY_RECORD_LIST_SUCCESS,

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

export function setRepayOrderNo(repayOrderNo){
     return {
        type: SET_REPAY_ORDER_NO,
        payload: repayOrderNo,
    }
}

export function setTipMessage(message){
     return {
        type: SET_TIP_MESSAGE,
        payload: message,
    }
}

export function setError(error) {
    return {
        type: SET_ERROR,
        payload: error,
    }
}

export function queryRepayRecordListRequest(){
     return {
        type: QUERY_REPAY_RECORD_LIST_REQUEST,
    }
}

export function queryRepayRecordListFailure(error){
     return {
        type: QUERY_REPAY_RECORD_LIST_FAILURE,
        payload: error,
    }
}

export function queryRepayRecordListSuccess(object){
     return {
        type: QUERY_REPAY_RECORD_LIST_SUCCESS,
        payload: object,
    }
}

// 处理还款记录数据
const _processRepayList = (list) => {
    if (list !== 'NULL') {
        _.forEach(list.rePayList, (data) => {
            if (data.status === 1) {
                data.repayStatus = '已还清';
                data.txt = '还款成功!';
                data.isTwo = true;
                data.isThree = true;
                delete data.desc;
            } else if (data.status === 2) {
                data.repayStatus = '还款失败';
                data.txt = '还款失败';
                if (data.desc && data.desc !== '') {
                    data.txt += `,${data.desc}`;
                }

                data.isTwo = true;
                data.isThree = true;
            } else {
                data.repayStatus = '还款中';
                data.txt = '预计1-2分钟内还款成功,<br>请到还款记录查看还款结果';
                delete data.desc;
            }

            data.url = `/home/installment/repay/detail?id=${data.prePayNo}`;

        });

        return list.rePayList;
    } else {
        return false;
    }
};

//还款详情
export function getRepayList() {
    return (dispatch, getState) => {
        let {app, repayDetail} = getState();
        let currentPage = 1;
        let queryRepayList = (uid) => {
            let {repayOrderNo} = repayDetail;
            let params = {
                uid,
                rePayNo: repayOrderNo,
                pageNo: currentPage,
            };
            dispatch(queryRepayRecordListRequest());
            return new InstallmentService(app.host).getQueryRePayList(params)
            .then(json => {
                let detailInfoList = _processRepayList(json);
                dispatch(queryRepayRecordListSuccess({detailInfoList,currentPage}));
            })
            .catch(error => {
                dispatch(setError(error));
            });
        };

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

			});
		});
    };
}

//还款记录
export function getRepayRecordList(page) {
    return (dispatch, getState) => {
        let {app, repayDetail} = getState();
        let {currentPage,repayList,pageTotal} = repayDetail;
        if (page) {
            currentPage = page
        } else {
            currentPage++;
        }
        if (pageTotal > 0 && currentPage > pageTotal) {
            return;
        }
        repayList = repayList.toJS();
        let queryRepayRecordList = (uid) => {
            let {repayOrderNo} = repayDetail;
            let params = {
                uid,
                pageNo: currentPage,
            };
            return new InstallmentService(app.host).getQueryRePayList(params)
            .then(json => {
                let detailInfoList = _processRepayList(json);
                let pageTotal = json.pageTotal;
                if (detailInfoList && detailInfoList.length > 0) {
        			detailInfoList = [...repayList, ...detailInfoList];
                    dispatch(queryRepayRecordListSuccess({detailInfoList,currentPage,pageTotal}));
                }
            })
            .catch(error => {
                dispatch(setError(error));
            });
        };

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

			});
		});
    };
}

export function gotoRepayRecordDetail(recordInfo) {
    return (dispatch, getState) => {
        let url = `http://m.yohobuy.com?openby:yohobuy={"action":"go.instalmentRepayRecordDetail","params":{"repayOrderCode":"${recordInfo.prePayNo}","isBackToRoot":false}}`;
        ReactNative.NativeModules.YH_CommonHelper.jumpWithUrl(url);
    };
}