orders.js 3.49 KB
'use strict';

const _ = require('lodash');
const ordersService = require('../models/orders-service');

const index = (req, res, next) => {
    let uid = req.user.uid;
    let page = _.parseInt(req.query.page) || 1;
    let type = _.parseInt(req.query.type) || ordersService.ORDER_TYPE.all;
    let limit = _.parseInt(req.query.limit) || 10;

    ordersService.index(uid, page, limit, type).then((result) => {
        return res.render('home/orders/orders', {
            meOrdersPage: true,
            meOrders: result
        });
    }).catch(next);
};

const reBuy = (req, res, next) => {
    let uid = req.user.uid;
    let orderId = req.body.orderCode || '';

    if (!orderId) {
        return res.json({
            code: 400,
            message: '缺少参数'
        });
    }

    ordersService.reBuy(uid, orderId).then((result) => {
        return res.json(result);
    }).catch(next);
};

const del = (req, res, next) => {
    let uid = req.user.uid;
    let orderId = req.body.orderCode || '';
    let channel = req.yoho.channel;
    let gender = req.yoho.gender;

    if (!orderId) {
        return res.json({
            code: 400,
            message: '缺失必填项'
        });
    }

    ordersService.del(uid, gender, channel, orderId).then((result) => {
        return res.json(result);
    }).catch(next);
};

const modifyAddress = (req, res, next) => {
    let uid = req.user.uid;
    let orderId = req.body.orderCode || '';   //订单号
    let userName = req.body.userName || '';   //收货人
    let areaCode = req.body.areaCode || '';   //区号
    let address = req.body.address || '';     //地址
    let mobile = req.body.mobile || '';       //手机号码
    let phoneNum = req.body.phoneNum || '';   //固定电话
    let phoneCode = req.body.phoneCode || ''; //电话编号
    let phone = phoneNum ? `${phoneCode}-${phoneNum}` : '';

    if (!orderId || !userName || !areaCode || !address) {
        return res.json({
            code: 400,
            message: '缺失必镇项'
        });
    }

    ordersService.updateDeliveryAddress(orderId, userName, areaCode, address, mobile, phone, uid)
        .then(result => res.json(result))
        .catch(next);
};

const confirm = (req, res, next) => {
    let uid = req.user.uid;
    let orderId = req.body.orderCode || '';

    if (!orderId) {
        return res.json({
            code: 400,
            message: '参数缺失'
        });
    }

    ordersService.confirm(uid, orderId).then((result) => {
        return res.json(result);
    }).catch(next);
};

const cancel = (req, res, next) => {
    let uid = req.user.uid;
    let orderId = req.body.orderCode || '';
    let reason = req.body.reason || '';
    let reasonId = req.body.reasonId || '';

    ordersService.cancel(uid, orderId, reason, reasonId)
        .then(result => res.json(result))
        .catch(next);
};

const detail = (req, res, next) => {
    let orderId = req.query.orderCode || req.query.order_code;
    let uid = req.user.uid;

    if (!orderId) {
        return res.redirect('/home');
    }

    ordersService.detail(uid, orderId).then((result) => {
        if (_.isEmpty(result)) {
            return res.redirect('/home');
        }

        return res.render('home/orders/order-detail', {
            meOrderPage: true,
            meOrderDetail: result.detail,
            packages: result.package,
            cancelReason: result.cancelReason
        });
    }).catch(next);
};

module.exports = {
    index,
    reBuy,
    del,
    modifyAddress,
    confirm,
    cancel,
    detail
};