cart-action.js
23 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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/**
* Created by yoho on 2017-01-05.
*/
var $ = require('yoho-jquery');
// var Dialog = require('../common/dialog').Dialog;
var Alert = require('../common/dialog').Alert;
var Confirm = require('../common/dialog').Confirm;
var $cartnewTips = $('.cartnew-tips'),
$payWapper = $('.pay-wapper'),
$cartnewSum = $('.cartnew-sum'),
CART_ITEM_DEL_URL = '/cart/index/remove',
CART_ITEM_FAV_URL = '/cart/index/fav',
selColorWinTpl = require('hbs/cart/select-color-panel.hbs');
// 关闭温馨提示
$cartnewTips.find('.btn_close').click(function() {
$cartnewTips.fadeOut();
});
// 滚动到第一个选中的商品
function scrollToFirst() {
var $selected = $payWapper.find('li[data-role="pitem"] .cart-item-check.cart-item-checked:eq(0)');
var top = 0;
if ($selected.length > 0) {
top = $selected.offset().top - 36;
$('html,body').scrollTop(top);
}
return false;
}
function toggleCheckAllPros() {
}
// checkbox提交ajax
function choiceOut(items) {
var skuList = $.isArray(items) ? items : [items];
var hasPromotion = false;
$.each(skuList, function(idx, it) {
if (it.promotion_id) {
hasPromotion = true;
return false;
}
});
return $.ajax({
type: 'POST',
dataType: 'json',
url: '/cart/index/select',
data: {
skuList: JSON.stringify(skuList),
hasPromotion: hasPromotion
},
beforeSend: function() {
$('.loading').css({
top: $(document).scrollTop() + 200
});
$('.loading').show();
}
}).then(function(d) {
if (d.code === 200) {
window.history.go(0);
}
});
}
/*
* 1. 删除购物车商品,把删除的商品移入cookie中
* 2. 移到收藏夹
* data: 数据
* tpe: true - 删除,默认 移入收藏夹
*/
function cartItemDel(items, type, cookieList) {
var selList = $.isArray(items) ? items : [items];
var hasPromotion = false;
$.each(selList, function(idx, it) {
if (it.promotion_id) {
hasPromotion = true;
return false;
}
});
return $.ajax({
type: 'POST',
dataType: 'json',
url: type === true ? CART_ITEM_DEL_URL : CART_ITEM_FAV_URL,
data: {
skuList: JSON.stringify(selList),
hasPromotion: hasPromotion
},
beforeSend: function() {
$('.loading').css({
top: $(document).scrollTop() + 200
});
$('.loading').show();
}
}).then(function(d) {
if (d.code === 200) {
if (cookieList) {
window.setCookie('cart-del-list', JSON.stringify(cookieList), {
domain: '.yohobuy.com',
path: '/'
});
}
window.history.go(0);
} else if (d.code === 300) {
$('.loading').hide();
new Alert(d.message).show();
} else if (d.code === 403) {
if (d.data.url) {
window.location = d.data.url;
}
}
});
}
// 购物车商品增减
var cartItemNumChg = (function(data) {
var countBusy = false; // 保证一次只请求完成前不能再次发起
return function(data) {
if (countBusy) {
return;
}
countBusy = true;
$.ajax({
type: 'POST',
dataType: 'json',
url: '/cart/index/modifyNum',
data: data
}).then(function(d) {
if (d.code === 200) {
window.history.go(0);
} else {
new Alert(d.message === '' ? '加入购物车失败哦~~' : d.message).show();
}
countBusy = false;
});
};
})();
function getProductInfo(pid, skn) {
return $.ajax({
type: 'GET',
url: '/cart/index/getProductData', // '/product/item/getProductInfo',
data: {
productId: pid,
skn: skn
}
}).done(function(res) {
return res;
});
}
// 加入购物车,弹出框中加入购物车
function addcart(data, cookieList) {
$.ajax({
type: 'POST',
url: '/cart/index/add',
data: data
}).then(function(d) {
if (d.code === 200) {
window.history.go(0);
if (cookieList) {
window.setCookie('cart-del-list', JSON.stringify(cookieList), {
domain: '.yohobuy.com',
path: '/'
});
}
} else {
new Alert(d.message === '' ? '加入购物车失败哦~~' : d.message).show();
}
});
}
function parseProductInfo(productInfo, defaultInfo) {
var index = 0;
var colors;
var colorsLen;
var color;
// 前端处理后的集合
var filterSet = [];
var sizeIdx;
var curColor;
var curSize;
var hasActiveColor = false;
var defaultColor = defaultInfo.color;
var defaultSize = defaultInfo.size;
var defaultImg;
// 没有res.code
if (productInfo.colors) {
// 获取成功
colors = productInfo.colors;
colorsLen = colors.length;
for (index; index < colorsLen; index++) {
color = colors[index];
// 迭代每一种颜色
filterSet.push({
pid: productInfo.productId,
skn: productInfo.skn,
name: color.name,
src: color.src,
focus: color.focus,
title: color.title,
sizes: color.size,
pic: color.thumbs[0].shower,
selectable: color.total > 0
});
}
}
// 默认选中用户选择的sku,若已售罄或下架,则选中列表中第一个非售罄的sku
for (index = 0; index < filterSet.length; index++) {
curColor = filterSet[index];
if (!hasActiveColor && String(curColor.name) === String(defaultInfo.color)) {
curColor.active = true;
curColor.hasActiveColor = hasActiveColor = true;
defaultImg = curColor.pic;
}
curSize = curColor.sizes;
for (sizeIdx = 0; sizeIdx < curSize.length; sizeIdx++) {
if (curColor.hasActiveColor && curSize[sizeIdx].sku === defaultInfo.sku) {
// console.log(curSize[sizeIdx]);
curSize[sizeIdx].sizeFocus = true;
break;
}
}
}
// 若无对应颜色,则选中第一个颜色
if (!hasActiveColor) {
filterSet[0].active = true;
defaultColor = filterSet[0].color;
defaultImg = filterSet[0].pic;
}
return {
colors: filterSet,
defaultColor: defaultColor,
defaultSize: defaultSize,
defaultImg: defaultImg
};
}
function updateCartItem(newSku, oldSku) {
$.ajax({
type: 'POST',
url: '/shopping/cart/updateProduct',
data: {
swapData: JSON.stringify([{
buy_number: '1',
selected: 'Y',
new_product_sku: newSku,
old_product_sku: oldSku
}])
}
}).then(function(d) {
if (d.code === 200) {
window.history.go(0);
} else {
new Alert(d.message === '' ? '修改商品失败哦~~' : d.message).show();
}
});
}
function renderAndShowSelWin($item, pinfo) {
$item.find('.goods-choose-box').remove();
var $selWin = $(selColorWinTpl(pinfo)).appendTo($item);
$selWin.show();
}
var Cart = {
addToCart: function(params) {
},
toggleSelectOne: function() { // 单选
var $this = $(this);
var $pitem = $this.closest('li[data-role="pitem"]');
var item = {
product_sku: $pitem.data('id'),
selected: $this.hasClass('cart-item-checked') ? 'Y' : 'N',
buy_number: $pitem.data('productnum'),
goods_type: $pitem.data('goodstype'),
promotion_id: $pitem.data('promotionid') ? $pitem.data('promotionid') : 0
};
return choiceOut(item);
},
toggleSelectAll: function() { // 全选
var $this = $(this);
var selected = $this.hasClass('cart-item-checked') ? 'Y' : 'N';
var selectArray = [];
$payWapper.find('.cart-item-check').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
});
}
});
choiceOut(selectArray);
},
del: function() {
var $this = $(this);
var $item = $this.closest('li[data-role="pitem"]');
var selectArray = [];
var content = '<div><span></span>删除商品</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')) {
countJSON = {
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')
};
}
new Confirm({
content: content,
cb: function() {
cartItemDel(selectArray, true, countJSON);
}
}).show();
},
delAll: function() {
var selectArray = [];
var PromotionArray = [];
var content = '<div><span></span>删除商品</div><p>确定从购物车中删除所有选中商品?</p>';
$payWapper.find('.cart-item-check').each(function() {
var $item = $(this);
var $chk = $item.find('.cart-item-check');
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 Confirm({
content: content,
cb: function() {
cartItemDel(selectArray, true, PromotionArray);
}
}).show();
} else {
new Alert('请至少选择一件商品').show();
}
},
toFav: function() {
var $this = $(this);
var $item = $this.closest('li[data-role="pitem"]');
var item = {
product_sku: $item.data('id'),
buy_number: $item.data('productnum'),
promotion_id: $item.data('promotionid') ? $item.data('promotionid') : 0
};
cartItemDel(item);
},
toFavAll: function() {
var selectArray = [];
$payWapper.find('.cart-item-check').each(function() {
var $item = $(this);
var $chk = $item.find('.cart-item-check');
if ($chk.hasClass('cart-item-checked')) {
if ($item.data('id')) {
selectArray.push({
product_sku: $(this).data('id'),
buy_number: $(this).data('productnum'),
promotion_id: $item.data('promotionid') || 0
});
}
}
});
if (!$.isEmptyObject(selectArray)) {
cartItemDel(selectArray);
} else {
new Alert('请至少选择一件商品').show();
}
},
modNum: function() {
var $this = $(this);
var countJSON = {};
var oprType = $this.hasClass('minus') ? 'decreaseNum' : 'increaseNum';
countJSON[oprType] = 1;
if ($this.siblings('input').val() === '1' && $this.hasClass('minus')) {
return false;
}
cartItemNumChg($.extend(countJSON, {
sku: $this.closest('li[data-role="pitem"]').data('id')
}));
},
cleanAllDisable: function() {
var selectArray = [];
var PromotionArray = [];
var content = '<div><span></span>删除商品</div><p>确定从购物车中删除所有选中商品?</p>';
$payWapper.find('.cart-item-check').each(function() {
var $item = $(this);
var $chk = $item.find('.cart-item-check');
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 Confirm({
content: content,
cb: function() {
cartItemDel(selectArray, true, PromotionArray);
}
}).show();
} else {
new Alert('请至少选择一件商品').show();
}
},
showColorSizePanel: function() {
var $this = $(this);
var $item = $this.closest('li[data-role="pitem"]');
var pinfo = $this.data('_p_info');
var $selWin = $item.find('.goods-choose-box');
var pid = $item.data('pid');
var skn = $item.data('skn');
var sku = $item.data('id');
var defaultInfo = {
color: $item.data('color'),
size: $item.data('size'),
pid: pid,
sku: sku,
skn: skn
};
$payWapper.find('.pay-pro-detail').removeClass('active');
if ($selWin && $selWin.length && $selWin.is(':visible')) {
$selWin.hide();
return;
}
$item.find('.pay-pro-detail').addClass('active');
$payWapper.find('.goods-choose-box').hide();
if (!pinfo) {
getProductInfo(pid, skn).done(function(productInfo) {
pinfo = parseProductInfo(productInfo, defaultInfo);
$this.data('_p_info', pinfo);
renderAndShowSelWin($item, pinfo);
}).fail(function() {
new Alert('此商品无法编辑颜色和尺寸').show();
});
return;
}
renderAndShowSelWin($item, pinfo);
},
editColorOrSize: function() {
var $this = $(this);
var $item = $this.closest('li[data-role="pitem"]');
var pid = $item.data('pid');
var oldSku = $item.data('id');
var $size = $this.closest('.goods-choose-box').find('.choose-size .dt.active');
var newSku = $size.data('sku');
// 没有重新选择颜色-尺码,则不用重新请求显示
if (!oldSku || !newSku || oldSku === newSku) {
Cart._hideColorSizePanel($item);
return false;
}
updateCartItem(newSku, oldSku);
},
_hideColorSizePanel: function($item) {
$item.find('.goods-choose-box').hide();
$item.find('.pay-pro-detail').removeClass('active');
},
hideColorSizePanel: function(event) {
var $this = $(this);
event.stopPropagation();
Cart._hideColorSizePanel($this.closest('li[data-role="pitem"]'));
},
selectColor: function() {
var $this = $(this);
var index = $this.index('.dt');
var $srows = $this.closest('.goods-info').find('.choose-size .size-row');
if ($this.hasClass('active')) return;
$this.siblings('.dt').remove('active');
$this.addClass('active');
$srows.find('.dt').removeClass('active');
$srows.hide();
$srows.indexOf(index).show();
},
selectSize: function() {
var $this = $(this);
$this.siblings('.dt').removeClass('active');
$this.addClass('active');
},
submit: function() {
/* understock = '';
if ($('.pay-wapper input:checked').parents('tr').find('.tipNoStore').length > 0) {
shopName = $('.pay-wapper input:checked').parents('tr').find('.tipNoStore');
$.each(shopName.parents('tr').find('.pay-pro-info a'), function() {
understock += $(this).html();
});
new Alert(understock + '库存不足').show();
} else {
if ($('.zp').length > 0 && !$(this).attr('title')) {
$(this).attr('title', '1');
new Alert('您有赠品没有选择,请选择完再结算!').show();
} else {
if ($('input:checked').length > 0) {
// 添加埋点
var productId = [];
$('.pay-wapper input:checked').parents('tr').each(function() {
if ($(this).attr('data-pid')) {
productId.push($(this).attr('data-pid'));
}
});
// 结算点击埋点
window.addPoint('YB_SC_TOBUY_CLICK', {PRD_ID: productId.join(',')});
if ($('.pre-sell-box input:checked').length > 0) {
window.location.href = '/cart/index/orderEnsure?type=2';
} else {
window.location.href = '/cart/index/orderEnsure?type=1';
}
} else {
new Alert('请至少选择一件商品').show();
}
}
}*/
}
};
module.exports = Cart;
$payWapper.one('click', 'li[data-role="pitem"] .cart-item-check', Cart.toggleSelectOne); // 单选
$cartnewSum.one('click', '.cart-item-check', Cart.toggleSelectAll); // 全选
$payWapper.on('click', '.cart-del-btn', Cart.del); // 删除商品
$cartnewSum.on('click', '.delAll', Cart.delAll); // 批量删除商品
$payWapper.on('click', '.cart-remove-btn', Cart.toFav); // 移入收藏夹
$cartnewSum.on('click', '.removeAll', Cart.toFavAll); // 批量移入收藏夹商品
$payWapper.on('click', '.minus, .plus', Cart.modNum); // 修改购物车数量
$cartnewSum.on('click', '.clean-all-disable', Cart.cleanAllDisable);
$('.btn_account').on('click', Cart.submit); // 结算
$payWapper.on('click', 'li[data-role="pitem"] .pay-pro-detail', Cart.showColorSizePanel);
$payWapper.on('click', 'li[data-role="pitem"] .button-cancel', Cart.hideColorSizePanel);
$payWapper.on('click', 'li[data-role="pitem"] .button-sure', Cart.editColorOrSize);
$payWapper.on('click', 'li[data-role="pitem"] .goods-choose-box .choose-color .dt', Cart.selectColor);
$payWapper.on('click', 'li[data-role="pitem"] .goods-choose-box .choose-size .dt', Cart.selectSize);
// / 绑定颜色尺码弹窗事件 begin
/*
oldProductSku = editTarget.find('.current').attr('data-sku');
// rebind events(Tips依赖接口数据,so 不能delegate)
editTarget.find('#confirm').click(function(e) {
e.preventDefault();
newProductSku = editTarget.find('.current-sizes .active').attr('data-sku');
// 没有重新选择颜色-尺码,则不用重新请求显示
if (!oldProductSku || !newProductSku || newProductSku === oldProductSku) {
editTarget.find('.edit-color-size').remove();
return false;
}
Util.ajax({
url: '/shopping/cart/updateProduct',
type: 'PUT',
data: {
swapData: JSON.stringify([{
buy_number: '1',
selected: 'Y',
new_product_sku: newProductSku,
old_product_sku: oldProductSku
}])
},
success: function(newCartData) {
editTarget.find('.edit-color-size').remove();
Util.refreshCart(newCartData);
setEditable();
}
});
return false;
});
editTarget.find('#cancel').click(function(e) {
e.preventDefault();
editTarget.find('.edit-color-size').remove();
setEditable();
return false;
});
editTarget.find('.edit-color-size').click(function(e) {
e.preventDefault();
return false;
});
editTarget.find('.color-item').click(function(e) {
e.preventDefault();
selectColor = $(this);
if (!selectColor.hasClass('current-color')) {
selectColor.addClass('current-color').siblings().removeClass('current-color');
selectColor.parent().siblings('.current-sizes').hide().removeClass('current-sizes');
editTarget.find('#' + selectColor.attr('data-target')).show().addClass('current-sizes')
.end()
.find('.right img').attr({
src: selectColor.attr('data-imageurl'),
title: selectColor.attr('data-title')
})
.end()
.find('.selected-color').text(selectColor.attr('data-title'));
}
return false;
});
editTarget.find('.size-item').click(function() {
var $this = $(this);
if ($this.hasClass('active') || $this.hasClass('disabled')) {
return;
}
$(this).addClass('active').siblings('.active').removeClass('active');
});
// 初始化size list选中项(其他颜色中第一个尺码)
editTarget.find('.sizes-list.mb10').each(function() {
if ($(this).find('.active').length === 0) {
$(this).find('.size-item:not(.disabled)').first().addClass('active');
}
});*/
// / 绑定颜色尺码弹窗事件 end
// 显示赠品
var giftBtn = '[data-role=gift-view-btn],[data-role=gift-resel-btn],[data-role=gift-sel-btn]';
$('.shop-cart').on('click', giftBtn, function() {
var mtext = $(this).closest('[data-role=promotion-wrap]').find('textarea[data-role=gifts-cansel]').text();
var mhtml = $(this).closest('[data-role=promotion-wrap]').find('textarea[data-role=gifts-cansel]').html();
console.log(mtext);
console.log(mhtml);
var textJson = $.parseJSON(mtext);
var htmlJson = $.parseJSON(mhtml);
console.log(textJson);
console.log(htmlJson);
});
// 显示加价购