order-ensure.js 2.6 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.cartType === '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 addressList = (req, res, next) => {
    oeModel.addressList(req.user.uid).then(data => {
        res.send(data);
    }).catch(next);
};

// 获取区域列表
const getAreaList = (req, res, next) => {
    oeModel.areaList(req.query.id).then(data => {
        res.send(data);
    }).catch(next);
};

// 地址删除
const delAddress = (req, res, next) => {
    oeModel.delAddress(req.user.uid, req.body.id).then(data => {
        res.send(data);
    }).catch(next);
};

// 地址/更新新增
const saveAddress = (req, res, next) => {
    oeModel.saveAddress(req.user.uid, req.body).then(data => {
        res.send(data);
    }).catch(next);
};

// 设置默认地址
const setDefault = (req, res, next) => {
    oeModel.setDefault(req.user.uid, req.body.id).then(data => {
        res.send(data);
    }).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 cartType = req.query.cartType === '2' ? 'advance' : 'ordinary';

    oeModel.compute(req.user.uid, cartType, req.query).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,
    addressList,
    getAreaList,
    delAddress,
    saveAddress,
    setDefault,
    getCoupons,
    compute,
    submit
};