installment.js 11.1 KB
/**
 * 分期付款
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/08/01
 */
'use strict';

const installmentModel = require('../models/installment');
const _ = require('lodash');
const helpers = global.yoho.helpers;

// 判断是否已经获取到了开通的状态值
const _reviewStatus = (uid, status, next) => {
    if (status === '1') {
        return {
            review: {
                url: helpers.urlFormat('/product/new')
            }
        };
    } else if (status === '2') {
        return Promise.all([installmentModel.getSearchIntallment(), installmentModel.getQueryCreditInfo(uid)]).then((result) => { //eslint-disable-line
            return {
                success: {
                    price: result[1].initCreditLimit,
                    installmentOnly: {
                        title: '分期专享',
                        goods: result[0]
                    }
                }
            };
        }).catch(next);

    } else if (status === '3') {
        return {
            error: {
                url: helpers.urlFormat('/product/new')
            }
        };
    }
};

// 还款列表公共处理块
const _repaymentList = (req, res, next, title, params) => {
    params = _.assign({
        uid: req.cookies.installmentUid || 1
    }, params);

    installmentModel.getQueryAmtList(params).then((result) => {
        res.render('installment/repayment-list', {
            module: 'home',
            page: 'repayment-list',
            isInstallmentPage: true,
            title: title,
            data: result
        });
    }).catch(next);
};

// 开通分期首页
const index = (req, res, next) => {
    let uid = req.query.uid;

    Promise.all([installmentModel.getStauts(uid), installmentModel.getSearchIntallment()]).then((result) => {

        // status:0 未申请 1审核中 2已开通 3 审核未通过
        let openStatus = result[0];

        if (openStatus === '0') {
            return installmentModel.getResources().then(data => {
                return {
                    bannerTop: data,
                    notOpen: true,
                    installmentOnly: {
                        title: '分期专享',
                        goods: result[1]
                    }
                };
            });
        } else if (openStatus === '2') {
            return Promise.all([installmentModel.getQueryCreditInfo(uid), installmentModel.getQueryAmtInfo(uid)]).then((data) => { //eslint-disable-line
                let params = _.assign({
                    isOverdue: false,
                    installmentOnly: {
                        title: '分期专享',
                        goods: result[1]
                    }
                }, data[0], data[1]);

                // status: 1 正常 2 逾期 3 不可用 4 未开通
                if (data[0].status === '2') {
                    params.replayStatus = '逾期';
                    params.isOverdue = true;
                } else if (data[0].status === '3') {
                    params.replayStatus = '不可用';
                }

                return params;
            });
        } else if (openStatus === 1) {
            res.redirect('/home/installment/review?status=' + openStatus);
        } else if (openStatus === 3) {
            res.redirect('/home/installment/review?status=' + openStatus);
        }
    }).then((result) => {
        // result.pageHeader = _.assign({
        //     installmentPage: true
        // }, headerModel.setNav({
        //     navTitle: '有货分期',
        //     navBtn: false
        // }));

        res.render('installment/open-index', _.assign({
            module: 'home',
            page: 'installment',
            isInstallmentPage: true,
            title: '有货分期'
        }, result));
    }).catch(next);
};

// 开通结果显示
const review = (req, res, next) => {
    let openStatus = req.query.status || false;
    let uid = req.query.uid;
    let data = {
        module: 'home',
        page: 'installment',
        title: '有货分期'
    };

    if (openStatus !== '2') {
        res.render('installment/open-result', _.assign(data, _reviewStatus(uid, openStatus)));
    } else {
        _reviewStatus(uid, openStatus, next).then((params) => {
            res.render('installment/open-result', _.assign(data, params));
        });
    }

    // if (openStatus) {
    //     res.render('installment/open-result', _.assign(data, _reviewStatus(uid, +openStatus)));
    // } else {
    //     installmentModel.getStauts(uid).then((status) => {
    //         return _reviewStatus(uid, status);
    //     }).then((params) => {
    //         res.render('installment/open-result', _.assign(data, params));
    //     }).catch(next);
    // }
};

// 逾期未还款列表
const overdueList = (req, res, next) => {
    _repaymentList(req, res, next, '逾期未还金额', {
        queryDays: -1
    });
};

// 7日待还款列表
const sevenDayList = (req, res, next) => {
    _repaymentList(req, res, next, '近7日待还款', {
        queryDays: 7
    });
};

// 本月待还款列表
const monthRepayList = (req, res, next) => {
    _repaymentList(req, res, next, '本月待还金额', {
        queryDays: 30
    });
};

// 待还总金额列表
const totalRepayList = (req, res, next) => {
    _repaymentList(req, res, next, '待还总金额', {
        queryDays: 0
    });
};

// 还款记录页面渲染
const repayRecordPage = (req, res) => {
    res.render('installment/repay-record', {
        module: 'home',
        page: 'repay-record',
        isInstallmentPage: true,
        title: '还款记录'
    });
};

// ajax 请求还款记录
const getRepayRecord = (req, res, next) => {
    let params = _.assign({
        uid: req.cookies.installmentUid || 1,
        pageNo: req.query.page || 1
    });

    installmentModel.getQueryRePayList(params).then((result) => {
        if (result) {
            res.render('installment/record-template', {
                layout: false,
                recordData: result
            });
        } else {
            res.json();
        }
    }).catch(next);
};

// 账号管理
const account = (req, res, next) => {
    let uid = req.cookies.installmentUid || 512579468;

    installmentModel.getBankCards(uid).then((result) => {
        res.render('installment/account', {
            accountList: result,
            title: '账号管理',
            isInstallmentPage: true,
            repaymentList: result
        });
    }).catch(next);
};

const startingService = (req, res) => {
    res.render('installment/starting-service', {
        module: 'home',
        page: 'installment.starting-service',
        title: '开通有货分期',
        navTitle: '开通有货分期',
        navBtn: false
    });
};

const activateService = (req, res, next) => {
    installmentModel.activateService({
        uid: req.cookies.installmentUid || 532892,
        userName: req.body.userName,
        identityCardNo: req.body.identityCardNo,
        cardNo: req.body.cardNo,
        mobile: req.body.mobile,
        snsCheckCode: req.body.snsCheckCode,
        bankCode: req.body.bankCode,
        bankName: req.body.bankName
    }).then((result)=> {
        res.json(result);
    }).catch(next);
};

const getBankInfo = (req, res, next) => {
    installmentModel.getBankInfo({
        cardNo: req.query.cardNo,
        uid: req.cookies.installmentUid || 512579468 // TODO: fix uid
    }).then((result)=> {
        res.json(result);
    }).catch(next);
};

const verifyCode = (req, res, next) => {
    installmentModel.sendVerifyCode(req.cookies.installmentUid || 1, req.query.mobile).then((result)=> {
        res.json(result);
    }).catch(next);
};

const orderIndex = (req, res) => {
    res.render('installment/order', {
        module: 'home',
        page: 'installment.order',
        title: '我的分期订单',
        navBtn: false,
        isInstallmentPage: true
    });
};

const orderList = (req, res, next) => {
    const params = {
        uid: req.cookies.installmentUid || 8041876, // TODO: fix me
        type: req.query.type || 1,
        page: req.query.page || 1,
        limit: req.query.limit || 10
    };

    installmentModel.getInstallmentOrders(params).then((result)=> {
        res.render('installment/order-list', {
            title: '我的分期订单',
            orders: result.data ? result.data.orderList : [],
            layout: false,
            isInstallmentPage: true
        });
    }).catch(next);
};

const orderDetail = (req, res, next) => {
    const params = {
        uid: req.cookies.installmentUid || 8041876, // TODO: fix me
        orderCode: req.params.id
    };

    installmentModel.getInstallmentOrderDetail(params).then((result)=> {
        res.render('installment/order-detail', {
            module: 'home',
            page: 'installment.order-detail',
            title: '分期详情',
            order: result.data,
            navBtn: false,
            currAmtCount: 0,
            currFeeCount: 0,
            isCurrFee: true,
            helpers: {
                isPaymentComplete: function(status, options) {
                    if (status === 2) { // 已结清
                        return options.fn(this);
                    }

                    return options.inverse(this);
                },
                isPaymentIncomplete: function(status, options) {
                    if (status !== 2) { // 其他状态
                        return options.fn(this);
                    }

                    return options.inverse(this);
                },
                greaterThanZero: function(value, options) {
                    if (value && parseFloat(value) > 0) {
                        return options.fn(this);
                    }

                    return options.inverse(this);
                }
            }
        });
    }).catch(next);
};

// 还款详情
const repayDetail = (req, res, next) => {
    let params = {
        uid: req.cookies.installmentUid || 512579468,
        rePayNo: req.query.id || '',
        pageNo: 1
    };

    installmentModel.getQueryRePayList(params).then((result) => {
        res.render('installment/repay-detail', _.assign({
            title: '还款详情',
            isInstallmentPage: true,
            isOne: true
        }, result[0]));
    }).catch(next);
};

// 帮助静态页面
const help = (req, res) => {
    res.render('installment/help', {
        title: '帮助'
    });
};

// 服务条款静态页面
const serviceProvision = (req, res) => {
    res.render('installment/service-provision', {
        title: '服务条款'
    });
};

// 协议静态页面
const agreement = (req, res) => {
    res.render('installment/agreement', {
        title: '服务协议'
    });
};

// 计算金额
const totalAmount = (req, res, next) => {
    installmentModel.totalAmount(req.query.prices).then((result) => {
        res.json(result);
    }).catch(next);
};


module.exports = {
    index,
    review,
    overdueList,
    sevenDayList,
    monthRepayList,
    totalRepayList,
    repayRecordPage,
    getRepayRecord,
    startingService,
    activateService,
    verifyCode,
    getBankInfo,
    account,
    orderIndex,
    orderList,
    orderDetail,
    repayDetail,
    help,
    serviceProvision,
    agreement,
    totalAmount
};