cart.js
7.8 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
/**
* 购物车模型 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 = (uid, sku, hasPromotion) => {
let method = hasPromotion ? 'app.Shopping.addfavoriteAndCart' : 'app.Shopping.addfavorite';
let params = { method };
_.merge(params, {
product_sku_list: sku,
uid // 用户必须登录吗 TODO
});
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});
return api.get('', params);
};
/*
* 商品选择与取消选择
* @function toggleSelectGoods
* @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 toggleSelectGoods
* @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,
advancedCartData;
// 普通商品
let ordinaryGoods = [],
selectedOrdinaryNum = 0,
ordinaryCartData;
// skn=0或者已下架的商品,定义为失效商品
let invalidGoods = [];
// 库存不足商品
let soldOutGoods = [];
// 商品总数量
let buyNumber;
let totalNum = 0;
// 返回值
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);
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);
totalNum += buyNumber;
if (good.selected === 'Y') {
selectedAdvanceNum += buyNumber;
}
});
}
if (result.data && result.data.ordinary_cart_data) {
// 普通商品
ordinaryCartData = result.data.ordinary_cart_data;
console.log('---------------ordinary------:', ordinaryCartData.goods_list.length);
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);
totalNum += buyNumber;
if (good.selected === 'Y') {
selectedOrdinaryNum += buyNumber;
}
});
}
// 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)
});
};
module.exports = {
addToCart,
getCartData,
transferToFavorite,
toggleSelectGoods,
removeGoods,
filterCartData
};