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

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

// 判断是否已经获取到了开通的状态值
const _reviewStatus = (status) => {
    if (status === 1) {
        return {
            review: {
                url: helpers.urlFormat('/product/new')
            }
        };
    } else if (status === 2) {
        return installmentModel.getSearchIntallment().then((goods) => {
            return {
                success: {
                    price: '5000',
                    installmentOnly: {
                        title: '分期专享',
                        goods: goods
                    }
                }
            };
        });

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

const index = (req, res) => {
    let query = req.query.query || '';
    let uid = req.user.uid || 3236556;

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

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

        if (openStatus === 0) {
            return {
                bannerTop: {
                    data: [
                        {
                            url: '',
                            src: '//img11.static.yhbimg.com/yhb-img01/2016/07/29/11/0112d076e4fa0c53635ed93b118ee3f74e.jpg?imageView2/2/w/640/h/240/q/70' // eslint-disable-line
                        }
                    ]
                },
                notOpen: true,
                installmentOnly: {
                    title: '分期专享',
                    goods: result[1]
                }
            };
        } else if (openStatus === 2) {
            if (query === 'usable') {
                return installmentModel.getQueryCreditInfo(uid).then((data) => {
                    if (data.status === 2) {
                        data.replayStatus = '逾期';
                    } else if (data.status === 3) {
                        data.replayStatus = '不可用';
                    }

                    return _.assign({
                        isUnable: true,
                        installmentOnly: {
                            title: '分期专享',
                            goods: result[1]
                        }
                    }, data);
                });
            } else {
                return installmentModel.getQueryAmtInfo(uid).then((data) => {
                    return _.assign({
                        isRepay: true
                    }, data);
                });
            }
        } else if (openStatus === 1) {
            res.redirect('/home/installment/review?status=' + openStatus);
        } else {
            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',
            title: '有货分期'
        }, result));
    });
};

const review = (req, res) => {
    let openStatus = req.query.status || false;
    let uid = req.user.uid || 20000032;
    let data = {
        module: 'home',
        page: 'installment',
        title: '有货分期',
        pageHeader: headerModel.setNav({
            navTitle: '有货分期',
            navBtn: false
        })
    };

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


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

const activateService = (req, res, next) => {
    installmentModel.activateService({
        uid: req.user.uid,
        userName: req.body.userName,
        identityCardNo: req.body.identityCardNo,
        cardNo: req.body.cardNo,
        mobile: req.body.mobile,
        snsCheckCode: req.body.snsCheckCode
    }).then((result)=> {
        res.json(result);
    }).catch(next);
};

const getBankInfo = (req, res, next) => {
    installmentModel.getBankInfo({
        cardNo: req.query.cardNo
    }).then((result)=> {
        res.json(result);
    }).catch(next);
};

const verifyCode = (req, res, next) => {
    installmentModel.sendVerifyCode(req.user.uid, req.body.mobile).then((result)=> {
        res.json(result);
    }).catch(next);
};

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

const orderDetail = (req, res) => {
    res.render('installment/order-detail', {
        module: 'home',
        page: 'installment.order-detail',
        navTitle: '分期详情',
        navBtn: false
    });
};

module.exports = {
    index,
    review,
    startingService,
    activateService,
    verifyCode,
    getBankInfo,
    order,
    orderDetail
};