order.js 9.24 KB
'use strict';

const api = global.yoho.API;
const helpers = global.yoho.helpers;
const _ = require('lodash');

/**
 * 门票
 * @type {{SINGLE_TICKETS_SKN: number}}
 */
const TICKETS = {
    SINGLE_TICKETS_SKN: 51335912, // 展览票(单日票)skn
    PACKAGE_TICKETS_SKN: 51335908 // 套票skn
};

/**
 * 格式化订单商品
 * @private
 */
const _formatOrderGoods = (orderGoods, count, haveLink, tickets) => {
    let result = [];

    _.forEach(orderGoods, value => {
        let goods = {
            thumb: value.goods_image,
            name: value.product_name,
            color: value.color_name,
            size: value.size_name,
            price: value.goods_price,
            count: value.buy_number
        };

        /* gift=>是否赠品,advanceBuy=>是否加价购 */
        if (value.goods_type === 'gift') {
            Object.assign(goods, {
                gift: true
            });
        } else if (value.goods_type === 'price_gift') {
            Object.assign(goods, {
                advanceBuy: true
            });
        }

        /* 上市期 */
        if (value.expect_arrival_time) {
            Object.assign(goods, {
                appearDate: value.expect_arrival_time
            });
        }

        /* 商品链接 */
        if (haveLink && value.product_skn) {
            Object.assign(goods, {
                link: helpers.urlFormat('/product/show_' + value.product_skn + '.html')
            });
        }

        /* 累计购买数 */
        count += parseInt(value.buy_number, 10);

        /* 门票 */
        if (tickets) {

            /* 展览票不显示区域 */
            if (value.product_skn === TICKETS.SINGLE_TICKETS_SKN) {
                delete goods.size;
            }
            Object.assign(goods, {
                tickets: true
            });
        }

        result.push(goods);
    });

    return result;
};

/**
 * 获取快递有关信息
 * @private
 */
const _assignExpressInfo = (showLogistics, order) => {
   let data = {};

    if (showLogistics && order.express_company && order.express_company.caption) {
        data = {
            logisticsUrl: helpers.urlFormat('/home/logistic', {order_code: order.order_code}),
            logisticsCompany: order.express_company.caption,
            logisticsNum: order.express_number || ''
        };
    }

    return data;
};

/**
 * 调用接口获取订单数据
 * @returns {*}
 * @private
 * @param params
 */
const _getOrderData = (params) => {
    return api.get('', {
        method: 'app.SpaceOrders.get',
        type: params.type,
        page: params.page,
        limit: params.limit,
        gender: params.gender,
        yh_channel: params.yh_channel,
        uid: params.uid
    }, {code: 200});
};

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

    result.useLimitCode = order.use_limit_code === 'Y';
    if (order.is_cancel === 'Y') {
        return Object.assign(result, {
            canceled: true
        });
    }

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

                /* 预售商品,不能进行任何操作 */
                Object.assign(result, {
                    isAdvance: true
                });
                break;
            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.virtual_type && parseInt(order.virtual_type, 10) === 3) {
                    Object.assign(result, {
                        qrcode: helpers.urlFormat(`/home/QRcode/${order.order_code}`)
                    });
                } else {
                    Object.assign(result, _assignExpressInfo(showLogistics, order));
                }
                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 {
                    Object.assign(result, _assignExpressInfo(showLogistics, order));
                }
                break;
            default:
                break;
        }
    } else {

        /* 订单为货到付款订单时,计算订单状态。(货到付款没有待付款状态) */
        switch (order.status) {
            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
                });
                Object.assign(result, _assignExpressInfo(showLogistics, order));
                break;
            case 6:

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

    return result;
};

/**
 * 获取页面顶部 tab 数据
 * @param type
 * @returns {Array}
 * @private
 */
const _getNavs = (type) => {
    let navType = {
        1: '全部',
        2: '待付款',
        3: '待发货',
        4: '待收货'
    };
    let nav = [];

    _.forEach(navType, (obj, key) => {
        nav.push({
            name: obj,
            typeId: key,
            active: parseInt(key, 10) === parseInt(type, 10),
            url: helpers.urlFormat('/home/orders', {type: key})
        });
    });

    return nav;
};

/**
 * 订单页面数据
 * @param params
 * @returns {*|Promise.<TResult>}
 */
const order = (params) => {
    let finalResult = {};

    Object.assign(finalResult, {navs: _getNavs(params.type)});

    return api.get('', _.assign({
        method: 'app.SpaceOrders.closeReasons'
    }, params), {code: 200}).then((result) => {

        if (result.data) {
            Object.assign(finalResult, {cancelReason: result.data});
        }

        return finalResult;
    });
};

/**
 * 获取订单列表
 * @param params
 */
const getOrders = (params) => {
    let finalResult = [];

    return _getOrderData(params).then(result => {
        if (result.data && result.data.page_total && params.page <= result.data.page_total && result.data.order_list) {
            let count = 0;

            _.forEach(result.data.order_list, value => {

                /* 订单件数清零 */
                count = 0;

                let perOrder = _getOrderStatus(value);

                /* 是否是虚拟商品 */
                let isTickets = order.virtual_type && parseInt(order.virtual_type, 10) === 3;

                Object.assign(perOrder, {
                    orderNum: value.order_code,
                    orderStatus: value.status_str,
                    sumCost: value.amount,
                    goods: _formatOrderGoods(value.order_goods, count, false, isTickets),
                    detailUrl: helpers.urlFormat('/home/orderdetail', {order_code: value.order_code}),
                    count: value.buy_total,
                    isVirtual: isTickets,
                    isDepositAdvance: value.attribute * 1 === 9// 定金预售
                });

                /* 如果运费大于0,会显示运费 */
                if (parseFloat(value.shipping_cost) > 0) {
                    Object.assign(perOrder, {
                        shippingCost: value.shipping_cost
                    });
                }

                /* 倒计时时间 */
                if (value.counter_flag && value.counter_flag === 'Y') {
                    Object.assign(perOrder, {
                        leftTime: parseInt(value.pay_lefttime, 10) * 1000
                    });
                }

                finalResult.push(perOrder);
            });
        }

        return finalResult;
    });
};

module.exports = {
    order,
    getOrders
};