Blame view

apps/home/models/order.js 9.33 KB
lijing authored
1 2 3 4 5 6
'use strict';

const api = global.yoho.API;
const helpers = global.yoho.helpers;
const _ = require('lodash');
郭成尧 authored
7
/**
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 * 门票
 * @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,
27
            color: value.factory_color_name ? value.factory_color_name : value.color_name,
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
            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;
};

/**
郭成尧 authored
80 81 82
 * 获取快递有关信息
 * @private
 */
83
const _assignExpressInfo = (showLogistics, order) => {
ccbikai(👎🏻🍜) authored
84
    let data = {};
85 86 87

    if (showLogistics && order.express_company && order.express_company.caption) {
        data = {
郭成尧 authored
88 89
            logisticsUrl: helpers.urlFormat('/home/logistic', {order_code: order.order_code}),
            logisticsCompany: order.express_company.caption,
90 91
            logisticsNum: order.express_number || ''
        };
郭成尧 authored
92
    }
93 94

    return data;
郭成尧 authored
95
};
lijing authored
96
郭成尧 authored
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
/**
 * 调用接口获取订单数据
 * @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 = {};
lijing authored
121
陈轩 authored
122
    result.useLimitCode = order.use_limit_code === 'Y';
郭成尧 authored
123
    if (order.is_cancel === 'Y') {
陈轩 authored
124
        return Object.assign(result, {
郭成尧 authored
125
            canceled: true
陈轩 authored
126
        });
郭成尧 authored
127 128 129 130 131 132
    }

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

                /* 预售商品,不能进行任何操作 */
                Object.assign(result, {
137
                    isAdvance: true
138 139
                });
                break;
郭成尧 authored
140 141 142 143 144
            case 0:

                /* 待付款 */
                Object.assign(result, {
                    unpaid: true,
145
                    payUrl: helpers.urlFormat('/home/orders/paynew', {order_code: order.order_code})
郭成尧 authored
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
                });
                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 {
171
                    Object.assign(result, _assignExpressInfo(showLogistics, order));
郭成尧 authored
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
                }
                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 {
187
                    Object.assign(result, _assignExpressInfo(showLogistics, order));
郭成尧 authored
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
                }
                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
                });
220
                Object.assign(result, _assignExpressInfo(showLogistics, order));
郭成尧 authored
221 222 223 224 225 226 227
                break;
            case 6:

                /* 待收货状态,给查看物流 url */
                Object.assign(result, {
                    completed: true
                });
228
                Object.assign(result, _assignExpressInfo(showLogistics, order));
郭成尧 authored
229 230 231 232 233 234 235
                break;
            default:
                break;
        }
    }

    return result;
lijing authored
236 237
};
郭成尧 authored
238 239 240 241 242 243
/**
 * 获取页面顶部 tab 数据
 * @param type
 * @returns {Array}
 * @private
 */
郭成尧 authored
244
const _getNavs = (type) => {
郭成尧 authored
245 246 247 248 249 250
    let navType = {
        1: '全部',
        2: '待付款',
        3: '待发货',
        4: '待收货'
    };
郭成尧 authored
251 252 253 254 255 256
    let nav = [];

    _.forEach(navType, (obj, key) => {
        nav.push({
            name: obj,
            typeId: key,
郭成尧 authored
257
            active: parseInt(key, 10) === parseInt(type, 10),
郭成尧 authored
258 259 260 261 262
            url: helpers.urlFormat('/home/orders', {type: key})
        });
    });

    return nav;
lijing authored
263 264
};
郭成尧 authored
265 266 267 268 269
/**
 * 订单页面数据
 * @param params
 * @returns {*|Promise.<TResult>}
 */
lijing authored
270
const order = (params) => {
郭成尧 authored
271 272 273 274
    let finalResult = {};

    Object.assign(finalResult, {navs: _getNavs(params.type)});
lijing authored
275
    return api.get('', _.assign({
郭成尧 authored
276
        method: 'app.SpaceOrders.closeReasons'
ccbikai(👎🏻🍜) authored
277 278 279 280
    }, params), {
        cache: true,
        code: 200
    }).then((result) => {
郭成尧 authored
281 282 283

        if (result.data) {
            Object.assign(finalResult, {cancelReason: result.data});
lijing authored
284
        }
郭成尧 authored
285 286

        return finalResult;
lijing authored
287 288 289
    });
};
郭成尧 authored
290 291 292 293 294 295 296 297 298
/**
 * 获取订单列表
 * @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) {
299 300
            let count = 0;
郭成尧 authored
301
            _.forEach(result.data.order_list, value => {
302 303 304

                /* 订单件数清零 */
                count = 0;
陈轩 authored
305
郭成尧 authored
306
                let perOrder = _getOrderStatus(value);
307 308 309 310 311 312 313 314 315 316

                /* 是否是虚拟商品 */
                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}),
zzzzzzz authored
317
                    count: value.buy_total,
318 319
                    isVirtual: isTickets,
                    isDepositAdvance: value.attribute * 1 === 9// 定金预售
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
                });

                /* 如果运费大于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
                    });
                }
郭成尧 authored
336 337 338 339
                finalResult.push(perOrder);
            });
        }
340
        return finalResult;
郭成尧 authored
341 342 343
    });
};
lijing authored
344
module.exports = {
郭成尧 authored
345 346
    order,
    getOrders
lijing authored
347
};