Blame view

apps/home/models/orderDetail.js 17.8 KB
lijing authored
1 2
'use strict';
zhangxiaoru authored
3
const _ = require('lodash');
zhangxiaoru authored
4 5

// const config = global.yoho.config;
zhangxiaoru authored
6 7
const helpers = global.yoho.helpers;
const camelCase = global.yoho.camelCase;
zhangxiaoru authored
8
const logger = global.yoho.logger;
郭成尧 authored
9
const stringProcess = require(`${global.utils}/string-process`);
zhangxiaoru authored
10
郭成尧 authored
11 12
const CODE_LOGISTIC_BANNER = '1fc9b2484fcd559049f2f7e0db313f20'; // 物流详情banner资源码
zhangxiaoru authored
13 14 15
module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
zhangxiaoru authored
16 17
    }
zhangxiaoru authored
18
    closeReasons() {
zhangxiaoru authored
19 20 21
        return this.get({
            data: {method: 'app.SpaceOrders.closeReasons'},
            param: {cache: true}
zhangxiaoru authored
22 23 24 25 26 27
        }).then((result) => {
            if (result && result.code === 200) {
                return result.data;
            } else {
                return {};
            }
zhangxiaoru authored
28
        });
郝肖肖 authored
29
    }
zhangxiaoru authored
30
zhangxiaoru authored
31 32 33 34 35 36 37
    /**
     * 获取快递有关信息
     * @private
     */
    _assignExpressInfo(showLogistics, order) {
        let data = {};
李靖 authored
38 39 40
        data = {
            logisticsUrl: helpers.urlFormat('/home/logistic', {order_code: order.orderCode})
        };
zhangxiaoru authored
41 42

        return data;
郝肖肖 authored
43
    }
zhangxiaoru authored
44
zhangxiaoru authored
45 46 47 48 49 50 51 52 53
    /**
     * 获取订单状态
     * @private
     */
    _getOrderStatus(order, showLogistics) {
        let orderBtn = [];

        if (!order) {
            return {};
zhangxiaoru authored
54 55
        }
李靖 authored
56
        let attribute = _.parseInt(_.get(order, 'orderExtInfo.attribute'));
57
李靖 authored
58 59 60 61
        order = _.assign(order, {
            orderCode: _.get(order, 'orderExtInfo.orderCode')
        });
62
        if (attribute === 9 || attribute === 11) {
李靖 authored
63 64 65 66 67 68 69 70 71 72 73 74
            // 定金预售拼接提示语
            let countTime = 0;
            let payAmount = 0;

            if (_.get(order, 'orderDetailInfo.ext.payLefttime') &&
            parseInt(order.orderDetailInfo.ext.payLefttime, 10)) {
                countTime = order.orderDetailInfo.ext.payLefttime * 1000;
            }

            if (_.get(order, 'orderDetailInfo.key') === '203') { // 待支付尾款
                payAmount = _.get(order, 'orderExtInfo.orderTailPayAmount');
                order.orderDetailInfo.ext.desc = `你的商品已到货,您还需支付尾款<span>¥${payAmount}</span>
李靖 authored
75
                请在<i class="hours new-hours hide">${countTime}</i>内完成支付,超时订单自动取消`;
李靖 authored
76 77 78
            } else if (_.get(order, 'orderDetailInfo.key') === '201') { // 待支付定金
                payAmount = _.get(order, 'orderExtInfo.orderDepositAmount');
                order.orderDetailInfo.ext.desc = `您需支付定金<span>¥${payAmount}</span>
李靖 authored
79
                请在<i class="hours new-hours hide">${countTime}</i>内完成支付,超时订单自动取消`;
李靖 authored
80 81
            }
zhangxiaoru authored
82 83 84 85
            /* 预售商品,不能进行任何操作 */
            orderBtn.push({
                isAdvance: true
            });
郭成尧 authored
86
            return { orderBtn: orderBtn };
zhangxiaoru authored
87
        }
zhangxiaoru authored
88
zhangxiaoru authored
89
        /* 倒计时时间 订单详情页倒计时不在同一级*/
李靖 authored
90
        if (_.get(order, 'orderDetailInfo.ext.payLefttime') && parseInt(order.orderDetailInfo.ext.payLefttime, 10)) {
李靖 authored
91 92 93 94
            let countTime = _.get(order, 'orderDetailInfo.ext.payLefttime') * 1000;

            order.orderDetailInfo.ext.desc = `您的订单已提交,
            请在<i class="hours new-hours hide">${countTime}</i>内完成支付,超时订单自动取消`;
zhangxiaoru authored
95
        }
lijing authored
96
zhangxiaoru authored
97
        _.each(order.links, (val) => {
zhangxiaoru authored
98 99 100 101 102 103 104 105 106 107 108 109
            switch (val) {
                case 'refundApply':// 申请退款
                    orderBtn.push({
                        refundApply: true
                    });
                    break;
                case 'modifyAddress':// 修改收货地址
                    orderBtn.push({
                        modifyAddress: true,
                        link: helpers.urlFormat('/home/orders/addressModify', {
                            relation: order.relateOrderCode === 'Y', // 判断是否有关联订单
                            orderCode: order.orderCode,
李靖 authored
110 111 112
                            areaCode: _.get(order, 'deliveryInfo.deliveryAddress.areaCode', ''),
                            provinceAreaCode: _.get(order, 'deliveryInfo.isSupportChangeProvince') !== 'Y' &&
                                _.get(order, 'deliveryInfo.deliveryAddress.provinceAreaCode', '') || ''// 如果不允许修改省
zhangxiaoru authored
113 114 115
                        })
                    });
                    break;
116 117 118 119
                case 'groupInvite':// 查看拼团详情
                    orderBtn.push({
                        groupInvite: true,
                        link: helpers.urlFormat('/activity/group/progress', {
120 121
                            groupNo: order.orderExtInfo.groupNo || '',
                            activityId: order.orderExtInfo.activityId || '',
122 123 124
                        })
                    });
                    break;
zhangxiaoru authored
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 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 171 172 173
                case 'buyNow':// 立即付款
                    orderBtn.push({
                        buyNow: true,
                        link: helpers.urlFormat('/home/orders/paynew', {order_code: order.orderCode}),
                    });
                    break;
                case 'closeOrder':// 取消订单
                    orderBtn.push({
                        closeOrder: true
                    });
                    break;
                case 'confirm':// 订单确认
                    orderBtn.push({
                        confirm: true
                    });
                    break;
                case 'getExpress':// 查看物流
                    orderBtn.push(Object.assign(
                        {getExpress: true},
                        this._assignExpressInfo(showLogistics, order)
                    ));
                    break;
                case 'shareOrder':// 晒单评价
                    break;
                case 'delOrder':// 删除订单
                    orderBtn.push({
                        delOrder: true,
                    });
                    break;
                case 'lookQrcode':// 查看二维码
                    orderBtn.push({
                        lookQrcode: true,
                        link: helpers.urlFormat(`/home/QRcode/${order.orderCode}`),
                    });
                    break;
                case 'afterService':// 售后服务
                    orderBtn.push({
                        afterService: true
                    });
                    break;
                case 'readd':// 再次购买
                    orderBtn.push({
                        readd: true
                    });
                    break;
                default:
                    break;
            }
        });
lijing authored
174
zhangxiaoru authored
175
        return {orderBtn: orderBtn};
郭成尧 authored
176 177
    }
zhangxiaoru authored
178
    orderDetailData(uid, orderCode) {
zhangxiaoru authored
179
zhangxiaoru authored
180 181 182
        if (!uid || !orderCode || !stringProcess.isNumeric(orderCode)) {
            return Promise.resolve({});
        }
zhangxiaoru authored
183
郭成尧 authored
184 185
        uid.appVersion = _.get(global, 'yoho.config.appVersion');
zhangxiaoru authored
186 187 188 189 190
        return this.get({
            data: {
                method: 'app.SpaceOrders.detail',
                uid: uid,
                order_code: orderCode
张孝茹 authored
191
            }
zhangxiaoru authored
192 193 194 195 196 197 198 199
        }).then(result => {
            if (result && result.code === 200) {
                let orderDetail = camelCase(result.data);
                let goods = [];
                let status = this._getOrderStatus(orderDetail, true);
                let offlinestore; // 线下店订单
                let deliveryOffline; // 门店取货
李靖 authored
200
                orderDetail = _.assign(orderDetail, status, {orderCode: orderCode});
zhangxiaoru authored
201
李靖 authored
202
                if (parseInt(_.get(orderDetail, 'orderExtInfo.attribute'), 10) === 3) {
zhangxiaoru authored
203 204
                    orderDetail = _.assign(orderDetail, {
                        isVirtual: true,
李靖 authored
205
                        mobile: _.get(orderDetail, 'deliveryInfo.mobile')
zhangxiaoru authored
206 207 208
                    });
                }
李靖 authored
209 210 211 212 213 214
                if (orderDetail.deliveryInfo) {
                    orderDetail = _.assign(orderDetail, {
                        addressAll: orderDetail.deliveryInfo.area + orderDetail.deliveryInfo.address
                    });
                }
李靖 authored
215 216 217
                if (_.get(orderDetail, 'orderDetailInfo.ext.needHref') === 'Y') {
                    orderDetail = _.assign(orderDetail, {
                        expressUrl: helpers.urlFormat('/home/logistic', {order_code: orderCode})
李靖 authored
218 219
                    });
                }
zhangxiaoru authored
220
zhangxiaoru authored
221 222 223
                _.forEach(orderDetail.orderGoods, function(data) {
                    let obj = {};
                    let count = +data.buyNumber;
zhangxiaoru authored
224
zhangxiaoru authored
225
                    obj = _.assign(obj, {
zhangxiaoru authored
226 227 228 229
                        thumb: data.goodsImage,
                        name: data.productName,
                        color: data.factoryColorName ? data.factoryColorName : data.colorName,
                        size: data.sizeName,
王水玲 authored
230
                        skuTitle: data.skuTitle || '尺码',
zhangxiaoru authored
231
                        payPrice: data.realPayPrice,
232
                        salePrice: parseFloat(data.salesPrice).toFixed(2),
zhangxiaoru authored
233 234
                        count: count,
                        tickets: data.goodsType === 'ticket'
zhangxiaoru authored
235
zhangxiaoru authored
236 237
                        // isVipPrice: data.discountTag === 'V',
                        // isStudebt: data.discountTag === 'S'
zhangxiaoru authored
238
                    });
zhangxiaoru authored
239
zhangxiaoru authored
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
                    if (parseInt(data.realPayPrice, 10) === parseInt(data.salesPrice, 10)) {
                        obj.salePrice = false;
                    }

                    if (data.goodsType === 'gift') {
                        obj = _.assign(obj, {
                            gift: true,
                            salePrice: parseFloat(data.salesPrice).toFixed(2),
                            payPrice: data.goodsPrice
                        });
                    } else if (data.goodsType === 'price_gift') {
                        obj = _.assign(obj, {
                            advanceBuy: true,
                            salePrice: parseFloat(data.salesPrice).toFixed(2),
                            payPrice: data.goodsPrice
                        });
                    }

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

                    if (data.productSkn) {
                        obj = _.assign(obj, {
                            link: '/product/' + data.productSkn + '.html' // 商品url改版
                        });
                    }

                    if (data.goodsType === 'ticket') {
                        obj = _.assign(obj, {
                            tickets: true
                        });
                    }

                    goods.push(obj);

                    orderDetail = _.assign(orderDetail, {
                        orderCount: count
zhangxiaoru authored
280
                    });
zhangxiaoru authored
281
zhangxiaoru authored
282 283 284 285 286
                });

                orderDetail.goods = goods;

                // 为支付的拆单配送信息
李靖 authored
287
                if (orderDetail.packageInfo) {
zhangxiaoru authored
288
                    orderDetail = _.assign(orderDetail, {
李靖 authored
289
                        jitDetailUrl: helpers.urlFormat('/cart/index/new/jitDetail', {
290
                            orderCode: orderCode
李靖 authored
291 292
                        }),
                        packageTitle: _.get(orderDetail, 'packageInfo.packageTitle')
293 294 295
                    });
                }
zhangxiaoru authored
296 297
                if (parseInt(orderDetail.paymentType, 10) !== 2) {
                    if (_.get(orderDetail, 'isOfflineshops', 'N') === 'Y') {
zhangxiaoru authored
298
zhangxiaoru authored
299 300 301
                        offlinestore = orderDetail.offlineStore !== '' ? orderDetail.offlineStore : false;
                        deliveryOffline = _.get(orderDetail, 'isDeliveryOffline', 'N') === 'Y' ? true : false;
                    } else {
zhangxiaoru authored
302
zhangxiaoru authored
303 304 305
                        offlinestore = false;
                        deliveryOffline = false;
                    }
zhangxiaoru authored
306
zhangxiaoru authored
307 308 309 310 311 312 313
                    orderDetail = _.assign(orderDetail, {
                        offlinestore: offlinestore,
                        deliveryOffline: deliveryOffline
                    });
                } else {
                    orderDetail.paymentName = orderDetail.paymentName !== '' ? orderDetail.paymentName : '货到付款';
                }
zhangxiaoru authored
314
zhangxiaoru authored
315 316 317
                if (orderDetail.invoice) {
                    orderDetail.invoice.type = (orderDetail.invoice.type + '' === '2');
                }
zhangxiaoru authored
318
zhangxiaoru authored
319 320 321
                if (_.get(result, 'data.isSupportRefund', 'N') === 'Y') {
                    orderDetail.refundUrl = helpers.urlFormat('/home/return/refund', {orderCode: orderCode});
                }
zhangxiaoru authored
322
zhangxiaoru authored
323 324
                if (_.get(result, 'data.isSupportExchange', 'N') === 'Y') {
                    orderDetail.changeUrl = helpers.urlFormat('/home/return/exchange', {orderCode: orderCode});
zhangxiaoru authored
325 326
                }
zhangxiaoru authored
327 328 329
                orderDetail.imUrl = helpers.urlFormat('/service/chatQaList');

                return orderDetail;
zhangxiaoru authored
330
            } else {
zhangxiaoru authored
331 332
                logger.error('detail info return no 200');
                return {};
zhangxiaoru authored
333 334
            }
zhangxiaoru authored
335 336
        });
    }
337
zhangxiaoru authored
338 339
    // 删除订单
    delOrder(orderCode, uid) {
zhangxiaoru authored
340 341 342 343 344
        return this.get({
            data: {
                method: 'app.SpaceOrders.delOrderByCode',
                uid: uid,
                order_code: orderCode
张孝茹 authored
345
            }
zhangxiaoru authored
346 347
        });
    }
348
zhangxiaoru authored
349 350
    // 再次购买
    readdData(orderCode, uid) {
zhangxiaoru authored
351 352 353 354 355
        return this.get({
            data: {
                method: 'app.Shopping.readd',
                uid: uid,
                order_code: orderCode
张孝茹 authored
356
            }
zhangxiaoru authored
357 358 359 360 361 362 363 364
        }).then((result) => {
            if (result && result.code === 200) {
                result.message = '商品已重新加入购物车';
            } else if (result.code === '400') {
                result.message = '缺失参数';
            } else {
                result.message = '商品加入购物车失败';
                result.datat = {};
365 366
            }
zhangxiaoru authored
367 368 369
            return result;
        });
    }
郭成尧 authored
370
zhangxiaoru authored
371 372 373
    // 取消订单
    cancelOrder(orderCode, uid, reasonId, gender, channel, reason) {
zhangxiaoru authored
374 375 376 377 378
        return this.get({
            data: {
                method: 'app.SpaceOrders.close',
                uid: uid,
                order_code: orderCode,
李靖 authored
379 380
                reason_id: reasonId,
                reason: reason,
zhangxiaoru authored
381 382
                gender: gender,
                yh_channel: channel
张孝茹 authored
383
            }
zhangxiaoru authored
384
        });
zhangxiaoru authored
385
    }
zhangxiaoru authored
386
zhangxiaoru authored
387 388 389 390
    /**
     * 申请退款
     */
    refundApply(params) {
zhangxiaoru authored
391 392 393 394 395 396 397
        return this.get({
            data: {
                method: 'app.SpaceOrders.refundApply',
                uid: params.uid,
                order_code: params.orderCode,
                reason_id: params.reasonId,
                reason: params.reason
张孝茹 authored
398
            }
zhangxiaoru authored
399 400
        });
    }
lijing authored
401
zhangxiaoru authored
402 403 404 405 406 407 408 409 410 411 412 413 414
    /*
    * 我的订单-查看物流
    * @param int $orderCode 订单号
    * @param int $uid 用户ID
    * @return array
    */
    _logisticsData(params) {
        let finalParams = {
            order_code: params.order_code,
            uid: params.uid
        };

        finalParams.method = 'app.express.li';
zhangxiaoru authored
415
zhangxiaoru authored
416 417 418 419 420 421 422
        // 退换货物流
        if (params.type === 'refund' || params.type === 'change') {
            _.assign(finalParams, {
                method: 'app.express.getNewExpress',
                id: params.id,
                type: params.type
            });
zhangxiaoru authored
423
        } else {
zhangxiaoru authored
424 425 426
            _.assign(finalParams, {
                method: 'app.express.li'
            });
zhangxiaoru authored
427 428
        }
zhangxiaoru authored
429
        return this.get({
张孝茹 authored
430
            data: finalParams
zhangxiaoru authored
431
        });
zhangxiaoru authored
432
    }
zhangxiaoru authored
433
zhangxiaoru authored
434 435 436 437
    /**
     * 获取物流详情页banner
     */
    _getLogisterBanner() {
zhangxiaoru authored
438 439 440
        return this.get({
            url: 'operations/api/v5/resource/get',
            data: {content_code: CODE_LOGISTIC_BANNER},
张孝茹 authored
441
            params: {code: 200},
张孝茹 authored
442
            api: global.yoho.ServiceAPI
zhangxiaoru authored
443
        });
zhangxiaoru authored
444
    }
zhangxiaoru authored
445
zhangxiaoru authored
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
    /**
     * 查看物流
     *
     * @param int orderCode 订单编号
     * @param int uid 用户ID
     * @return array
     */
    logistics(params) {
        return Promise.all([
            this._getLogisterBanner(),
            this._logisticsData(params),
        ]).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
                        });
                    });
                });
            }
郭成尧 authored
475
zhangxiaoru authored
476 477 478 479 480 481 482 483 484 485 486 487 488
            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,
                    });
                });
郭成尧 authored
489
zhangxiaoru authored
490
            }
郭成尧 authored
491
zhangxiaoru authored
492
            return finalResult;
郭成尧 authored
493 494 495
        });
    }
zhangxiaoru authored
496 497 498 499 500 501 502 503 504 505 506 507 508 509
    /**
     * 修改订单地址
     * @param {参数} params
     */
    changeAddress(params) {
        let apiParams = {
            method: 'app.SpaceOrders.updateDeliveryAddress',
            order_code: params.orderCode,
            uid: params.uid,
            user_name: params.username,
            area_code: params.areaCode,
            address: params.address,
            mobile: params.mobile,
            udid: params.udid
郭成尧 authored
510
        };
zhangxiaoru authored
511 512 513 514

        if (params.addressId) {
            Object.assign(apiParams, {
                address_id: params.addressId
郭成尧 authored
515 516 517
            });
        }
zhangxiaoru authored
518 519 520
        if (params.phone) {
            Object.assign(apiParams, {
                phone: params.phone
郭成尧 authored
521 522 523
            });
        }
毕凯 authored
524
        return this.get({
张孝茹 authored
525
            data: apiParams
zhangxiaoru authored
526
        });
郭成尧 authored
527
    }
lijing authored
528
};