order-ensure.js 1.71 KB
/**
 * 订单结算controller
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2016/12/21
 */

'use strict';

const headerModel = require('../../../doraemon/models/simple-header');
const oeModel = require('../models/order-ensure');

const stepper = [
    {name: '查看购物车', focus: true},
    {name: '填写订单', focus: true},
    {name: '付款,完成购买'}
];

const index = (req, res, next) => {
    let cartType = req.query.type === '2' ? 'advance' : 'ordinary';

    oeModel.index(req.user.uid, cartType).then(result => {
        let header = headerModel.setSimpleHeaderData() || {};

        Object.assign(result, {
            stepper: stepper
        });

        res.render('order-ensure2016', {
            page: 'ensure',
            content: result,
            simpleHeader: header
        });
    }).catch(next);
};

// 获取优惠券列表
const getCoupons = (req, res, next) => {
    oeModel.getCoupons(req.user.uid).then(data => {
        res.send(data);
    }).catch(next);
};

// 订单金额计算
const compute = (req, res, next) => {
    let params = req.body;
    let cartType = params.cartType === '2' ? 'advance' : 'ordinary';

    oeModel.compute(req.user.uid, cartType, params).then(data => {
        res.send(data);
    }).catch(next);
};

// 提交订单
const submit = (req, res, next) => {
    let cartType = req.body.cartType === '2' ? 'advance' : 'ordinary';

    if (!req.body.addressId) {
        res.send({
            code: 500,
            message: '配送地址不能为空'
        });
        return;
    }

    oeModel.submit(req.user.uid, cartType, req.body).then(data => {
        res.send(data);
    }).catch(next);
};

module.exports = {
    index,
    getCoupons,
    compute,
    submit
};