order.js 4.19 KB
/**
 * [个人中心]首页/我的订单
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2016/07/04
 */

'use strict';

const mcHandler = require('../models/menu-crumb-handler');
const orderModel = require('../models/order');
const _ = require('lodash');
const crypto = global.yoho.crypto;
const config = global.yoho.config;

/**
 * 我的订单
 */
const index = (req, res, next) => {
    const type = req.query.type;
    const page = req.query.page;
    const uid = req.user.uid;

    Promise.all([orderModel.getOrderData(uid, type, page), mcHandler.getMeThumb()]).then(result => {
        const pageData = result[0];
        const thumb = result[1];

        res.display('index', {
            page: 'order',
            isMe: true,
            content: Object.assign({
                nav: mcHandler.getMeCrumb(),
                navigation: mcHandler.getSideMenu('我的订单'),
                banner: thumb
            }, pageData)
        });
    }).catch(next);
};

const detail = (req, res, next) => {
    const code = req.query.orderCode;
    const uid = req.user.uid;

    Promise.all([orderModel.getOrderDetail(uid, code), mcHandler.getMeThumb()]).then(result => {
        const pageData = result[0];
        const thumb = result[1];

        if (_.has(pageData, 'orderDetail.orderCode')) {
            pageData.orderDetail.orderCodeM = crypto.encryption(config.crypto.common, pageData.orderDetail.orderCode);
        }

        res.display('index', {
            isMe: true,
            page: 'order-detail',
            content: Object.assign({
                nav: mcHandler.getMeCrumb(),
                navigation: mcHandler.getSideMenu('订单详情'),
                banner: thumb
            }, pageData)
        });
    }).catch(next);
};

const getOrderList = (req, res, next) => {
    const type = req.query.type;
    const page = req.query.page;
    const uid = req.user.uid;

    orderModel.getOrderData(uid, type, page).then(result => {
        res.display('order-table', {
            layout: false,
            page: 'order',
            isMe: true,
            orderList: result.order.orderList,
            paginationOpts: result.order.paginationOpts,
            orderData: result.order.orderData
        });
    }).catch(next);
};

const getOrderTotal = (req, res, next) => {
    const type = req.query.type;
    const uid = req.user.uid;


    orderModel.getOrderData(uid, type).then(result => {

        res.json({
            total: result.order.total,
            type: result.order.type
        });
    }).catch(next);
};

const cancelOrder = (req, res, next) => {
    const uid = req.user.uid;
    const data = req.query;

    orderModel.cancelOrder(uid, data).then(result => {
        res.json(result);
    }).catch(next);
};

const deleteOrder = (req, res, next) => {
    const uid = req.user.uid;
    const code = req.query.orderCode;

    orderModel.deleteOrder(uid, code).then(result => {
        res.json(result);
    }).catch(next);
};

const getExpressInfo = (req, res, next) => {
    const uid = req.user.uid;
    const code = req.query.orderCode;

    orderModel.getExpressInfo(uid, code).then(result => {
        res.json(result);
    }).catch(next);
};

const getCancelOrderReason = (req, res, next) => {
    orderModel.getCancelOrderReason().then(result => {
        res.json(result);
    }).catch(next);
};

const editOrder = (req, res, next) => {
    const uid = req.user.uid;
    const query = req.query;

    query.uid = uid;
    query.orderCode = crypto.decrypt(config.crypto.common, query.orderCode);

    orderModel.editOrder(query).then(result => {
        res.json(result);
    }).catch(next);
};

const reAdd = (req, res, next) => {
    const uid = req.user.uid;
    const code = req.query.orderCode;

    orderModel.reAddCart(uid, code).then(result => {
        res.json(result);
    }).catch(next);
};

const confirmReceive = (req, res, next) => {
    const uid = req.user.uid;
    const code = req.query.orderCode;

    orderModel.confirmReceive(uid, code).then(result => {
        res.json(result);
    }).catch(next);
};

module.exports = {
    index,
    detail,
    getOrderList,
    getOrderTotal,
    cancelOrder,
    deleteOrder,
    editOrder,
    getExpressInfo,
    getCancelOrderReason,
    reAdd,
    confirmReceive
};