orderDetail.js 10.1 KB
'use strict';


const api = global.yoho.API;

const _ = require('lodash');

// const config = global.yoho.config;
const helpers = global.yoho.helpers;
const camelCase = global.yoho.camelCase;
const logger = global.yoho.logger;

const closeReasons = () => {
    return api.get('', {
        method: 'app.SpaceOrders.closeReasons'
    }).then((result) => {
        if (result && result.code === 200) {
            return result.data;
        } else {
            return {};
        }
    });
};

/**
 * 获取快递有关信息
 * @private
 */
const _assignExpressInfo = (showLogistics, order, result) => {
    if (showLogistics && order.express_company.caption && order.express_number) {
        Object.assign(result, {
            logisticsUrl: helpers.urlFormat('/home/logistic', {order_code: order.order_code}),
            logisticsCompany: order.express_company.caption,
            logisticsNum: order.express_number
        });
    }
};

/**
 * 获取订单状态
 * @private
 */
const _getOrderStatus = (order, showLogistics) => {
    let result = {};

    if (order.isCancel === 'Y') {
        return {
            canceled: true
        };
    }

    // 先判断订单付款方式,根据不同的付款方式计算订单状态。(注:货到付款没有待付款状态)
    // 支付方式为非货到付款时,计算订单状态。
    if (parseInt(order.paymentType, 10) !== 2) {
        switch (parseInt(order.status, 10)) {
            case 0:

                /* 待付款 */
                Object.assign(result, {
                    unpaid: true,
                    payUrl: helpers.urlFormat('/home/orders/pay', {order_code: order.order_code})
                });
                break;
            case 1:
            case 2:
            case 3:

                /* 已付款状态不给查看物流 URL */
                Object.assign(result, {
                    unreceived: true
                });
                break;
            case 4:
            case 5:

                /* 已发货状态,给查看物流或二维码URL */
                Object.assign(result, {
                    unreceived: true
                });

                /* 是否门票 */
                if (order.virtualType && parseInt(order.virtualType, 10) === 3) {
                    Object.assign(result, {
                        qrcode: helpers.urlFormat(`/home/QRcode/${order.order_code}`)
                    });
                } else {
                    _assignExpressInfo(showLogistics, order, result);
                }
                break;
            case 6:

                /* 已成功订单,给查看物流或二维码URL */
                Object.assign(result, {
                    completed: true
                });

                /* 是否门票 */
                if (order.virtual_type && parseInt(order.virtual_type, 10) === 3) {
                    Object.assign(result, {
                        qrcode: helpers.urlFormat(`/home/QRcode/${order.order_code}`)
                    });
                } else {
                    _assignExpressInfo(showLogistics, order, result);
                }
                break;
            default:
                break;
        }
    } else {

        /* 订单为货到付款订单时,计算订单状态。(货到付款没有待付款状态) */
        switch (parseInt(order.status, 10)) {
            case 0:

                /* 备货中 */
                Object.assign(result, {
                    unpaid: true
                });
                break;
            case 1:
            case 2:
            case 3:

                /* 已付款状态不给查看物流URL */
                Object.assign(result, {
                    unreceived: true
                });
                break;
            case 4:
            case 5:

                /* 待收货状态,给查看物流 url */
                Object.assign(result, {
                    unreceived: true
                });
                _assignExpressInfo(showLogistics, order, result);
                break;
            case 6:

                /* 待收货状态,给查看物流 url */
                Object.assign(result, {
                    completed: true
                });
                _assignExpressInfo(showLogistics, order, result);
                break;
            default:
                break;
        }
    }

    return result;
};


const orderDetailData = (uid, orderCode) => {

    return api.get('', {
        method: 'app.SpaceOrders.detail',
        uid: uid,
        order_code: orderCode
    }).then((result) => {

        if (result && result.code === 200) {
            let orderDetail = camelCase(result.data);
            let goods = [];

            let status = _getOrderStatus(orderDetail);

            orderDetail = _.assign(orderDetail, status);
            orderDetail.useLimitCode = orderDetail.useLimitCode === 'Y';

            if (orderDetail.virtualType && orderDetail.virtualType === 3) {
                orderDetail = _.assign(orderDetail, {
                    isVirtual: true,
                    mobile: result.data.mobile
                });
            }

            orderDetail = _.assign(orderDetail, {
                addressAll: orderDetail.area + orderDetail.address

                // createTime: date('Y-m-d H:i:s', orderDetail.createTime)
            });

            // _.defer(function(orderDetail.createTime) {
                // console.log(_.now() - orderDetail.createTime);
            // }, _.now());

            if (orderDetail.counterFlag && orderDetail.counterFlag === 'Y') {
                orderDetail = _.assign(orderDetail, {
                    leftTime: parseInt(orderDetail.payLefttime, 10) * 1000
                });
            }

            _.forEach(orderDetail.orderGoods, function(data) {

                let obj = {};
                let count = +data.buyNumber;

                obj = _.assign(obj, {
                    thumb: data.goodsImage,
                    name: data.productName,
                    color: data.colorName,
                    size: data.sizeName,
                    price: data.goodsPrice,
                    count: count
                });

                if (data.goodsType === 'gift') {
                    obj = _.assign(obj, {
                        gift: true
                    });
                } else if (data.goodsType === 'priceGift') {
                    obj = _.assign(obj, {
                        advanceBuy: true
                    });
                }

                if (data.expectArrivalTime) {
                    obj = _.assign(obj, {
                        appearDate: data.expectArrivalTime
                    });
                }

                if (data.productSkn) {
                    obj = _.assign(obj, {
                        link: '/product/show_' + data.productSkn + '.html'
                    });
                }

                goods.push(obj);

                orderDetail = _.assign(orderDetail, {
                    orderCount: count
                });

            });

            orderDetail.goods = goods;

            if (orderDetail.paymentStatus === 'Y') {
                orderDetail = _.assign(orderDetail, {
                    isPay: true
                });
            }

            // 判断是否可以修改地址
            if (orderDetail.canUpdateDeliveryAddress === 'Y') {
                orderDetail = _.assign(orderDetail, {
                    changeable: true
                });
            }

            // 定金预售
            if (orderDetail.attribute * 1 === 9) {
                orderDetail = _.assign(orderDetail, {
                    isDepositAdvance: true
                });
            }

            // 判断是否有关联订单
            if (orderDetail.relateOrderCode === 'Y') {
                orderDetail = _.assign(orderDetail, {
                    relation: true
                });
            } else {
                orderDetail = _.assign(orderDetail, {
                    relation: false
                });
            }

            orderDetail = _.assign(orderDetail, {
                goodsAmount: orderDetail.paymentAmount,
                url: '/home/addressModify?orderCode=' + orderCode + '&relation=' + orderDetail.relation
            });

             // 为支付的拆单配送信息
            if (orderDetail.isMultiPackage && orderDetail.isMultiPackage === 'Y') {
                orderDetail = _.assign(orderDetail, {
                    isJit: true

                   // jitDetailUrl: '/cart/index/jitDetailUrl'
                });
            }

            // 取消订单原因列表
            let resons = closeReasons();

            return closeReasons().then(list => {
                resons = list;

                orderDetail = _.assign(orderDetail, {
                    cancelReason: resons
                });

                return orderDetail;
            });

            // return orderDetail;
        } else {
            logger.error('detail info return no 200');
            return {};
        }

    });
};

// 删除订单
const delOrder = (orderCode, uid) => {
    return api.get('', {
        method: 'app.SpaceOrders.delOrderByCode',
        uid: uid,
        order_code: orderCode
    });
};

// 再次购买
const readdData = (orderCode, uid) => {
    return api.get('', {
        method: 'app.Shopping.readd',
        uid: uid,
        order_code: orderCode
    }).then((result) => {
        if (result && result.code === 200) {
            result.message = '商品已重新加入购物车';
        } else if (result.code === '400') {
            result.message = '缺失参数';
        } else {
            result.message = '商品加入购物车失败';
            result.datat = {};
        }

        return result;
    });
};

// 取消订单
const cancelOrder = (orderCode, uid, reasonId, gender, channel, reason) => {


    return api.get('', {
        method: 'app.SpaceOrders.close',
        uid: uid,
        order_code: orderCode,
        reasonId: reasonId,
        reasons: reason,
        gender: gender,
        yh_channel: channel
    });
};

module.exports = {
    orderDetailData,
    closeReasons,
    delOrder,
    readdData,
    cancelOrder
};