cart-api.js
8.04 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
/**
* cart api
* @author: lcy<chuanyang.liu@yoho.cn>
* @date: 2016/12/26
*/
'use strict';
const api = global.yoho.API;
const _ = require('lodash');
/**
* 获取购物车数据
* @param uid
* @param shoppingKey
* @returns {*}
*/
const cartData = (uid, shoppingKey) => {
let param = {
method: 'app.Shopping.queryCart' // old: 'app.Shopping.cart'
};
if (uid) {
param.uid = uid;
}
if (shoppingKey) {
param.shopping_key = shoppingKey;
}
return api.get('', param);
};
/**
* 加入购物车接口
*
* @param int $productSku 商品SKU
* @param int $buyNumber 购买数量
* @param int $goodsType 商品类型,0表示普通商品,1表示加价购商品
* @param int $isEdit 是否是编辑商品SKU,0表示不是编辑
* @param null|int $promotionId 促销id,默认null(加价购有关)
* @param null|int $uid 用户UID,可以不传
* @param string $shoppingKey 未登录用户唯一识别码,可以不传
* @return array 加入购物车接口返回的数据
*/
const addToCart = (productSku, buyNumber,
goodsType, isEdit,
promotionId, uid, shoppingKey) => {
let param = {
method: 'app.Shopping.add',
product_sku: productSku,
buy_number: Number(buyNumber),
goods_type: goodsType,
edit_product_sku: isEdit || 0,
selected: 'Y',
promotion_id: promotionId || null
};
if (uid) {
param.uid = uid;
}
if (shoppingKey) {
param.shopping_key = shoppingKey;
}
return api.get('', param);
};
/**
* 购物车商品选择与取消接口
*
* @param int $uid 用户ID
* @param string $sku 商品sku列表
* @param string $shoppingKey 未登录用户唯一识别码
* @param bool $hasPromotion 是否有促销ID
* @return array 购物车接口返回的数据
*/
const selectGoods = (uid, sku, shoppingKey /* , hasPromotion*/) => {
let param = {
// method: hasPromotion ? 'app.Shopping.selectedAndCart' : 'app.Shopping.selected',
method: 'app.Shopping.selectedAndQryCart',
product_sku_list: sku
};
if (uid) {
param.uid = uid;
}
if (shoppingKey) {
param.shopping_key = shoppingKey;
}
return api.get('', param);
};
/**
* 移出购物车
*
* @param int $uid 用户ID
* @param string $sku 商品sku列表
* @param string $shoppingKey 未登录用户唯一识别码
* @param bool $hasPromotion 是否有促销ID
* @return array 接口返回的数据
*/
const removeFromCart = (uid, shoppingKey, skuList, hasPromotion) =>{
let param = {
method: hasPromotion ? 'app.Shopping.removeAndQryCart' : 'app.Shopping.remove',
product_sku_list: skuList
};
if (uid) {
param.uid = uid;
}
if (shoppingKey) {
param.shopping_key = shoppingKey;
}
return api.get('', param);
};
/**
* 移入收藏夹
*
* @param int $uid 用户ID
* @param string $sku 商品sku列表
* @param bool $hasPromotion 是否有促销ID
* @return array 接口返回的数据
*/
const addToFav = (uid, skuList /* , hasPromotion*/) =>{
let param = {
// method: hasPromotion ? 'app.Shopping.addfavoriteAndCart' : 'app.Shopping.addfavorite',
method: 'app.Shopping.addfavoriteAndQryCart',
product_sku_list: skuList,
uid: uid
};
return api.get('', param);
};
/**
* 增减购物车商品数量
*
* @param int $uid 用户ID
* @param string $sku 商品SKU
* @param int $increaseNum 增加的数目
* @param int $decreaseNum 减少的数目
* @param string $shoppingKey 未登录用户唯一识别码
* @return array 接口返回的数据
*/
const modifyProductNum = (uid, sku, increaseNum, decreaseNum, shoppingKey) => {
let param = {
product_sku: sku
};
// 增加
if (increaseNum) {
param.method = 'app.Shopping.increase';
param.increase_number = Number(increaseNum);
}
// 减少
if (decreaseNum) {
param.method = 'app.Shopping.decrease';
param.decrease_number = Number(decreaseNum);
}
if (uid) {
param.uid = uid;
}
if (shoppingKey) {
param.shopping_key = shoppingKey;
}
return api.get('', param);
};
/**
* 购物车数量
*
* @param int $uid 用户ID
* @param string $shoppingKey 未登录用户唯一识别码
* @return array 购物车接口返回的数据
*/
const cartCount = (uid, shoppingKey) => {
let param = {
method: 'app.Shopping.count'
};
if (uid) {
param.uid = uid;
}
if (shoppingKey) {
param.shopping_key = shoppingKey;
}
return api.get('', param);
};
/**
* 凑单商品
*
* @param int $page 分页页码
* @return array
*/
const togetherProduct = (page) => {
let param = {
method: 'web.product.together',
client_type: 'web',
// private_key: Yohobuy::$privateKeyList['web'];
page: page
};
return api.get('', param);
};
/**
* 判断商品列表是否收藏情况
* @param uid
* @param pidList
* @returns {Promise.<T>}
*/
const checkUserIsFavProductList = (uid, pidList) => {
pidList = _.isArray(pidList) ? pidList : [];
let promises = _.map(pidList, pid => {
return api.get('', {
method: 'app.favorite.isFavorite',
id: pid,
uid: uid,
type: 'product'
});
});
return Promise.all(promises)
.then(ret => {
let result = {};
_.forEach(pidList, (pid, index) => {
result[pid] = ret[index];
});
return result;
});
};
/**
* 个人中心页面优选新品数据
*
* @param int $channel 频道,1代表男生,2代表女生,3代表潮童,4代表创意生活
* @param $uid 用户ID
* @param $udid 设备ID
* @param $rec_pos 位置码
* @param $limit 数量限制
* @return array 接口返回的数据
*/
const newPreference = (channel, uid, udid, recPos, limit) => {
let param = {
method: 'app.home.newPreference',
yh_channel: channel,
udid: udid,
rec_pos: recPos,
limit: limit
};
if (uid) {
param.uid = uid;
}
return api.get('', param);
};
/**
* 修改商品颜色和尺寸
* @function modifyProduct
* @param { Number } uid 用户ID
* @param { String } swapData 商品修改信息
* @param { String } shoppingKey 未登录用户唯一识别码
* @return { Object } 接口返回单个商品修改结果
*/
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);
};
/**
* 更换加价购,赠品商品
* @function modifyProduct
* @param { Number } uid 用户ID
* @param { String } swapData 商品修改信息
* @param { String } shoppingKey 未登录用户唯一识别码
* @return { Object } 接口返回单个商品修改结果
*/
const swapGift = (uid, shoppingKey, promotionId, newSkn, newSku) => {
let param = {
method: 'app.Shopping.swapGift',
promotion_id: promotionId,
new_product_skn: newSkn,
new_product_sku: newSku
};
if (uid) {
param.uid = uid;
}
if (shoppingKey) {
param.shopping_key = shoppingKey;
}
return api.get('', param);
};
/**
* 查询所有可选的赠品或者加价购活动下的商品
* @param promotionId array
* @returns {*}
*/
const queryPromotionGift = (promotionIds) => {
let param = {
method: 'app.Shopping.queryPromotionGifts',
promotion_ids: promotionIds
};
return api.get('', param);
};
module.exports = {
cartData,
addToCart,
selectGoods,
removeFromCart,
addToFav,
modifyProductNum,
cartCount,
togetherProduct,
checkUserIsFavProductList,
newPreference,
modifyProduct,
swapGift,
queryPromotionGift
};