returns.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
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
/**
* 个人中心 退换货
* @author chenxuan <xuan.chen@yoho.cn>
*/
'use strict';
const _ = require('lodash');
const path = require('path');
const Promise = require('bluebird');
const returnAPI = require('./returns-api');
// 使用 product中的分页逻辑
const pagerPath = path.join(global.appRoot, '/apps/product/models/public-handler.js');
const pager = require(pagerPath).handlePagerData;
const co = Promise.coroutine;
const api = global.yoho.API;
const helpers = global.yoho.helpers;
// 常量
const REFUND = 1; // 退货
const EXCHANGE = 2; // 换货
const TRUE = 'Y';
const RETURNS_EMPTY = '您没有退/换货订单';
const REFUND_URI = '/home/returns/refundDetail';
const EXCHANGE_URI = '/home/returns/exchangeDetail';
// 处理退换货商品链接
const getProductUrlBySkc = (pid, gid, cnAlphabet) => {
cnAlphabet = cnAlphabet || 'cnalphabet';
return helpers.getUrlBySkc(pid, gid, cnAlphabet);
};
const setDetailGoods = (list) => {
let resData = [];
_.forEach(list, value => {
resData.push({
name: value.product_name || '',
reason: value.reason_name
});
});
return resData;
};
// 处理订单商品的数据
function getGoodsData(goods) {
const arr = [];
goods.forEach(good => {
const obj = {};
const cnAlphabet = good.cn_alphabet || '';
obj.href = getProductUrlBySkc(good.product_id, good.goods_id, cnAlphabet);
obj.thumb = helpers.image(good.goods_image, 60, 60);
obj.name = good.product_name;
obj.color = good.color_name;
obj.size = good.size_name;
arr.push(obj);
});
return arr;
}
/**
* 退换货列表页数据
* @param {string} uid 用户 uid
* @param {Number} page 当前页数
* @param {Number} limit 每页最大条数
* @return {Object} Promise
*/
const getReturnsList = co(function*(uid, page, limit) {
page = Number.parseInt(page, 10) || 1;
limit = Number.parseInt(limit, 10) || 10;
const obj = {
orders: [],
pager: {}
};
let response = yield api.post('', {
method: 'app.refund.getList',
data: { uid: uid, page: page, limit: limit }
});
let repData = response.data;
// 处理数据
if (response.code === 200 && repData && repData.list.length) {
obj.pager = pager(repData.total, {
page: page,
limit: limit
});
repData.list.forEach(item => {
const t = {};
t.returnId = item.id;
t.orderNum = item.order_code;
t.orderTime = item.oreder_create_time.replace('-', '/');
t.returnTime = item.create_time;
t.returnStatus = item.status_name;
const canCancel = item.canCancel === TRUE;
let isChange, uri;
switch (item.refund_type) {
case REFUND:
isChange = false;
if (canCancel) {
uri = REFUND_URI;
}
break;
case EXCHANGE:
isChange = true;
if (canCancel) {
uri = EXCHANGE_URI;
}
break;
default:
}
t.isChange = isChange;
t.canCancelUrl = helpers.urlFormat(uri);
t.moreHref = helpers.urlFormat(uri, { id: item.id });
t.goods = getGoodsData(item.goods);
obj.orders.push(t);
});
} else {
obj.empty = RETURNS_EMPTY;
}
return obj;
});
const getOrderRefund = (orderCode, uid) => {
let process = function*() {
let resData = {};
let result = yield returnAPI.getRefundGoodsAsync(orderCode, uid);
if (result.data) {
let goods = [];
let returnReason = result.data.return_reason,
remarks = _.split(_.get(result, 'data.special_notice.remark', ''), ' ', 2), // 使用3个空格拆分
amount = 0;
_.forEach(_.get(result, 'data.goods_list', []), value => {
let cnAlphabet = value.cn_alphabet || 'cnalphabet';
let item = {
href: getProductUrlBySkc(value.product_id, value.goods_id, cnAlphabet),
name: value.product_name,
color: value.color_name,
size: value.size_name,
price: value.last_price,
skn: value.product_skn,
skc: value.product_skc,
sku: value.product_sku,
goods_type_id: value.goods_type_id,
goods_type: value.goods_type,
reason: returnReason
};
amount += parseInt(value.last_price, 10);
// tar note 为每个特殊商品都添加标识
if (value.is_limit_skn === 'Y') {
item.specialNoticeBo = {
title: _.get(result, 'data.special_notice.title', ''),
remark1: remarks[0] || '',
remark2: remarks[1] || ''
};
// tar note 对数组做处理,为不显示的添加 inactive
if (result.data.special_return_reason) {
let spReason = result.data.special_return_reason;
_.forEach(item.reason, (subVal, subKey) => { // eslint-disable-line
if (_.indexOf(spReason, subKey)) {
_.set(item, `reason[${subKey}].inactive`, true);
}
});
}
}
goods.push(item);
// 商品中有鞋类,传给前端标识
if (value.hasShoes) {
_.set(resData, 'tips.footwear', true);
}
});
Object.assign(resData, {
goods: goods,
orderCode: orderCode,
// 计算相关支付细节
amount: amount - _.get(result, 'data.yoho_coin_num', 0) -
_.get(result, 'data.coupon_amount', 0),
yohoCoin: result.data.yoho_coin_num,
coupon: result.data.coupon_amount,
cash: amount
});
_.forEach(result.data.return_amount_mode, (val, key) => {
if (val.is_default === 'Y') {
_.set(result, `data.return_amount_mode[${key}].default`, true);
}
});
resData.returnAmountMode = result.data.return_amount_mode;
}
return resData;
};
return co(process)();
};
const getRefundDetail = (applyId, uid) => {
let process = function*() {
let resData = {};
let result = yield Promise.all([
returnAPI.getRefundDetailAsync(applyId, uid),
returnAPI.getExpressCompanyAsync()
]);
if (result[0].data) {
let data = result[0].data;
let detail = {
isChange: false,
returnId: applyId,
orderNum: data.source_order_code,
nowStatus: data.status_name,
applyTime: data.create_time,
payMode: data.source_payment_type_desc || '',
backMode: data.return_amount_mode_name || '',
goods: setDetailGoods(data.goods_list)
};
if (+data.status !== 0) {
detail.express = {
id: _.get(data, 'notice.express_id', ''),
company: _.get(data, 'notice.express_company', ''),
number: _.get(data, 'notice.express_number', '')
};
}
detail.statusList = [];
_.forEach(data.statusList, value => {
detail.statusList.push({
name: value.name,
act: value.act === 'Y'
});
});
if (data.canCancel === 'Y') {
detail.canCancelUrl = '/home/refund/cancel';
}
resData.detail = detail;
}
if (result[1].data && resData.detail) {
_.set(resData, 'detail.expressList', _.toArray(result[1].data));
}
return resData;
};
return co(process)();
};
const getOrderExchange = (orderCode, uid) => {
let process = function*() {
let resData = {};
let result = yield returnAPI.getExchangeGoodsAsync(orderCode, uid);
if (result.data) {
let goods = [];
let sknPromise = [];
let returnReason = result.data.return_reason,
remarks = _.split(_.get(result, 'data.special_notice.remark', ''), ' ', 2); // 使用3个空格拆分
_.forEach(_.get(result, 'data.goods_list', []), value => {
let cnAlphabet = value.cn_alphabet || 'cnalphabet';
let item = {
href: getProductUrlBySkc(value.product_id, value.goods_id, cnAlphabet),
thumb: value.goods_image,
name: value.product_name,
color: value.color_name,
size: value.size_name,
price: value.last_price,
skn: value.product_skn,
skc: value.product_skc,
sku: value.product_sku,
goods_type_id: value.goods_type_id,
reason: returnReason
};
// tar note 为每个特殊商品都添加标识
if (value.is_limit_skn === 'Y') {
item.specialNoticeBo = {
title: _.get(result, 'data.special_notice.title', ''),
remark1: remarks[0] || '',
remark2: remarks[1] || ''
};
// tar note 对数组做处理,为不显示的添加 inactive
if (result.data.special_return_reason) {
let spReason = result.data.special_return_reason;
_.forEach(item.reason, (subVal, subKey) => { // eslint-disable-line
if (_.indexOf(spReason, subKey)) {
_.set(item, `reason[${subKey}].inactive`, true);
}
});
}
}
goods.push(item);
// 商品中有鞋类,传给前端标识
if (value.hasShoes) {
_.set(resData, 'tips.footwear', true);
}
sknPromise.push(returnAPI.getProductDataAsync(value.product_skn));
});
Object.assign(resData, {
orderCode: orderCode,
// 换货地址相关数据(邮编为可空参数)
name: _.get(result, 'data.address.consignee', ''),
area: _.get(result, 'data.address.area', ''),
area_code: _.get(result, 'data.address.area_code', ''),
address: _.get(result, 'data.address.address', ''),
phone: _.get(result, 'data.address.mobile', ''),
postcode: _.get(result, 'data.address.zip_code', '')
});
let product = yield Promise.all(sknPromise);
_.forEach(product, (value, key) => { // 遍历得到每件商品
let colorSize = [];
_.forEach(value.goods_list, val => { // 遍历商品得到每个颜色
let size = [];
_.forEach(val.size_list, v => { // 遍历颜色得到每个尺码
if (+v.storage_number > 0) { // 当某个尺码下有库存时,将该颜色及其对应的尺码加入该商品选项下
size.push(v);
}
});
if (size.length) {
colorSize.push({
color: val.color_name,
colorId: val.color_id,
goodsId: val.goods_id,
sizeList: size
});
}
});
if (colorSize.length) {
goods[key].colorSize = colorSize;
} else {
goods[key].banMsg = '库存不足不能换货';
}
});
resData.goods = goods;
}
return resData;
};
return co(process)();
};
module.exports = {
getReturnsList,
getOrderRefund,
getRefundDetail,
getOrderExchange
};