cart-action.js
14.2 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
/**
* Created by yoho on 2017-01-05.
*/
var $ = require('yoho-jquery'), // eslint-disable-line
rDialog = require('./rdialog'),
RConfirm = rDialog.RConfirm,
RAlert = rDialog.RAlert,
capi = require('./cart-api'),
yas = require('../common/data-yas'),
Cart;
var $cartnewTips = $('.cartnew-tips'),
$cartListWrap = $('#Y_CartListWrap');
// 关闭温馨提示
$cartnewTips.find('.btn_close').click(function() {
$cartnewTips.fadeOut();
});
function toastNoStore(parent, info) {
var tipInfo = info || '您勾选的商品库存不足';
var $tip = $('<sub class="out-of-stock">' + tipInfo + '</sub>').appendTo(parent);
$tip.fadeIn();
setTimeout(function() {
$tip.fadeOut();
}, 2000);
}
function submitPoint(type) {
// 添加埋点
var productId = [];
var $wrap = $('#Y_CartListWrap');
if (type === 'A') {
$wrap = $wrap.find('[data-role=advance]');
} else if (type === 'O') {
$wrap = $wrap.find('[data-role=ordinary]');
}
$wrap.find('.cart-item-check.cart-item-checked').each(function() {
var $this = $(this);
var $t = $this.closest('li[data-role=pitem]');
var pid = $t.data('pid');
if (pid) {
productId.push(pid);
}
});
// 结算点击埋点
yas.givePoint('YB_SC_TOBUY_CLICK', {PRD_ID: productId.join(',')});
}
Cart = {
toggleSelectOne: function() { // 单选
var $this = $(this);
var $pitem = $this.closest('li[data-role="pitem"]');
var item;
if (!$this.hasClass('cart-item-checked') && $this.data('tipnostore')) { // 无库存提示
toastNoStore($pitem);
return;
}
// $this.toggleClass('cart-item-checked');
item = {
product_sku: $pitem.data('id'),
selected: $this.hasClass('cart-item-checked') ? 'N' : 'Y', // 'Y' : 'N',
buy_number: $pitem.data('productnum'),
goods_type: $pitem.data('goodstype'),
promotion_id: $pitem.data('promotionid') ? $pitem.data('promotionid') : 0
};
return capi.choiceOut(item);
},
toggleSelectAll: function() { // 全选
var $this = $(this);
var selected;
var selectArray = [];
var $noStores = $cartListWrap.find('[data-role=cart-item-check][data-tipnostore]');
$this.toggleClass('cart-item-checked');
selected = $this.hasClass('cart-item-checked') ? 'Y' : 'N';
$cartListWrap.find('li[data-role=pitem]').each(function() {
var $t = $(this);
if ($t.data('id')) {
selectArray.push({
product_sku: $t.data('id'),
selected: selected,
buy_number: $t.data('productnum'),
goods_type: $t.data('goodstype'),
promotion_id: $t.data('promotionid') || 0
});
}
});
if (selected === 'Y' && $noStores && $noStores.length > 0) { // 无库存提示
$('html,body').animate({scrollTop: $noStores.eq(0).offset().top - 50 + 'px'}, 500);
$noStores.each(function() {
toastNoStore($(this).closest('li[data-role=pitem]'), '您全选的商品中存在库存不足商品,已帮您自动取消勾选');
});
setTimeout(function() {
capi.choiceOut(selectArray);
}, 2000);
} else {
capi.choiceOut(selectArray);
}
},
del: function() {
var $this = $(this);
var $item = $this.closest('li[data-role="pitem"]');
var selectArray = [];
var content = '<div><i class="iconfont"></i>删除商品</div><p>确定要从购物车中删除该商品?</p>';
var countJSON;
selectArray.push({
product_sku: $item.data('id'),
buy_number: $item.data('productnum'),
promotion_id: $item.data('promotionid') || 0
});
if (!$this.data('gift') && !$item.hasClass('tr-disabled')) {
countJSON = {
productPrice: $item.find('.product-price').text(),
productTitle: $item.find('.pay-pro-info a').text(),
link: $item.find('.pay-pro-info a').attr('href'),
productNum: $item.data('productnum'),
productSku: $item.data('id'),
promotionId: $item.data('promotionid')
};
}
new RConfirm(content, function() {
capi.cartItemDel(selectArray, true, countJSON ? [countJSON] : '');
});
},
delAll: function() {
var selectArray = [];
var PromotionArray = [];
var content = '<div><i class="iconfont"></i>删除商品</div><p>确定从购物车中删除所有选中商品?</p>'; //eslint-disable-line
$cartListWrap.find('[data-role=cart-item-check]').each(function() {
var $chk = $(this);
var $item = $chk.closest('[data-role=pitem]');
if ($chk.hasClass('cart-item-checked')) {
if ($item.data('id')) {
selectArray.push({
product_sku: $item.data('id'),
buy_number: $item.data('productnum'),
promotion_id: $item.data('promotionid') || 0
});
PromotionArray.push({
productPrice: $item.find('.productPrice').text(),
productTitle: $item.find('.pay-pro-info a').text(),
link: $item.find('.pay-pro-info a').attr('href'),
productNum: $item.data('productnum'),
productSku: $item.data('id'),
promotionId: $item.data('promotionid')
});
}
}
});
if (!$.isEmptyObject(selectArray)) {
new RConfirm(content, function() { // eslint-disable-line
capi.cartItemDel(selectArray, true, PromotionArray);
});
} else {
content = '<div class="alert-main"><i class="iconfont"></i>请至少选中一件商品!</div>'; // eslint-disable-line
new RAlert(content); // eslint-disable-line
}
},
cleanAllDisable: function() {
var selectArray = [];
var PromotionArray = [];
var content;
$cartListWrap.find('.tr-disabled[data-role=pitem]').each(function() {
var $item = $(this);
// var $chk = $item.find('[data-role=cart-item-check]');
if ($item.data('id')) {
selectArray.push({
product_sku: $item.data('id'),
buy_number: $item.data('productnum'),
promotion_id: $item.data('promotionid') || 0
});
PromotionArray.push({
productPrice: $item.find('.productPrice').text(),
productTitle: $item.find('.pay-pro-info a').text(),
link: $item.find('.pay-pro-info a').attr('href'),
productNum: $item.data('productnum'),
productSku: $item.data('id'),
promotionId: $item.data('promotionid')
});
}
});
if (!$.isEmptyObject(selectArray)) {
content = '<div><i class="iconfont"></i>清除失效商品</div><p>确定要清除失效商品吗?</p>'; // eslint-disable-line
new RConfirm(content, function() { // eslint-disable-line
capi.cartItemDel(selectArray, true);
}).show();
} else {
content = '<div class="alert-main"><i class="iconfont"></i>购物车中没有失效商品!</div>'; // eslint-disable-line
new RAlert(content); // eslint-disable-line
}
},
toFav: function() {
var $this = $(this);
var $item = $this.closest('li[data-role="pitem"]');
var content = '<div><i class="iconfont"></i>移入收藏</div><p>确定要将该商品从购物车中移入收藏夹吗?<br/>移入收藏后该商品将不在购物车中显示</p>';
var item = {
product_sku: $item.data('id'),
buy_number: $item.data('productnum'),
promotion_id: $item.data('promotionid') ? $item.data('promotionid') : 0
};
if ($this.hasClass('has-col-btn')) {
return;
}
new RConfirm(content, function() {
capi.cartItemDel(item);
});
},
toFavAll: function() {
var selectArray = [];
var content;
$cartListWrap.find('[data-role=cart-item-check]').each(function() {
var $chk = $(this);
var $item = $chk.closest('li[data-role=pitem]');
if ($chk.hasClass('cart-item-checked')) {
if ($item.data('id')) {
selectArray.push({
product_sku: $item.data('id'),
buy_number: $item.data('productnum'),
promotion_id: $item.data('promotionid') || 0
});
}
}
});
if (!$.isEmptyObject(selectArray)) {
content = '<div><i class="iconfont"></i>移入收藏</div><p>确定要将已选中的商品从购物车中移入收藏夹吗?<br/>移入收藏后已选中的商品将不在购物车中显示</p>'; // eslint-disable-line
new RConfirm(content, function() { // eslint-disable-line
capi.cartItemDel(selectArray);
});
} else {
content = '<div class="alert-main"><i class="iconfont"></i>请至少选中一件商品!</div>'; // eslint-disable-line
new RAlert(content); // eslint-disable-line
}
},
modNum: function() {
var $this = $(this);
var $item = $this.closest('li[data-role=pitem]');
var $btn = $item.find('.cart-item-check');
// var storagenum = $item.data('storagenum') ? $item.data('storagenum') * 1 : 0;
var countJSON = {};
var oprType = $this.hasClass('minus') ? 'decreaseNum' : 'increaseNum';
countJSON[oprType] = 1;
if ($this.siblings('input').val() === '1' && $this.hasClass('minus')) {
return false;
}
if ($this.hasClass('disabled')) {
return;
}
capi.cartItemNumChg($.extend(countJSON, {
sku: $item.data('id')
}));
if (!$btn.hasClass('cart-item-checked')) {
$btn.click();
}
},
reAdd2Cart: function() {
var $this = $(this);
var $li = $this.closest('li');
capi.addcart({
productSku: $li.data('sku'),
buyNumber: $li.data('num'),
promotionId: $li.data('promotionid'),
isReAdd: true
});
},
reFav: function() {
var $this = $(this);
var $li = $this.closest('li');
var item = {
product_sku: $li.data('sku'),
buy_number: $li.data('num'),
promotion_id: $li.data('promotionid') || 0
};
capi.cartItemDel(item, null, null, true);
},
_submit: function($t) {
if ($t.data('mix')) {
return capi.showMDialog('#Y_CartSelectDialog');
}
submitPoint();
window.location = $t.data('ensureurl') || '//www.yohobuy.com/cart/ensure';
},
submit: function() {
var content = '<div><i class="iconfont icon-tip"></i>您还未选择赠品</div><p>是否去选择赠品?</p>';
var $this = $(this);
if ($this.hasClass('btn-account-disable')) {
return false;
}
if ($('[data-role="gift-sel-btn"]').length) {
new RConfirm(content, function() {
var $firstGift = $('[data-role="gift-sel-btn"]').eq(0);
var $sitem = $firstGift;
if (!$sitem.is(':visible')) {
$sitem = $sitem.closest('.sale-info');
}
$('html,body').animate({scrollTop: $sitem.offset().top - 50 + 'px'}, 500, 'swing', function() {
$firstGift.click();
});
}, function() {
Cart._submit($this);
// capi.showMDialog('#Y_CartSelectDialog');
}, '去选择', '不要赠品');
} else {
Cart._submit($this);
}
},
submitSingle: function() {
var $t = $(this);
var type = $t.data('type');
submitPoint(type);
window.location = $t.data('url');
},
linkOffShelveTip: function() {
return new RAlert('<div class="alert-main">该商品已经下架</div>');
}
};
$cartListWrap.on('click', 'li[data-role="pitem"] .cart-item-check', Cart.toggleSelectOne); // 单选
$cartListWrap.on('click', '[data-role=cart-del-btn]', Cart.del); // 删除商品
$cartListWrap.on('click', '[data-role=cart-mov2fav-btn]', Cart.toFav); // 移入收藏夹
$cartListWrap.on('click', '.minus, .plus', Cart.modNum); // 修改购物车数量
$cartListWrap.on('click', '.cart-title .cart-item-check', Cart.toggleSelectAll); // 全选
$cartListWrap.on('click', '.cartnew-sum .cart-item-check', Cart.toggleSelectAll); // 全选
$cartListWrap.on('click', '.cartnew-sum .delete-all-sel', Cart.delAll); // 批量删除商品
$cartListWrap.on('click', '.cartnew-sum .remove-all-2fav', Cart.toFavAll); // 批量移入收藏夹商品
$cartListWrap.on('click', '.cartnew-sum .clean-all-disable', Cart.cleanAllDisable);
$cartListWrap.on('click', '#Y_SubmitBtn', Cart.submit); // 结算
$cartListWrap.on('click', '#Y_CartSelectDialog .btn-account', Cart.submitSingle);
// 重新加入购物车
$cartListWrap.on('click', '#Y_delReselWrap [data-role=readd2cart]', Cart.reAdd2Cart);
$cartListWrap.on('click', '#Y_delReselWrap [data-role=reFav]', Cart.reFav);
// 失效商品的提示
$cartListWrap.on(
'click',
'li[data-isoffshelve] [data-role=item-img], li[data-isoffshelve] [data-role=item-title]',
Cart.linkOffShelveTip);
// 商品子优惠显示和隐藏
$cartListWrap.on('mouseenter', '.sale-info', function() {
$(this).addClass('active').children('ul').removeClass('none');
});
$cartListWrap.on('mouseleave', '.sale-info', function() {
$(this).removeClass('active').children('ul').addClass('none');
});
module.exports = Cart;