order.js 4.99 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 cleanHtml = require(`${global.utils}/cleanHtml`);
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.order_code')) {
            pageData.orderDetail.orderCodeM =
                crypto.encryption(config.crypto.common, (pageData.orderDetail.order_code).toString());
        }

        // 转译
        if (pageData.orderDetail) {
            pageData.orderDetail.address = cleanHtml.htmlDecode(pageData.orderDetail.address);
            pageData.orderDetail.allAddress = cleanHtml.htmlDecode(pageData.orderDetail.allAddress);
            pageData.orderDetail.remark = cleanHtml.htmlDecode(pageData.orderDetail.remark);
            if (pageData.orderDetail.invoice) {
                pageData.orderDetail.invoice.title = cleanHtml.htmlDecode(pageData.orderDetail.invoice.title);
            }
        }

        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 parameter = req.body;

    parameter.uid = uid;
    parameter.orderCode = crypto.decrypt(config.crypto.common, parameter.orderCode);
    if (!parameter.uid || !parameter.orderCode) {
        res.json({
            code: '401',
            message: '请输入合法订单号'
        });
    } else {

        orderModel.editOrder(parameter).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
};