orders.js 4.91 KB
'use strict';

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

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

    ordersModel.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: '缺少参数'
        });
    }

    req.ctx(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: '缺失必填项'
        });
    }

    req.ctx(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 = (function() {
        if (phoneNum) {
            if (phoneCode) {
                return `${phoneCode}-${phoneNum}`;
            } else {
                return phoneNum;
            }
        } else {
            return '';
        }
    }());
    let udid = req.yoho.udid;

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

    req.ctx(ordersService).updateDeliveryAddress(orderId, userName, areaCode, address, mobile, phone, uid, udid)
        .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: '参数缺失'
        });
    }

    req.ctx(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 || '';

    req.ctx(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');
    }

    req.ctx(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);
};

const express = (req, res, next) => {
    let orderId = req.query.orderId || '';
    let uid = req.user.uid;
    let payType = req.query.payType;
    let time = req.query.time;

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

    req.ctx(ordersService).express(orderId, uid, payType, time).then((result) => {
        return res.json(result);
    }).catch(next);
};

const refund = (req, res, next) => {
    let uid = req.user.uid;
    let orderId = req.query.orderId;
    let reasonId = req.query.reasonId;

    if (!orderId || !reasonId) {
        return res.json({
            code: 400,
            message: '参数错误'
        });
    }

    req.ctx(ordersService).refund(uid, orderId, reasonId).then((result) => {
        return res.json(result);
    }).catch(next);
};

const refundReason = (req, res, next) => {
    req.ctx(ordersService).refundReason().then((result) => {
        return res.json({
            code: 200,
            data: result
        });
    }).catch(next);
};

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