cart.js
15 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
/**
* 购物车模型 model
* @author: jinhu.dong<jinhu.dong@yoho.cn>
* @date: 2016/07/04
*/
'use strict';
var _ = require('lodash');
const api = global.yoho.API;
/*
* 加入购物车
* @function addToCart
* @param { String } productSku 商品SKU
* @param { Number } buyNumber 购买商品数量
* @param { Number } goodType 商品类型,0表示普通商品,1表示加价购商品
* @param { Number } isEdit 是否是编辑商品SKU,0表示不是编辑
* @param { Number } uid 用户UID,可以不传
* @param { String } shoppingKey 未登录用户唯一识别码,可以不传
* @return { Array } 加入购物车接口返回的数据
*/
const addToCart = (options) => {
const productSku = options.productSku;
const buyNumber = options.buyNumber;
const goodType = options.goodType || 0;
const isEdit = options.isEdit || 0;
const uid = options.uid;
const shoppingKey = options.shoppingKey;
let params = {
method: 'app.Shopping.add',
product_sku: productSku,
buy_number: buyNumber,
goods_type: goodType,
edit_product_sku: isEdit,
selected: 'Y',
promotion_id: null
};
if (uid) {
_.merge(params, {
uid: uid
});
}
if (shoppingKey) {
_.merge(params, {
shoppingKey: shoppingKey
});
}
return api.get('', params);
};
/*
* 获取购物车数据
* @function getCartData
* @param { Number } uid 用户UID,可以不传
* @param { String } shoppingKey 未登录用户唯一识别码,客户端从cookie中获取
* @return { Object }
* {
* advance_cart_data: { // 预售商品
* gift_list: [], // 赠品
* goods_list: [] // 商品列表
* off_shelves_goods_list: [], // 下架商品, 失效商品
* price_gift: [], // 加价购商品
* promotion_info: [], // 促销信息
* shopping_cart_data: [], // 商品价格计算相关信息
* sold_out_goods_list: [] // 售罄商品
* },
* ordinary_cart_data: { // 普通商品
* gift_list: [],
* goods_list: [],
* off_shelves_goods_list: [],
* price_gift: [],
* promotion_info: [],
* shopping_cart_data: [],
* sold_out_goods_list: []
* },
* md5: '...',
* message: '...'
* }
*/
const getCartData = (shoppingKey, uid) => {
// console.log('SPK:', shoppingKey);
// console.log('uid:', uid);
let params = {
method: 'app.Shopping.cart'
};
if (shoppingKey) {
_.merge(params, {
shopping_key: shoppingKey
});
}
if (uid) {
_.merge(params, {
uid: uid
});
}
return api.get('', params);
};
/*
* 移入收藏夹
* @function transferToFavorite
* @param { Number } uid 用户UID
* @param { String } sku 商品sku列表
* @param { Boolean } hasPromotion 是否有促销ID, 默认值是false
* @return { Array }
*/
const transferToFavorite = (params) => {
let method = 'app.Shopping.addfavoriteAndCart';
_.merge(params, {method});
return api.get('', params);
};
/*
* 商品选择与取消选择
* @function toggleSelectGoods
* @param { Number } uid 用户UID
* @param { String } sku 商品sku列表
* @param { String } shoppingKey 未登录用户唯一识别码,可以不传
* @return { Array }
*/
const toggleSelectGoods = (params) => {
// BLK 没有促销, 新接口
let method = 'app.Shopping.selectedAndCart';
_.merge(params, {method});
/*
let resultData;
let skus = JSON.parse(params.product_sku_list).map(sku => return sku.product_sku);
const skusLen = skus.length;
_.merge(params, {method});
return getCartData(params.shopping_key, params.uid).then(function(result) {
// 检查库存量
resultData = result.data;
_.find(
_.concat(resultData.ordinary_cart_data.goods_list, resultData.advance_cart_data.goods_list),
(goodItem) => {
if (skusLen == 1 && goodItem.product_sku === skus[0] &&
(parseInt(goodItem.buy_number, 10) > parseInt(goodItem.storage_number, 10))) {
// 单个商品
return {
code: '1000',
message: '您勾选的商品库存不足'
};
} else {
if((parseInt(goodItem.buy_number, 10) > parseInt(goodItem.storage_number, 10))) {
// 全选
return api.get('', params).then(result => {
return _.merge(result, {
code: '1001',
message: '您全选的商品中存在库存不足商品,已帮您自动取消勾选'
})
});
}
}
});
});
*/
return api.get('', params);
};
/*
* 删除商品
* @function removeGoods
* @param { Number } uid 用户UID
* @param { String } sku 商品sku列表
* @param { String } shoppingKey 未登录用户唯一识别码,可以不传
* @return { Array }
*/
const removeGoods = (params) => {
// BLK 没有促销, 新接口
let method = 'app.Shopping.removeAndCart';
_.merge(params, {method});
return api.get('', params);
};
/*
* 每次操作处理购物车数据
* @function filterCartData
* @param { Object } result 购物车数据
* {
* advance_cart_data: { // 预售商品
* gift_list: [], // 赠品
* goods_list: [] // 商品列表
* off_shelves_goods_list: [], // 下架商品, 失效商品
* price_gift: [], // 加价购商品
* promotion_info: [], // 促销信息
* shopping_cart_data: [], // 商品价格计算相关信息
* sold_out_goods_list: [] // 售罄商品
* },
* ordinary_cart_data: { // 普通商品
* gift_list: [],
* goods_list: [],
* off_shelves_goods_list: [],
* price_gift: [],
* promotion_info: [],
* shopping_cart_data: [],
* sold_out_goods_list: []
* },
* md5: '...',
* message: '...'
* }
*/
const filterCartData = (result, uid) => {
// 预售商品
let advancedGoods = [],
selectedAdvanceNum = 0,
totalAdvanceMoney,
advancedCartData;
// 普通商品
let ordinaryGoods = [],
selectedOrdinaryNum = 0,
totalOrdinaryMoney,
ordinaryCartData;
// skn=0或者已下架的商品,定义为失效商品
let invalidGoods = [];
// 库存不足商品
let soldOutGoods = [];
// 商品总数量
let buyNumber;
let storageNumber;
let totalNum = 0;
let noStorage = [];
// 返回值
let resData = {
loggedIn: uid
};
// 预售商品
if (result.data && result.data.advance_cart_data) {
advancedCartData = result.data.advance_cart_data;
// console.log('---------------advance------:', advancedCartData.goods_list.length);
totalAdvanceMoney = advancedCartData.shopping_cart_data.last_order_amount;
advancedGoods = _.concat(advancedGoods, advancedCartData.goods_list);
invalidGoods = _.concat(invalidGoods, advancedCartData.off_shelves_goods_list);
soldOutGoods = _.concat(soldOutGoods, advancedCartData.sold_out_goods_list);
_.forEach(advancedGoods, function(good) {
buyNumber = parseInt(good.buy_number, 10);
storageNumber = parseInt(good.storage_number, 10);
totalNum += buyNumber;
_.merge(good, {
left_number: storageNumber - buyNumber
});
if (good.selected === 'Y') {
selectedAdvanceNum += buyNumber;
if (storageNumber < buyNumber) {
noStorage.push(good.product_name);
}
}
});
}
// 普通商品
if (result.data && result.data.ordinary_cart_data) {
ordinaryCartData = result.data.ordinary_cart_data;
// console.log('---------------ordinary------:', ordinaryCartData.goods_list.length);
totalOrdinaryMoney = ordinaryCartData.shopping_cart_data.last_order_amount;
ordinaryGoods = _.concat(ordinaryGoods, ordinaryCartData.goods_list);
invalidGoods = _.concat(invalidGoods, ordinaryCartData.off_shelves_goods_list);
soldOutGoods = _.concat(soldOutGoods, ordinaryCartData.sold_out_goods_list);
_.forEach(ordinaryGoods, function(good) {
buyNumber = parseInt(good.buy_number, 10);
storageNumber = parseInt(good.storage_number, 10);
totalNum += buyNumber;
_.merge(good, {
left_number: storageNumber - buyNumber
});
if (good.selected === 'Y') {
selectedOrdinaryNum += buyNumber;
if (storageNumber < buyNumber) {
noStorage.push(good.product_name);
}
}
});
}
// console.log('ordinaryGoods:', ordinaryGoods);
return _.merge(resData, {
hasGoods: advancedGoods.length || ordinaryGoods.length || invalidGoods.length,
preSalePros: advancedGoods.length ? _.groupBy(advancedGoods, 'brand_id') : [],
commonPros: ordinaryGoods.length ? _.groupBy(ordinaryGoods, 'brand_id') : [],
invalidPros: invalidGoods,
selectedNum: selectedAdvanceNum + selectedOrdinaryNum,
checkAll: totalNum === (selectedAdvanceNum + selectedOrdinaryNum),
totalMoney: totalAdvanceMoney + totalOrdinaryMoney,
noStorage
});
};
/**
* 增减购物车商品数量
* @function modifyProductNum
* @param { Number } uid 用户ID
* @param { String } sku 商品SKU
* @param { Number } increaseNum 增加的数目
* @param { Number } decreaseNum 减少的数目
* @param { String } shoppingKey 未登录用户唯一识别码
* @return { Array } 接口返回单个商品的增减数据
*/
const modifyProductNum = (options) => {
const increaseNum = options.increaseNum;
const decreaseNum = options.decreaseNum;
const uid = options.uid;
const shoppingKey = options.shoppingKey;
const sku = options.sku;
const goodType = options.goodTpye;
let buyNumber,
storageNumber;
let params = {
product_sku: options.sku
};
// 减少到库存范围内,自动勾选
// let selectedParams;
// 增加
if (increaseNum) {
_.merge(params, {
method: 'app.Shopping.increase',
increase_number: parseInt(increaseNum, 10)
});
}
// 减少
if (decreaseNum) {
_.merge(params, {
method: 'app.Shopping.decrease',
decrease_number: parseInt(decreaseNum, 10)
});
}
if (uid) {
_.merge(params, {uid});
}
if (shoppingKey) {
_.merge(params, {
shopping_key: shoppingKey
});
}
_.merge(params, {
product_sku: sku
});
return getCartData(shoppingKey, uid).then(function(result) {
// console.log('getCartData--pre-callback-result:', JSON.stringify(result, '', 4));
// console.log('modifyProductNum---params:', params);
if (goodType === 'advance') {
// 检查库存量
// 同步
_.find(result.data.advance_cart_data.goods_list, (goodItem) => {
if (goodItem.product_sku === sku) {
buyNumber = parseInt(goodItem.buy_number, 10);
storageNumber = parseInt(goodItem.storage_number, 10);
// 计算剩余多少
_.merge(goodItem, {
left_number: storageNumber - buyNumber
});
// 减少到库存范围内,自动勾选
// if(decreaseNum && buyNumber <= storageNumber) {
// selectedParams = {
// product_sku_list: JSON.stringify([{
// "goods_type": "ordinary",
// "buy_number": buyNumber,
// "selected": "Y",
// "product_sku": sku,
// "promotion_id": 0
// }]),
// uid: uid,
// shopping_key: shoppingKey
// };
// }
}
});
// 增加
if (increaseNum && (buyNumber + increaseNum) > storageNumber) {
return {
code: '1000',
message: '商品库存不足'
};
}
} else if (goodType === 'ordinary') {
// 检查库存量
// 同步
_.find(result.data.ordinary_cart_data.goods_list, (goodItem) => {
if (goodItem.product_sku === sku) {
buyNumber = parseInt(goodItem.buy_number, 10);
storageNumber = parseInt(goodItem.storage_number, 10);
// 计算剩余多少
_.merge(goodItem, {
left_number: storageNumber - buyNumber
});
// 减少到库存范围内,自动勾选
// if(decreaseNum && buyNumber <= storageNumber) {
// selectedParams = {
// product_sku_list: JSON.stringify([{
// "goods_type": "ordinary",
// "buy_number": buyNumber,
// "selected": "Y",
// "product_sku": sku,
// "promotion_id": 0
// }]),
// uid: uid,
// shopping_key: shoppingKey
// };
// }
}
});
// 增加
if (increaseNum && (buyNumber + increaseNum) > storageNumber) {
return {
code: '1000',
message: '商品库存不足'
};
}
}
// 异步
return api.get('', params);
});
};
const modifyProduct = (options) => {
let params = {
method: 'app.Shopping.swap'
};
_.merge(params, {
swap_data: options.swapData
});
if (options.uid) {
_.merge(params, {
uid: options.uid
});
}
if (options.shoppingKey) {
_.merge(params, {
shopping_key: options.shoppingKey
});
}
return api.get('', params);
};
module.exports = {
addToCart,
getCartData,
transferToFavorite,
toggleSelectGoods,
removeGoods,
filterCartData,
modifyProductNum,
modifyProduct
};