OrderModel.js
12.6 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
'use strict';
const Promise = require('bluebird');
const co = Promise.coroutine;
const OrderData = require('./OrderData');
const ChannelConfig = require('./ChannelConfig');
const helpers = global.yoho.helpers;
const api = global.yoho.API;
const _ = require('lodash');
const Image = require('../../../utils/images');
/**
* 获取我的订单列表数据-分页
* @param type $uid
* @param type $page
* @param type $limit
* @param type $type 获取订单类型 type=1全部,type=2待付款,type=3待发货,type=4待收货,type=5待评论(已成功) 7取消
*/
const getOrders = ($uid, $page, $limit, $type, $isPage)=>{
return co(function *() {
$isPage = $isPage || false;
let getOrderDescStr = {
1: '您还没有任何订单',
5: '您目前还没有成功的订单',
7: '您还没有任何取消的订单'
};
let $descStr = getOrderDescStr[$type] || '';
let $orders = {empty: $descStr};
let $orderInfo = yield OrderData.getUserOrders($uid, $page, $limit, $type);
if ($orderInfo.data && $orderInfo.data.order_list) {
$orders = {};
$orderInfo.data.order_list.forEach(function($orderV, $orderK) {
$orders[$orderK] = {};
$orders[$orderK]['orderNum'] = $orderV['order_code']; // 订单标识
$orders[$orderK]['orderTime'] = new Date($orderV['create_time']);
let $statusInfo = getOrderStatus($orderV['is_cancel'], $orderV['status'], $orderV['payment_type'], $orderV['payment_status']);
// 订单状态
if ($statusInfo['cancel']) {
$orders[$orderK]['cancel'] = $statusInfo['cancel'];
}
else {
if ($statusInfo['keyName']) {
$orders[$orderK][$statusInfo['keyName']] = true;
// 已发货,物流信息
if ($statusInfo['keyName'] == 'shipped') {
let $expressInfo = getExpressInfo($orderV['order_code'], $uid, $orderV['payment_type'], $orderV['create_time']);
$orders[$orderK]['logistics'] = $expressInfo['logistics'];
}
}
}
// 订单商品相关信息
let $opRefundStatus = true; // 订单不可操作退换货
if ($orderV['order_goods'] && $orderV['order_goods']) {
let $goods = {}, $refundFlag = {};
$orderV['order_goods'].forEach(function($goval, $gokey) {
$goods[$gokey] = {};
$goods[$gokey]['href'] = helpers.getUrlBySkc($goval['product_id'], $goval['goods_id'], $goval['cn_alphabet']);
$goods[$gokey]['thumb'] = $goval['goods_image'] && $goval['goods_image'] ? Image.getImageUrl($goval['goods_image'], 100, 100) : '';
$goods[$gokey]['name'] = $goval['product_name'];
$goods[$gokey]['color'] = $goval['color_name'];
$goods[$gokey]['size'] = $goval['size_name'];
$goods[$gokey]['price'] = $goval['goods_price'];
let $buyNum = Number($goval['buy_number']);
let $refundNum = Number($goval['refund_num']);
$goods[$gokey]['count'] = $buyNum;
let $refundStatus = ($refundNum > 0) ? true : false; // 只要发生一件退换,退换过的标记
$goods[$gokey]['refundStatus'] = $refundStatus;
$refundFlag = (($buyNum == $refundNum) && $refundNum > 0) ? 'finished' : 'unfinished'; // 某一件商品全部退换结束
$goods[$gokey]['arrivalDate'] = $goval['expect_arrival_time'];
let $goodsTagName = getGoodsTag($orderV['attribute'], $goval['goods_type']);
if ($goodsTagName) {
$goods[$gokey][$goodsTagName] = true;
}
$orders[$orderK]['goods'] = $goods;
});
if ($refundFlag.indexOf('unfinished')) {
$opRefundStatus = false;
}
$orders[$orderK]['pay'] = $orderV['amount']; // 付款数
$orders[$orderK]['fregit'] = $orderV['shipping_cost']; // 邮费
}
// 操作
$orders[$orderK]['operation'] = getOperateInfo($orderV['attribute'], $orderV['is_cancel'], $orderV['status'], $orderV['payment_status'], $orderV['update_time'], $orderV['order_type'], $orderV['refund_status'], $orderV['payment_type'], $orderV['order_code'], $opRefundStatus);
});
if ($isPage) {
$orders['pager']['total'] = $orderInfo['data']['total'];
$orders['pager']['pageTotal'] = $orderInfo['data']['page_total'];
$orders['pager']['page'] = $orderInfo['data']['page'];
}
return $orders;
}
})();
};
const getOrderStatus = ($isCancel, $status, $payType, $payStatus)=>{
// 初始化:未取消,待付款
let $ret = {cancel: false, keyName: 'noPay', statusStr: '待付款'};
if ($isCancel == 'Y') {
$ret = {cancel: true, statusStr: '已取消'};
} else {
switch ($status) {
case 0:
// '订单已成功,等待付款'
if ($payType != 2 && $payStatus == 'N') {
$ret['keyName'] = 'noPay';
$ret['statusStr'] = '待付款';
}
// '订单已付款,等待备货中'
else if ($payType != 2 && $payStatus == 'Y') {
$ret['keyName'] = 'paid';
$ret['statusStr'] = '备货中';
}
// '订单已成功,等待备货中'-货到付款
else if ($payType == 2 && $payStatus == 'N') {
$ret['keyName'] = 'complete';
$ret['statusStr'] = '备货中';
}
break;
case 1:
case 2:
case 3:
// '订单已付款,等待备货中'
$ret['keyName'] = 'paid';
$ret['statusStr'] = '备货中';
break;
case 4:
case 5:
// '订单已发货'
$ret['keyName'] = 'shipped';
$ret['statusStr'] = '待收货';
break;
case 6:
// '交易完成';
$ret['keyName'] = 'reback';
$ret['statusStr'] = '交易完成';
break;
}
}
return $ret;
};
const getExpressInfo = ($orderCode, $uid, $paymetType, $createTime, $isDetail)=>{
return co(function * () {
$isDetail = $isDetail || false;
let $result = {};
$result['logisticsUrl'] = '';
$result['logisticsImg'] = '';
$result['logisticsCompany'] = '';
$result['courierNumbe'] = '';
if ($paymetType == 1) {
if ($isDetail) {
$result['logistics'] = [new Date($createTime), ' ', '您的订单已提交,等待付款'];
}
else {
$result['logistics'] = new Date($createTime) + ' ' + '您的订单已提交,等待付款';
}
}
if ($paymetType == 2) {
if ($isDetail) {
$result['logistics'] = [new Date($createTime), ' ', '您的订单已提交,等待审核'];
}
else {
$result['logistics'] = new Date($createTime) + ' ' + '您的订单已提交,等待审核';
}
}
// 有物流
if ($orderCode && _.isNumber($uid)) {
let $logistics = yield OrderData.getLogisticsData($orderCode, $uid);
if ($logistics['data']) {
$result['logisticsUrl'] = $logistics['data']['url'] ? helper.getUrlSafe($logistics['data']['url']) : '';
$result['logisticsImg'] = $logistics['data']['logo'] ? $logistics['data']['logo'] : '';
$result['logisticsCompany'] = $logistics['data']['caption'] ? $logistics['data']['caption'] : '';
$result['courierNumbe'] = $logistics['data']['express_number'] ? $logistics['data']['express_number'] : '';
let $expressDetail = $logistics['data']['express_detail'] ? $logistics['data']['express_detail'] : {};
if ($expressDetail) {
let $logisticsTmp = $result['logistics'][0]; // 暂存
$result['logistics'] = {};
$expressDetail.forEach(function($value) {
let $pos = $value['accept_address'].indexOf(' ') / 3;
let $city = $value['accept_address'].substr(0, $pos);
let $exInfo = $value['accept_address'].substr($pos);
if ($isDetail) {
$result['logistics'] = [$value['acceptTime'], $city, $exInfo];
}
else {
$result['logistics'] = $value['acceptTime'] + $city + $exInfo;
}
});
// 把最初的处理放最后
$result['logistics'] = $logisticsTmp;
}
}
}
return $result;
})();
};
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 getOperateInfo = ($attribute, $isCancel, $status, $payStatus, $updateTime, $orderType, $refundStatus, $paymentType, $orderCode, $opRefundStatus)=>{
// 查看订单
let $orderDetailUrl = helpers.urlFormat('/home/orders/detail', {orderCode: $orderCode});
// 查看二维码
let $ticketUrl = helpers.urlFormat('/home/orders/tickets', {orderCode: $orderCode});
// 立即付款
let $payUrl = helpers.urlFormat('/shopping/pay', {orderCode: $orderCode});
// 取消订单
let $cancelOrderUrl = 'javascript:void(0)';
// 确认订单
let $confirmOrderUrl = 'javascript:void(0)';
// 申请换货
let $exchangeUrl = helpers.urlFormat('/home/returns/exchangeRequest', {orderCode: $orderCode});
// 申请退货
let $refundUrl = helpers.urlFormat('/home/returns/refundrequest', {orderCode: $orderCode});
let $operation = {};
// 立即付款
if ($payStatus == 'N' && $paymentType != 2 && $isCancel == 'N') {
$operation = {payNow: true, href: $payUrl};
}
$operation = {href: $orderDetailUrl, name: '查看订单'};
// 查看订单,虚拟订单查看二维码
if ($attribute == 3) {
if ($payStatus == 'Y') {
$operation = {href: $ticketUrl, name: '查看二维码'};
}
}
// 取消订单
if ($status < 3 && $isCancel == 'N' && $orderType != 5 && $payStatus == 'N') {
$operation = {href: $cancelOrderUrl, name: '取消订单', cancelOrder: true};
}
// 确认收货
if ($status >= 4 && $status < 6 && $refundStatus == 0 && $attribute != 3 && $isCancel == 'N') {
$operation = {href: $confirmOrderUrl, name: '确认收货', confirmReceived: true};
}
// 换货
let $time = (Date.now() - $updateTime);
let $orderExchangeLimitTime = ChannelConfig.$exchangeDay;
if ($status >= 6 && $time < 86400 * $orderExchangeLimitTime && $attribute != 3 && $isCancel == 'N') {
$operation = {href: $exchangeUrl, name: '申请换货', optDis: $opRefundStatus};
}
// 退货
let $orderRefundLimitTime = ChannelConfig.$refundDay;
if ($status >= 6 && $time < 86400 * $orderRefundLimitTime && $attribute != 3 && $isCancel == 'N') {
$operation = {href: $refundUrl, name: '申请退货', optDis: $opRefundStatus};
}
return $operation;
};
module.exports = {
getOrders
};