order.js 2.76 KB
/**
 * 个人中心-订单列表 controller
 * @author: jing.li<jing.li@yoho.cn>
 * @date: 2016/08/15
 */

'use strict';

const headerModel = require('../../../doraemon/models/header'); // 头部model
const orderModel = require('../models/order');
const helpers = global.yoho.helpers;

/**
 * 订单页面
 * @param req
 * @param res
 * @param next
 */
exports.order = (req, res, next) => {
    let params = {
        type: req.query.type || 1,
        page: req.query.page || 1,
        gender: req.query.gender || '1,3',
        yh_channel: req.query.channel || 1,
        uid: req.user.uid
    };

    // 获取第一页数据做服务端渲染
    let initialData = {
        type: req.query.type || 1,
        page: req.query.page || 1,
        gender: req.query.gender || '1,3',
        yh_channel: req.query.channel || 1,
        uid: req.user.uid
    };

    return Promise.all([
        req.ctx(orderModel).order(params),
        req.ctx(orderModel).getOrders(initialData),
        req.ctx(orderModel).refundReason()
    ]).then(result => {
        // 申请退款原因
        let refundReason = result[2];

        res.render('order', {
            module: 'home',
            page: 'order',
            pageHeader: headerModel.setNav({
                navTitle: '我的订单',
                backUrl: helpers.urlFormat('/home')
            }),
            title: 'Yoho!Buy 有货',
            localCss: true,
            pageFooter: true,
            order: result[0] || [],
            refundReason: refundReason,
            walkwayUrl: result[1] && result[1].length ? '' : '//m.yohobuy.com/product/new',
            firstPageOrdersList: result[1] || []
        });
    }).catch(next);

};

/**
 * 获取订单
 * @param req
 * @param res
 * @param next
 */
exports.getOrders = (req, res, next) => {
    let start = req.query.start || 0;

    let params = {
        type: req.query.type || 1,
        page: req.query.page || 1,
        gender: req.query.gender || '1,3',
        yh_channel: req.query.channel || 1,
        uid: req.user.uid
    };

    req.ctx(orderModel).getOrders(params).then(result => {
        if (result && parseInt(params.page, 10) === 1 && parseInt(start, 10) > 0) {
            result = result.slice(start || 0);
        }
        res.render('order-content', {
            layout: false,
            orders: result
        });
    }).catch(next);
};

/**
 * 确认订单(确认收货)
 */
exports.sure = (req, res, next) => {
    let orderCode = req.body.orderCode;
    let uid = req.user.uid;

    if (!orderCode) {
        return res.json({
            code: 400,
            message: '没有订单号'
        });
    }

    req.ctx(orderModel).sure({
        order_code: orderCode,
        uid: uid
    }).then(result => {
        res.json(result);
    }).catch(next);
};