orders-service.js
24.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
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
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
'use strict';
const Promise = require('bluebird');
const co = Promise.coroutine;
const _ = require('lodash');
const moment = require('moment');
const helpers = global.yoho.helpers;
const pager = require('./pager').handlePagerData;
const orderApi = require('./orders-api');
const ChannelConfig = require('./channel-config');
const ORDER_TYPE = {
all: 1, // 全部
waitingForPay: 2, // 待付款
waitingForSend: 3, // 待发货
waitingForReceive: 4, // 待收货
completed: 5, // 已完成
canceled: 7 // 已取取消
};
const ORDER_EMPTY_DESC = {
1: '您还没有任何订单',
5: '您目前还没有成功的订单',
7: '您还没有任何取消的订单'
};
const TABS = [
{type: 1, name: '现有订单'},
{type: 5, name: '成功订单'},
{type: 7, name: '已取消订单'}
];
const ORDER_OP_ALL = [
{
type: 'closeOrder',
name: '取消订单',
cancelOrder: true,
hrefFun: () => 'javascript:void(0)' // eslint-disable-line
},
{
type: 'buyNow',
name: '立即付款',
payNow: true,
hrefFun: it => helpers.urlFormat('/home/orders/detail', {orderCode: it})
},
{
type: 'getExpress',
name: '查看物流',
express: true,
hrefFun: () => 'javascript:void(0)' // eslint-disable-line
},
{
type: 'confirm',
name: '确认订单',
confirmReceived: true,
hrefFun: () => 'javascript:void(0)' // eslint-disable-line
},
{
type: 'delOrder',
name: '删除订单',
delOrder: true,
hrefFun: () => ''
},
{
type: 'lookQrcode',
name: '查看二维码',
qrcode: true,
hrefFun: it => helpers.urlFormat('/home/orders/detail', {orderCode: it})
},
{
type: 'afterService',
name: '售后服务',
afterService: true,
hrefFun: () => ''
},
{
type: 'exchange',
name: '申请换货',
optDis: true,
hrefFun: it => helpers.urlFormat('/home/returns/exchangeRequest', {orderCode: it})
},
{
type: 'refund',
name: '申请退货',
optDis: true,
hrefFun: it => helpers.urlFormat('/home/returns/refundrequest', {orderCode: it})
},
{
type: 'shareOrder',
name: '晒单评价',
comment: true,
hrefFun: it => helpers.urlFormat('/home/comment/order', {orderId: it})
},
{
type: 'readd',
name: '再次购买',
reBuy: true,
hrefFun: () => ''
},
{
type: 'showDetail',
name: '查看订单',
showDetail: true,
hrefFun: it => helpers.urlFormat('/home/orders/detail', {orderCode: it})
},
{
type: 'refundApply',
name: '申请退款',
refund: true,
hrefFun: () => 'javascript:void(0)' // eslint-disable-line
},
{
type: 'deposit',
name: '定金预售商品只能在APP端操作',
deposit: true,
hrefFun: () => ''
}
];
const _getTabs = (type) => {
type = type || 1;
return TABS.map((tab) => {
tab.active = tab.type === type;
tab.url = helpers.urlFormat('/home/orders', {page: 1, type: tab.type});
return tab;
});
};
const _getOrderStatus = (isCancel, status, payType, payStatus, statusStr) => {
// 初始化:未取消,待付款
let ret = {
cancel: false,
keyName: 'noPay'
};
if (isCancel === 'Y') {
ret = {cancel: true, statusStr: '已取消'};
} else {
switch (status) {
case 0:
// '订单已成功,等待付款'
if (payType !== 2 && payStatus === 'N') {
ret.keyName = 'noPay';
} else if (payType !== 2 && payStatus === 'Y') {
// '订单已付款,等待备货中'
ret.keyName = 'paid';
} else if (payType === 2 && payStatus === 'N') {
// '订单已成功,等待备货中'-货到付款
ret.keyName = 'complete';
}
break;
case 1:
case 2:
case 3:
// '订单已付款,等待备货中'
ret.keyName = 'paid';
break;
case 4:
case 5:
// '订单已发货'
ret.keyName = 'shipped';
break;
case 6:
// '交易完成';
ret.keyName = 'reback';
break;
default:
ret.keyName = 'unknown';
}
}
ret.statusStr = statusStr;
return ret;
};
const _getOperateInfo = (orderCode, operators) => {
return _.flatten(operators.map(it => ORDER_OP_ALL.filter(op => op.type === it).map((op) => {
return Object.assign({}, op, {
href: op.hrefFun(orderCode)
});
})));
};
const _getGoodsTag = (attribute, goodsType) => {
let goodsTagName = '';
switch (goodsType) {
// 赠品
case 'gift':
goodsTagName = 'freebie';
break;
// 加价购
case 'price_gift':
goodsTagName = 'advanceBuy';
break;
// 预售
case 'advance':
goodsTagName = 'preSaleGood';
break;
// outlet
case 'outlet':
goodsTagName = '';
break;
// 免单
case 'free':
goodsTagName = '';
break;
// 电子
case 'ticket':
goodsTagName = '';
break;
default:
break;
}
// 虚拟
if (attribute === 3) {
goodsTagName = 'virtualGood';
}
return goodsTagName;
};
const _getUrlSafe = (url) => {
if (!url) {
return '';
}
const WhiteList = ['/special_', '/special/'];
if (_.some(WhiteList, (val) => _.includes(url, val))) {
return url;
}
return url.replace('http://', '//');
};
const _getExpressInfo = (orderCode, uid, paymentType, createTime, isDetail) => {
return co(function * () {
isDetail = isDetail || false;
let result = {};
result.logisticsUrl = '';
result.logisticsImg = '';
result.logisticsCompany = '';
result.courierNumbe = '';
result.logistics = [];
if (paymentType === 1) {
if (isDetail) {
result.logistics.push('');
} else {
result.logistics.push(moment.unix(createTime).format('YYYY.MM.DD HH:mm:ss') + ' 您的订单已提交,等待付款');
}
}
if (paymentType === 2) {
if (isDetail) {
result.logistics = result.logistics.concat([
moment.unix(createTime).format('YYYY-M-D H:m:s'),
' ',
'您的订单已提交,等待审核'
]);
} else {
result.logistics.push(moment.unix(createTime).format('YYYY.MM.DD HH:mm:ss') + ' 您的订单已提交,等待审核');
}
}
// 有物流
if (orderCode) {
let logistics = yield orderApi.getLogisticsData(orderCode, uid);
if (_.has(logistics, 'data')) {
result.logisticsUrl = _getUrlSafe(logistics.data.url);
result.logisticsImg = logistics.data.logo || '';
result.logisticsCompany = logistics.data.caption || '';
result.courierNumbe = logistics.data.express_number || '';
let expressDetail = logistics.data.express_detail || [];
if (!_.isEmpty(expressDetail)) {
let logisticsTmp = result.logistics[0]; // 暂存
result.logistics = [];
expressDetail.forEach((value) => {
let pos = value.accept_address.indexOf(' ');
pos = pos === -1 ? 0 : pos;
let city = value.accept_address.substr(0, pos);
let exInfo = value.accept_address.substr(pos);
if (isDetail) {
result.logistics.push([value.acceptTime, city, exInfo]);
} else {
result.logistics.push(value.acceptTime + city + exInfo);
}
});
// 把最初的处理放最后
result.logistics.push(logisticsTmp);
}
}
}
return result;
})();
};
/**
* 获取我的订单列表数据
*/
const getOrders = (uid, page, limit, type, isPage)=> {
return co(function *() {
isPage = isPage || false;
let orderInfo = yield orderApi.getUserOrders(uid, page, limit, type).then(res => _.get(res, 'data', {}));
if (_.isEmpty(_.get(orderInfo, 'order_list', []))) {
return {
empty: ORDER_EMPTY_DESC[type] || '',
list: []
};
}
let result = {};
const handleOrder = function(order) {
order.payment_type = +order.payment_type;
let newOrder = {};
newOrder.orderNum = order.order_code; // 订单标识
newOrder.orderTime = moment.unix(_.get(order, 'create_time')).format('YYYY-MM-DD HH:mm:ss');
newOrder.time = _.get(order, 'create_time');
newOrder.payType = _.get(order, 'payment_type');
if (order.is_cancel === 'Y' || order.status === 6) {
newOrder.canDelete = true; // 删除订单
}
let statusInfo = _getOrderStatus(order.is_cancel, order.status, order.payment_type,
order.payment_status, order.status_str
);
// 订单状态
if (statusInfo.cancel) {
newOrder.cancel = statusInfo.cancel;
} else {
if (statusInfo.keyName) {
newOrder[statusInfo.keyName] = true;
}
}
newOrder.statusStr = statusInfo.statusStr;
newOrder.goods = _.get(order, 'order_goods', []).map((good) => {
let newGood = {};
newGood.href = helpers.getUrlBySkc(good.product_id, good.goods_id, good.cn_alphabet);
newGood.thumb = helpers.image(good.goods_image, 100, 100);
newGood.name = good.product_name;
newGood.color = good.color_name;
newGood.size = good.size_name;
newGood.price = good.goods_price;
let buyNum = _.parseInt(good.buy_number);
let refundNum = _.parseInt(good.refund_num);
newGood.count = buyNum;
newGood.refundStatus = refundNum > 0;// 只要发生一件退换,退换过的标记
newGood.goRefundUrl = helpers.urlFormat('/home/returns');
newGood.arrivalDate = good.expect_arrival_time;
let goodsTagName = _getGoodsTag(order.attribute, good.goods_type);
if (goodsTagName) {
newGood[goodsTagName] = true;
}
return newGood;
});
newOrder.pay = order.amount; // 付款数
newOrder.fregit = order.shipping_cost; // 邮费
// 操作
let op = _.get(order, 'links', []);
// 新增加一些操作
op.unshift('showDetail');
if (_.get(order, 'is_support_exchange', 'N') === 'Y') {
op.push('exchange');
}
if (_.get(order, 'is_support_refund', 'N') === 'Y') {
op.push('refund');
}
if (order.attribute === 9) {
op = ['deposit'];
}
newOrder.operation = _getOperateInfo(order.order_code, op);
return newOrder;
};
result.list = _.get(orderInfo, 'order_list', []).map(handleOrder);
if (isPage) {
result.pager = {
total: orderInfo.total,
pageTotal: orderInfo.page_total,
page: orderInfo.page
};
}
return result;
})();
};
const index = (uid, page, limit, type) => {
return co(function * () {
let result = yield Promise.props({
orders: getOrders(uid, page, limit, type, true),
cancelReason: orderApi.closeReasons().then(res => _.get(res, 'data', ''))
});
result.pager = pager(_.get(result, 'orders.pager.total', 0), {page, limit, type});
return Object.assign(result, {
tabs: _getTabs(type)
});
})();
};
const reBuy = (uid, orderId) => {
return co(function * () {
let newOrder = yield orderApi.reBuy(uid, orderId);
if (newOrder.code !== 200) {
return {
code: 400,
message: '商品加入购物车失败'
};
}
return {
code: 200,
message: '商品已重新加入购物车',
data: newOrder.data
};
})();
};
const del = co(function * (uid, gender, channel, orderId) {
let orderInfo = yield orderApi.del(uid, gender, channel, orderId);
if (!_.has(orderInfo, 'code')) {
return {
code: 400,
message: '删除失败'
};
}
return orderInfo;
});
const _getVirtualPro = (isCancel, status, createTime) => {
let process = {
middleStatus: [
{
name: '1. 提交订单',
date: moment.unix(createTime).format('YYYY.MM.DD HH:mm:ss')
},
{
name: '2. 已发货'
},
{
name: '3. 交易完成'
}
]
};
if (isCancel === 'N') {
if (status === 0) {
process.percent = '30%';
process.middleStatus[0].cur = true;
} else if (status > 0 && status < 6) {
process.percent = '80%';
process.middleStatus[1].cur = true;
} else if (status === 6) {
process.percent = '100%';
process.middleStatus[3].cur = true;
}
}
return process;
};
const _getNormalPro = (isCancel, status, createTime) => {
let process = {
middleStatus: [
{
name: '1. 提交订单',
date: moment.unix(createTime).format('YYYY.MM.DD HH:mm:ss')
},
{
name: '2. 商品出库'
},
{
name: '3. 等待收货'
},
{
name: '4. 交易完成'
}
]
};
if (isCancel === 'N') {
if (status === 0) {
process.percent = '25%';
process.middleStatus[0].cur = true;
} else if (status > 0 && status < 4) {
process.percent = '50%';
process.middleStatus[1].cur = true;
} else if (status >= 4 && status < 6) {
process.percent = '75%';
process.middleStatus[2].cur = true;
} else if (status === 6) {
process.percent = '100%';
process.middleStatus[3].cur = true;
}
}
return process;
};
const _getOrderDetailOp = (orderId, payment, status,
isCancel, paymentStatus, paymentType,
orderType, attribute, refundStatus) => {
let operation = {};
// 立刻付款
if (paymentType === 1 && paymentStatus === 'N' && isCancel === 'N' && payment !== 19) {
Object.assign(operation, {
goPay: helpers.urlFormat('/shopping/pay', {ordercode: orderId})
});
}
// 取消订单
if (status < 3 && isCancel === 'N' && paymentStatus === 'N' && orderType !== 5) {
Object.assign(operation, {
cancelOrder: true
});
}
// 订单已支付
if (paymentType === 1 && paymentStatus === 'Y' && status < 6) {
Object.assign(operation, {
paid: true
});
}
// 确认收货
if (status >= 4 && status < 6 && refundStatus === 0 && attribute !== 3 && isCancel === 'N') {
Object.assign(operation, {
shipped: true
});
}
// 订单已取消
if (isCancel === 'Y') {
Object.assign(operation, {
cancel: true
});
}
// 虚拟查看二维码
if (attribute === 3) {
Object.assign(operation, {
checkQrCode: helpers.urlFormat('/home/orders/ticket', {orderCode: orderId})
});
}
return operation;
};
const _getPackageInfo = (cartInfo) => {
// 是否拆单
if (cartInfo.is_multi_package !== 'Y') {
return [];
}
return _.get(cartInfo, 'package_list', []).map((pack) => {
let newPack = {
supplierId: pack.supplier_id,
fee: pack.shopping_cost === '0.00' ? '' : pack.shopping_cost,
orign: pack.shopping_orig_cost,
count: pack.shopping_cut_cost
};
newPack.goodlist = _.get(pack, 'goods_list', []).map((good) => {
let tagInfo = ChannelConfig.orderTagArr[good.goods_type] || '';
return {
src: helpers.image(good.goods_images, 90, 90),
goodsType: tagInfo.name || '',
classname: tagInfo.classname || '',
link: 'javascritp:void(0)'
};
});
return newPack;
});
};
const _getOrderDetail = co(function * (uid, orderId) {
let orderInfo = yield orderApi.getOrderDetail(uid, orderId);
let detail = {};
if (orderInfo.code === 400) {
return orderInfo;
}
if (orderInfo.data) {
let orderDetail = orderInfo.data;
detail.orderNum = orderDetail.order_code;
// 订单状态
let statusInfo = _getOrderStatus(
orderDetail.is_cancel, +orderDetail.status,
orderDetail.payment_type, orderDetail.payment_status, orderDetail.status_str
);
detail.statusStr = statusInfo.statusStr;
// 订单是否已完成
detail.complete = +orderDetail.status === 6;
detail.progress = statusInfo.cancel ? false : (function() {
// 未取消订单,进度
if (orderDetail.attribute === 3) {
return _getVirtualPro(orderDetail.is_cancel, +orderDetail.status, orderDetail.create_time);
} else {
return _getNormalPro(orderDetail.is_cancel, +orderDetail.status, orderDetail.create_time);
}
}());
// 物流信息
detail.traceOrder = {};
detail.traceOrder.orderDate = moment.unix(orderDetail.create_time).format('YYYY.MM.DD HH:mm:ss');
let expressInfo = yield _getExpressInfo(orderId, uid, +orderDetail.payment_type, orderDetail.create_time, true);
detail.traceOrder.logistics = expressInfo.logistics;
if (_.get(expressInfo, 'logistics[0]')) {
detail.hastrace = true;
} else {
detail.hastrace = false;
}
detail.traceOrder.logisticsCompany = expressInfo.logisticsCompany;
detail.traceOrder.courierNumbe = expressInfo.courierNumbe;
// 虚拟商品
if (orderDetail.attribute === 3) {
detail.virtualGoods = true;
detail.virtualPayMode = {
payMode: ChannelConfig.payType[orderDetail.payment_type],
phone: _.fill(orderDetail.mobile.split(''), '*', 3, 4).join('')
};
} else {
detail.virtualGoods = false;
detail.noramlPayMode = {
payMode: ChannelConfig.payType[orderDetail.payment_type],
payWay: orderDetail.payment_name,
deliverTime: orderDetail.delivery_time || ''
};
// 配送信息
detail.orderInfo = {
receiver: orderDetail.user_name,
address: orderDetail.area + orderDetail.address,
phone: _.fill(orderDetail.mobile.split(''), '*', 3, 4).join('') +
(orderDetail.phone ? ',' + _.fill(orderDetail.phone.split(''), '*', 3, 5).join('') : '')
};
detail.editInfo = {
userName: orderDetail.user_name,
address: orderDetail.address,
areaCode: orderDetail.area_code,
mobile: orderDetail.mobile,
phoneNum: _.nth(_.split(orderDetail.phone), 0) || '',
phoneCode: _.nth(_.split(orderDetail.phone), 1) || ''
};
}
// 商品信息
if (orderDetail.order_goods) {
detail.goods = _.get(orderDetail, 'order_goods', []).map((good) => {
return {
url: helpers.getUrlBySkc(good.product_id, good.goods_id, good.cn_alphabet),
img: helpers.image(good.goods_image, 60, 60),
name: good.product_name,
color: good.factory_color_name,
size: good.size_name,
price: good.goods_price,
coin: good.yoho_give_coin,
num: good.buy_number,
sum: good.goods_amount,
sku: good.product_sku,
[_getGoodsTag(good.attribute, good.goods_type)]: true
};
});
}
// 详情页-订单付费详情
if (orderDetail.promotion_formulas) {
detail.orderBalance = _.get(orderDetail, 'promotion_formulas', []).map((promotion) => {
return {
promotion: promotion.promotion,
account: promotion.promotion_amount
};
});
detail.orderBalance.push({
promotion: '实际应支付',
account: orderDetail.amount
});
}
// 发票
if (orderDetail.invoice) {
detail.invoiceMode = true;
detail.invoiceType = _.get(orderDetail, 'invoice.type') === 2 ? '电子发票' : '纸质发票';
detail.showInvoice = _.get(orderDetail, 'invoice.showInvoice');
detail.pdfUrl = detail.showInvoice ? _.get(orderDetail, 'invoice.pdfUrl') : '';
detail.title = _.get(orderDetail, 'invoice.title') || '';
detail.contentValue = _.get(orderDetail, 'invoice.contentValue') || '个人';
}
detail.totalYoho = orderDetail.yoho_give_coin;
detail.yohoCoinUrl = helpers.urlFormat('/help/detail', {id: 105}); // 有货币介绍
detail.remark = orderDetail.remark;
detail.operation = _getOrderDetailOp(orderDetail.order_cde, orderDetail.payment, +orderDetail.status,
orderDetail.is_cancel, orderDetail.payment_status, orderDetail.payment_type,
orderDetail.order_type, orderDetail.attribute, orderDetail.refund_status);
detail.packages = _getPackageInfo(orderDetail);
// 判断是否可以修改地址
if (orderDetail.can_update_delivery_address === 'Y') {
detail.changeable = true;
}
// 判断是否可以修改省份
if (orderDetail.is_support_change_province === 'Y') {
detail.changeProvince = true;
}
return detail;
}
});
const updateDeliveryAddress = orderApi.updateDeliveryAddress;
const confirm = orderApi.confirmUserOrder;
const cancel = orderApi.cancelUserOrder;
const closeReason = () => {
return orderApi.closeReasons().then((result) => {
return _.get(result, 'data', []);
});
};
const refundReason = () => {
return orderApi.refundReason().then((result) => {
return _.get(result, 'data', []);
});
};
const refund = orderApi.refund;
const detail = co(function * (uid, orderId) {
let apiData = yield Promise.props({
detailData: _getOrderDetail(uid, orderId),
reason: closeReason()
});
if (apiData.detailData.code === 400) {
return {};
}
return {
detail: apiData.detailData,
package: apiData.detailData.package,
cancelReason: apiData.reason
};
});
const express = _.partial(_getExpressInfo, _, _, _, _, true);
module.exports = {
index,
ORDER_TYPE,
reBuy,
del,
updateDeliveryAddress,
confirm,
cancel,
detail,
getOrders,
closeReason,
express,
refund,
refundReason
};