order.js 9.94 KB
'use strict';

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

/**
 * 门票
 * @type {{SINGLE_TICKETS_SKN: number}}
 */

/**
 * 格式化价格
 * @private
 */
const transPrice = (price, isSepcialZero) => {
    return (price > 0 || isSepcialZero) ? parseFloat(price).toFixed(2) : 0;
};

module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }

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

        _.forEach(orderGoods, value => {
            let goods = {
                thumb: value.goods_image,
                name: value.product_name,
                color: value.factory_color_name ? value.factory_color_name : value.color_name,
                size: value.size_name,
                payPrice: value.real_pay_price,
                salePrice: transPrice(value.sales_price) || value.goods_price,
                count: value.buy_number

                // isVipPrice: value.discount_tag === 'V',
                // isStudebt: value.discount_tag === 'S'
            };

            if (parseInt(value.real_pay_price, 10) === parseInt(value.sales_price, 10)) {
                goods.salePrice = false;
            }

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

            /* 上市期 */
            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/' + value.product_skn + '.html') // 商品url改版
                });
            }

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

            /* 门票 */
            if (value.goods_type === 'ticket') {
                Object.assign(goods, {
                    tickets: true
                });
            }

            result.push(goods);
        });

        return result;
    }

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

    /**
     * 获取订单状态
     * @private
     */
    _getOrderStatus(order) {
        let orderBtn = [];

        if (!order) {
            return {};
        }

        let attribute = _.parseInt(order.attribute);

        if (attribute === 9 || attribute === 11) {
            /* 预售商品,不能进行任何操作 */
            orderBtn.push({
                isAdvance: true
            });

            return {orderBtn: orderBtn};
        }

        /* 倒计时时间 */
        if (order.counter_flag === 'Y' && parseInt(order.pay_lefttime, 10)) {
            orderBtn.push({
                leftTime: order.pay_lefttime * 1000
            });
        }

        _.each(order.links, function(val) {
            switch (val) {
                case 'refundApply':// 申请退款
                    orderBtn.push({
                        refundApply: true
                    });
                    break;
                case 'modifyAddress':// 修改收货地址
                    orderBtn.push({
                        modifyAddress: true,
                        link: helpers.urlFormat('/home/orders/addressModify', {
                            relation: order.relate_order_code === 'Y', // 判断是否有关联订单
                            orderCode: order.order_code,
                            areaCode: _.get(order, 'delivery_address.area_code', ''),
                            provinceAreaCode: order.is_support_change_province !== 'Y' &&
                                _.get(order, 'delivery_address.province_area_code', '') || ''// 如果不允许修改省
                        })
                    });
                    break;
                case 'buyNow':// 立即付款
                    orderBtn.push({
                        buyNow: true,
                        link: helpers.urlFormat('/home/orders/paynew', {order_code: order.order_code}),
                    });
                    break;
                case 'closeOrder':// 取消订单
                    orderBtn.push({
                        closeOrder: true
                    });
                    break;
                case 'confirm':// 订单确认
                    orderBtn.push({
                        confirm: true
                    });
                    break;
                case 'getExpress':// 查看物流
                    orderBtn.push({
                        getExpress: true,
                        logisticsUrl: helpers.urlFormat('/home/logistic', {order_code: order.order_code}),
                    });
                    break;
                case 'shareOrder':// 晒单评价
                    break;
                case 'delOrder':// 删除订单
                    orderBtn.push({
                        delOrder: true,
                    });
                    break;
                case 'lookQrcode':// 查看二维码
                    orderBtn.push({
                        lookQrcode: true,
                        link: helpers.urlFormat(`/home/QRcode/${order.order_code}`),
                    });
                    break;
                case 'afterService':// 售后服务
                    orderBtn.push({
                        afterService: true
                    });
                    break;
                case 'readd':// 再次购买
                    orderBtn.push({
                        readd: true
                    });
                    break;
                default:
                    break;
            }
        });

        return {orderBtn: orderBtn};
    }

    /**
     * 获取页面顶部 tab 数据
     * @param type
     * @returns {Array}
     * @private
     */
    _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>}
     */
    order(params) {
        let finalResult = {
            navs: this._getNavs(params.type)
        };

        return this.get({
            data: _.assign({
                method: 'app.SpaceOrders.closeReasons'
            }, params),
            param: {cache: true, code: 200}
        }).then(result => {

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

            return finalResult;
        });
    }

    /**
     * 申请退款原因
     */
    refundReason() {
        return this.get({
            data: {method: 'app.SpaceOrders.refundApplyReasons'},
            param: {code: 200, cache: true}
        }).then(result => {
            return _.get(result, 'data', []);
        });
    }

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

        return this._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 = this._getOrderStatus(value);


                    Object.assign(perOrder, {
                        orderNum: value.order_code,
                        orderStatus: value.status_str,
                        sumCost: value.amount,
                        goods: this._formatOrderGoods(value.order_goods, count, false),
                        detailUrl: helpers.urlFormat('/home/orderdetail', {order_code: value.order_code}),
                        count: value.buy_total,
                        isVirtual: _.get(value, 'order_goods[0].goods_type'),
                        orderTitle: value.order_title
                    });

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

                    finalResult.push(perOrder);
                });
            }

            return finalResult;
        });
    }

    /**
     * 确认收货确认订单接口
     */
    sure(params) {
        return this.get({
            data: {
                method: 'app.SpaceOrders.confirm',
                order_code: params.order_code,
                uid: params.uid
            }
        });
    }
};