Blame view

apps/home/models/orderDetail.js 12.9 KB
lijing authored
1 2 3 4
'use strict';


const api = global.yoho.API;
郭成尧 authored
5
const serviceApi = global.yoho.ServiceAPI;
zhangxiaoru authored
6
zhangxiaoru authored
7
const _ = require('lodash');
zhangxiaoru authored
8 9

// const config = global.yoho.config;
zhangxiaoru authored
10 11
const helpers = global.yoho.helpers;
const camelCase = global.yoho.camelCase;
zhangxiaoru authored
12
const logger = global.yoho.logger;
zhangxiaoru authored
13
郭成尧 authored
14 15
const CODE_LOGISTIC_BANNER = '1fc9b2484fcd559049f2f7e0db313f20'; // 物流详情banner资源码
zhangxiaoru authored
16 17 18
const closeReasons = () => {
    return api.get('', {
        method: 'app.SpaceOrders.closeReasons'
ccbikai(👎🏻🍜) authored
19
    }, {
20
        cache: true
zhangxiaoru authored
21
    }).then((result) => {
zhangxiaoru authored
22 23
        if (result && result.code === 200) {
            return result.data;
zhangxiaoru authored
24 25 26
        } else {
            return {};
        }
zhangxiaoru authored
27 28 29 30 31 32 33
    });
};

/**
 * 获取快递有关信息
 * @private
 */
34 35 36
const _assignExpressInfo = (showLogistics, order) => {
    let data = {};
37
    if (showLogistics && order.expressCompany && order.expressCompany.caption) {
38 39 40
        data = {
            logisticsUrl: helpers.urlFormat('/home/logistic', {order_code: order.orderCode}),
            logisticsCompany: order.expressCompany.caption,
41
            logisticsNum: order.expressNumber || ''
42
        };
zhangxiaoru authored
43
    }
44 45

    return data;
zhangxiaoru authored
46
};
zhangxiaoru authored
47 48 49 50 51 52 53

/**
 * 获取订单状态
 * @private
 */
const _getOrderStatus = (order, showLogistics) => {
    let result = {};
zhangxiaoru authored
54
zhangxiaoru authored
55 56 57 58 59 60
    if (order.isCancel === 'Y') {
        return {
            canceled: true
        };
    }
61 62
    order.status = parseInt(order.status, 10);
zhangxiaoru authored
63 64 65
    // 先判断订单付款方式,根据不同的付款方式计算订单状态。(注:货到付款没有待付款状态)
    // 支付方式为非货到付款时,计算订单状态。
    if (parseInt(order.paymentType, 10) !== 2) {
zhangxiaoru authored
66 67 68 69
        Object.assign(result, {
            isPayonline: true
        });
70
        switch (order.status) {
zhangxiaoru authored
71 72 73 74 75
            case 0:

                /* 待付款 */
                Object.assign(result, {
                    unpaid: true,
76
                    payUrl: helpers.urlFormat('/home/orders/paynew', {order_code: order.orderCode})
zhangxiaoru authored
77 78 79 80 81 82 83 84
                });
                break;
            case 1:
            case 2:
            case 3:

                /* 已付款状态不给查看物流 URL */
                Object.assign(result, {
85 86
                    unreceived: true,
                    payAly: true
zhangxiaoru authored
87 88 89 90 91 92 93
                });
                break;
            case 4:
            case 5:

                /* 已发货状态,给查看物流或二维码URL */
                Object.assign(result, {
94 95
                    unreceived: true,
                    payAly: true
zhangxiaoru authored
96 97 98 99 100
                });

                /* 是否门票 */
                if (order.virtualType && parseInt(order.virtualType, 10) === 3) {
                    Object.assign(result, {
101
                        qrcode: helpers.urlFormat(`/home/QRcode/${order.orderCode}`)
zhangxiaoru authored
102 103
                    });
                } else {
104
                    Object.assign(result, _assignExpressInfo(showLogistics, order));
zhangxiaoru authored
105 106 107 108 109 110
                }
                break;
            case 6:

                /* 已成功订单,给查看物流或二维码URL */
                Object.assign(result, {
111 112
                    completed: true,
                    payAly: true
zhangxiaoru authored
113 114 115 116 117
                });

                /* 是否门票 */
                if (order.virtual_type && parseInt(order.virtual_type, 10) === 3) {
                    Object.assign(result, {
118
                        qrcode: helpers.urlFormat(`/home/QRcode/${order.orderCode}`)
zhangxiaoru authored
119 120
                    });
                } else {
121
                    Object.assign(result, _assignExpressInfo(showLogistics, order));
zhangxiaoru authored
122 123 124 125 126 127
                }
                break;
            default:
                break;
        }
    } else {
128 129 130 131
        Object.assign(result, {
            payAly: true,
            paymentName: (order.paymentName !== '') ? order.paymentName : '货到付款'
        });
zhangxiaoru authored
132 133

        /* 订单为货到付款订单时,计算订单状态。(货到付款没有待付款状态) */
134
        switch (order.status) {
zhangxiaoru authored
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
            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
                });
158
                Object.assign(result, _assignExpressInfo(showLogistics, order));
zhangxiaoru authored
159 160 161 162 163 164 165
                break;
            case 6:

                /* 待收货状态,给查看物流 url */
                Object.assign(result, {
                    completed: true
                });
166
                Object.assign(result, _assignExpressInfo(showLogistics, order));
zhangxiaoru authored
167 168 169 170 171 172 173 174 175
                break;
            default:
                break;
        }
    }

    return result;
};
lijing authored
176
zhangxiaoru authored
177
const orderDetailData = (uid, orderCode) => {
lijing authored
178
zhangxiaoru authored
179 180 181 182 183
    return api.get('', {
        method: 'app.SpaceOrders.detail',
        uid: uid,
        order_code: orderCode
    }).then((result) => {
zhangxiaoru authored
184
zhangxiaoru authored
185
        if (result && result.code === 200) {
zhangxiaoru authored
186
            let orderDetail = camelCase(result.data);
zhangxiaoru authored
187
            let goods = [];
188
            let status = _getOrderStatus(orderDetail, true);
zhangxiaoru authored
189
zhangxiaoru authored
190
            orderDetail = _.assign(orderDetail, status);
zhangxiaoru authored
191
zhangxiaoru authored
192
            if (orderDetail.virtualType && orderDetail.virtualType === 3) {
zhangxiaoru authored
193 194 195
                orderDetail = _.assign(orderDetail, {
                    isVirtual: true,
                    mobile: result.data.mobile
zhangxiaoru authored
196
                });
zhangxiaoru authored
197 198 199 200
            }

            orderDetail = _.assign(orderDetail, {
                addressAll: orderDetail.area + orderDetail.address
zhangxiaoru authored
201
zhangxiaoru authored
202
                // createTime: date('Y-m-d H:i:s', orderDetail.createTime)
zhangxiaoru authored
203
            });
zhangxiaoru authored
204
zhangxiaoru authored
205
            if (orderDetail.counterFlag && orderDetail.counterFlag === 'Y') {
zhangxiaoru authored
206 207
                orderDetail = _.assign(orderDetail, {
                    leftTime: parseInt(orderDetail.payLefttime, 10) * 1000
zhangxiaoru authored
208
                });
zhangxiaoru authored
209 210 211 212 213 214 215 216 217
            }

            _.forEach(orderDetail.orderGoods, function(data) {
                let obj = {};
                let count = +data.buyNumber;

                obj = _.assign(obj, {
                    thumb: data.goodsImage,
                    name: data.productName,
郝肖肖 authored
218
                    color: data.factoryColorName ? data.factoryColorName : data.colorName,
zhangxiaoru authored
219 220 221
                    size: data.sizeName,
                    price: data.goodsPrice,
                    count: count
zhangxiaoru authored
222
                });
zhangxiaoru authored
223
zhangxiaoru authored
224
                if (data.goodsType === 'gift') {
zhangxiaoru authored
225 226
                    obj = _.assign(obj, {
                        gift: true
zhangxiaoru authored
227 228
                    });
                } else if (data.goodsType === 'priceGift') {
zhangxiaoru authored
229 230
                    obj = _.assign(obj, {
                        advanceBuy: true
zhangxiaoru authored
231
                    });
zhangxiaoru authored
232
                }
zhangxiaoru authored
233
zhangxiaoru authored
234
                if (data.expectArrivalTime) {
zhangxiaoru authored
235 236
                    obj = _.assign(obj, {
                        appearDate: data.expectArrivalTime
zhangxiaoru authored
237
                    });
zhangxiaoru authored
238
                }
zhangxiaoru authored
239
zhangxiaoru authored
240
                if (data.productSkn) {
zhangxiaoru authored
241 242
                    obj = _.assign(obj, {
                        link: '/product/show_' + data.productSkn + '.html'
zhangxiaoru authored
243
                    });
zhangxiaoru authored
244
                }
zhangxiaoru authored
245
zhangxiaoru authored
246 247 248 249
                goods.push(obj);

                orderDetail = _.assign(orderDetail, {
                    orderCount: count
zhangxiaoru authored
250
                });
zhangxiaoru authored
251 252 253 254 255

            });

            orderDetail.goods = goods;
zhangxiaoru authored
256
            if (orderDetail.paymentStatus === 'Y') {
zhangxiaoru authored
257
                orderDetail = _.assign(orderDetail, {
zhangxiaoru authored
258 259
                    isPay: true
                });
zhangxiaoru authored
260 261
            }
zhangxiaoru authored
262 263
            // 判断是否可以修改地址
            if (orderDetail.canUpdateDeliveryAddress === 'Y') {
zhangxiaoru authored
264
                orderDetail = _.assign(orderDetail, {
zhangxiaoru authored
265 266
                    changeable: true
                });
zhangxiaoru authored
267 268
            }
zhangxiaoru authored
269 270
            // 判断是否有关联订单
            if (orderDetail.relateOrderCode === 'Y') {
zhangxiaoru authored
271
                orderDetail = _.assign(orderDetail, {
zhangxiaoru authored
272 273 274
                    relation: true
                });
            } else {
zhangxiaoru authored
275
                orderDetail = _.assign(orderDetail, {
zhangxiaoru authored
276 277
                    relation: false
                });
zhangxiaoru authored
278 279
            }
郝肖肖 authored
280 281 282 283 284 285 286
            // 定金预售
            if (orderDetail.attribute * 1 === 9) {
                orderDetail = _.assign(orderDetail, {
                    isDepositAdvance: true
                });
            }
zhangxiaoru authored
287 288 289
            orderDetail = _.assign(orderDetail, {
                goodsAmount: orderDetail.paymentAmount,
                url: '/home/addressModify?orderCode=' + orderCode + '&relation=' + orderDetail.relation
zhangxiaoru authored
290
            });
zhangxiaoru authored
291
zhangxiaoru authored
292 293
             // 为支付的拆单配送信息
            if (orderDetail.isMultiPackage && orderDetail.isMultiPackage === 'Y') {
郭成尧 authored
294 295 296 297 298 299 300
                let jitInfo = {
                    deliveryId: orderDetail.deliveryId,
                    paymentType: orderDetail.paymentType,
                    couponCode: orderDetail.couponCode,
                    yohoCoin: orderDetail.yohoCoin,
                    cartType: 'ordinary'
                };
301
郭成尧 authored
302 303 304
                orderDetail = _.assign(orderDetail, {
                    isJit: true,
                    jitDetailUrl: helpers.urlFormat('/cart/index/new/jitDetail', jitInfo)
zhangxiaoru authored
305
                });
zhangxiaoru authored
306 307
            }
308
            if (orderDetail.invoice) {
309
                orderDetail.invoice.type = (orderDetail.invoice.type + '' === '2');
310 311
            }
zhangxiaoru authored
312
            // 取消订单原因列表
zhangxiaoru authored
313
            let resons = closeReasons();
zhangxiaoru authored
314 315 316

            return closeReasons().then(list => {
                resons = list;
zhangxiaoru authored
317 318

                orderDetail = _.assign(orderDetail, {
zhangxiaoru authored
319 320
                    cancelReason: resons
                });
zhangxiaoru authored
321 322 323 324

                return orderDetail;
            });
        } else {
郭成尧 authored
325
            logger.error('detail info return no 200');
zhangxiaoru authored
326 327
            return {};
        }
zhangxiaoru authored
328
lijing authored
329 330 331
    });
};
zhangxiaoru authored
332
// 删除订单
zhangxiaoru authored
333 334 335 336 337 338
const delOrder = (orderCode, uid) => {
    return api.get('', {
        method: 'app.SpaceOrders.delOrderByCode',
        uid: uid,
        order_code: orderCode
    });
zhangxiaoru authored
339
};
zhangxiaoru authored
340
zhangxiaoru authored
341
// 再次购买
zhangxiaoru authored
342 343 344 345 346
const readdData = (orderCode, uid) => {
    return api.get('', {
        method: 'app.Shopping.readd',
        uid: uid,
        order_code: orderCode
zhangxiaoru authored
347
    }).then((result) => {
zhangxiaoru authored
348 349 350 351
        if (result && result.code === 200) {
            result.message = '商品已重新加入购物车';
        } else if (result.code === '400') {
            result.message = '缺失参数';
zhangxiaoru authored
352 353 354 355 356 357
        } else {
            result.message = '商品加入购物车失败';
            result.datat = {};
        }

        return result;
zhangxiaoru authored
358
    });
zhangxiaoru authored
359
};
zhangxiaoru authored
360
zhangxiaoru authored
361
// 取消订单
zhangxiaoru authored
362 363 364
const cancelOrder = (orderCode, uid, reasonId, gender, channel, reason) => {

zhangxiaoru authored
365 366 367 368
    return api.get('', {
        method: 'app.SpaceOrders.close',
        uid: uid,
        order_code: orderCode,
zhangxiaoru authored
369 370 371 372
        reasonId: reasonId,
        reasons: reason,
        gender: gender,
        yh_channel: channel
zhangxiaoru authored
373
    });
zhangxiaoru authored
374
};
zhangxiaoru authored
375
郭成尧 authored
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
/*
* 我的订单-查看物流
* @param int $orderCode 订单号
* @param int $uid 用户ID
* @return array
*/
const _logisticsData = (orderCode, uid) => {
    return api.get('', {
        method: 'app.express.li',
        order_code: orderCode,
        uid: uid
    });
};

/**
 * 获取物流详情页banner
 */
const _getLogisterBanner = () => {
    return serviceApi.get('operations/api/v5/resource/get', {
        content_code: CODE_LOGISTIC_BANNER
    }, {code: 200});
};

/**
 * 查看物流
 *
 * @param int orderCode 订单编号
 * @param int uid 用户ID
 * @return array
 */
const logistics = (orderCode, uid) => {
    return Promise.all([
        _getLogisterBanner(),
        _logisticsData(orderCode, uid),
    ]).then(result => {
        let finalResult = {
            banner: []
        };
        let banners = result[0];
        let logistic = result[1];

        // 获取物流详情页banner
        if (banners && banners.data) {
            _.forEach(banners.data, value => {
                _.forEach(value.data, subValue => {
                    finalResult.banner.push({
                        url: subValue.url,
                        img: subValue.src
                    });
                });
            });
        }

        if (logistic && logistic.data) {
            finalResult.logisticUrl = _.get(logistic, 'data.url', '');
            finalResult.logisticImg = _.get(logistic, 'data.logo', '');
            finalResult.logisticCompany = _.get(logistic, 'data.caption', '');
            finalResult.logisticNumber = _.get(logistic, 'data.express_number', '');
            finalResult.logisticDetail = [];

            _.forEach(_.get(logistic, 'data.express_detail', []), value => {
                finalResult.logisticDetail.push({
                    status: value.accept_address,
                    date: value.acceptTime,
                });
            });

        }

        return finalResult;
    });
};
lijing authored
449
module.exports = {
zhangxiaoru authored
450
    orderDetailData,
zhangxiaoru authored
451 452 453
    closeReasons,
    delOrder,
    readdData,
郭成尧 authored
454 455
    cancelOrder,
    logistics
lijing authored
456
};