cart-goods-win.js
14 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
/**
* Created by yoho on 2017-01-05.
*/
var $ = require('yoho-jquery'),
Alert = require('../common/dialog').Alert,
capi = require('./cart-api'),
rDialog = require('./rdialog'),
giftsWinTpl = require('hbs/cart/cart-gifts-win-tpl.hbs'),
productInfoTpl = require('hbs/cart/cart-product-info-tpl.hbs');
var RAlert = rDialog.RAlert;
var $cartListWrap = $('#Y_CartListWrap'),
$goodsSelWin = $('#Y_goodsSelectWin'),
GoodsWinAction,
// 显示赠品
giftBtn = ['[data-role=gift-view-btn]',
'[data-role=gift-resel-btn]',
'[data-role=gift-sel-btn]',
'[data-role=pg-sel-btn]',
'[data-role=pg-resel-btn]'];
function renderAndShowGiftWin(plist) {
if (!plist.goodsList || !plist.goodsList.length) {
new RAlert('<div class="alert-main">已抢光</div>');
return;
}
if (plist.productInfo.skuTitle === '规格') {
plist.productInfo.isFood = true;
}
$goodsSelWin.find('.detail-body').empty().html(giftsWinTpl(plist));
capi.showMDialog('#Y_goodsSelectWinDialog');
$goodsSelWin.find('.side-img-dd').slider2({
shownum: 7,
isCircle: true
});
}
/** 赠品加价购弹窗 **/
GoodsWinAction = {
showGiftWin: function() {
var $this = $(this);
var $wrap = $this.closest('[data-role="promotion-wrap"]');
var promotionid = $wrap.data('promotionid');
var $promotionPool = $this.closest('.promotion-pool');
var promotionInfo = $wrap.data('_promotionInfo');
var role = $this.data('role');
var isSwap = role === 'pg-resel-btn' || role === 'gift-resel-btn';
var isView = role === 'gift-view-btn';
var $selectedItem;
var selectedSkn;
if ($this.data('select')) {
return false;
}
$this.data('select', true);
if (role === 'pg-resel-btn') {
$selectedItem = $promotionPool.find('[data-role=pitem]')
.filter('[data-ispricegift]')
.filter('[data-promotionid="' + promotionid + '"]');
} else if (role === 'gift-resel-btn') {
$selectedItem = $promotionPool.find('[data-role=pitem]')
.filter('[data-isgift]')
.filter('[data-promotionid="' + promotionid + '"]');
}
if ($selectedItem && $selectedItem.length > 0) {
$selectedItem = $selectedItem.eq(0);
selectedSkn = $selectedItem.data('skn');
}
$promotionPool.find('[data-role=pitem][data-ispricegift], [data-role=pitem][data-isgift]');
if (!promotionInfo) {
capi.getProductByPromotionId(promotionid).done(function(pinfo) {
$this.data('select', false);
if (!pinfo && pinfo.code !== 200) {
return new Alert('获取商品失败,请稍后再试!').show();
}
promotionInfo = pinfo.data;
promotionInfo.isSwap = isSwap;
promotionInfo.isView = isView;
if (selectedSkn) {
promotionInfo.selectedSkn = selectedSkn;
}
if (promotionInfo.productInfo) {
promotionInfo.productInfo.isView = isView;
}
$wrap.data('_promotionInfo', promotionInfo);
renderAndShowGiftWin(promotionInfo);
}).fail(function() {
$this.data('select', false);
new Alert('获取商品失败,请稍后再试!').show();
});
return;
}
renderAndShowGiftWin(promotionInfo);
},
/* closeWin: function() {
$ModalOverflow.hide();
},*/
changeGoods: function() {
var $this = $(this);
var id = $this.data('id');
var skn = $this.data('skn');
var title = $this.data('promotiontitle');
$this.siblings('li').removeClass('active');
$this.addClass('active');
if (title) {
$this.closest('.slide-img').children('.sell-class-title').text(title);
}
capi.getProductInfo(id, skn).then(function(res) {
// 赠品价格为 0, 加价购价格为 活动价
if ($this.data('isgift')) {
res.salePrice = '¥0.00';
} else if ($this.data('ispricegift')) {
if ($this.data('subjoinprice')) {
res.salePrice = '¥' + $this.data('subjoinprice');
}
}
res.isView = $this.closest('.detail-body').find('.product-detail-info').data('isview');
if (res.skuTitle === '规格') {
res.isFood = true;
}
$goodsSelWin.find('.product-detail-info').empty().append(productInfoTpl(res));
});
$('[data-role="fav-count"]').attr('data-id', id);
},
selThumb: function() {
var $this = $(this);
var idx = $(this).index();
$goodsSelWin.find('.detail-bigpic:not(.none) .piclist li').removeClass('active');
$this.addClass('active');
$goodsSelWin.find('.detail-bigpic:not(.none) .bigpic').hide().eq(idx).show();
},
selThumbPrevNext: function() {
var $this = $(this);
var $detailBigpic = $this.closest('.detail-bigpic');
var curIndex = Number($detailBigpic.data('_index') || 0);
var $lis = $this.siblings('.con').find('li');
if ($this.hasClass('next')) {
if (curIndex >= $lis.length - 1) {
return false;
}
curIndex++;
} else {
if (curIndex < 1) {
return false;
}
curIndex--;
}
$detailBigpic.data('_index', curIndex);
$lis.removeClass('active').eq(curIndex).addClass('active');
$detailBigpic.find('.bigpic').hide().eq(curIndex).show();
},
selColor: function() {
var $this = $(this);
var idx = $this.index();
var $detail = $this.closest('.detail-goods');
var $sizes = $detail.find('.size-row');
var $detailBigpic = $detail.find('.detail-bigpic');
var $curSize = $sizes.eq(idx);
var $curDetailBig = $detailBigpic.eq(idx);
var bigPicIndex = 0; // 默认显示大图中的第一个图
$curDetailBig.data('_index', bigPicIndex);
$this.siblings('.color').find('p').removeClass('active');
$this.find('p').addClass('active');
$sizes.addClass('none');
$curSize.removeClass('none');
if ($curSize.find('span').length < 2) {
$curSize.find('span:first').addClass('active');
}
$detailBigpic.addClass('none');
$curDetailBig.removeClass('none');
$curDetailBig.find('.bigpic').hide();
$curDetailBig.find('.bigpic').eq(bigPicIndex).show();
$curDetailBig.find('.con li').removeClass('active');
$curDetailBig.find('.con li').eq(bigPicIndex).addClass('active');
$sizes.eq(idx).each(function() {
if ($(this).find('.active').data('num') === 0) {
$goodsSelWin.find('.addcart').addClass('none');
$goodsSelWin.find('.btn_sellOut').removeClass('none');
$goodsSelWin.find('.color-box .active').addClass('disabled');
} else {
$goodsSelWin.find('.addcart').removeClass('none');
$goodsSelWin.find('.btn_sellOut').addClass('none');
$goodsSelWin.find('.color-box .active').removeClass('disabled');
return false;
}
});
},
selSize: function() {
var $this = $(this);
// var idx = $this.index();
var shopNumAll = $this.data('num');
$this.closest('.detail-info').find('.submit').css('marginTop', '12px');
$this.siblings('span').removeClass('active');
$this.addClass('active');
$this.closest('[data-role=sizes]').find('.size-p-tip').hide();
if (shopNumAll > 0) {
$goodsSelWin.find('.addcart').removeClass('none');
$goodsSelWin.find('.btn-sell-out').addClass('none');
$goodsSelWin.find('.color-box .active').removeClass('disabled');
} else {
$goodsSelWin.find('.addcart').addClass('none');
$goodsSelWin.find('.btn-sell-out').removeClass('none');
$goodsSelWin.find('.color-box .active').addClass('disabled');
}
},
changeNum: function() {
var $this = $(this);
var $pinfo = $this.closest('.product-detail-info');
// var count = $this.hasClass('minus') ? 'decreaseNum' : 'increaseNum';
var promotionId = $pinfo.data('promotionid');
var $num = $goodsSelWin.find('#num');
var shopNum = Number($num.val() || 1);
if (promotionId) {
if ($this.hasClass('cut')) {
new Alert('-_-,已经是最后一件,不能再减了!').show();
} else {
new Alert('最多只能购买一件,您好像购买的太多了!').show();
}
} else {
if ($this.hasClass('add')) {
shopNum++;
} else {
shopNum--;
}
if (shopNum < 1) {
new Alert('-_-,已经是最后一件,不能在减了!').show();
shopNum = 1;
return false;
}
$num.val(shopNum);
}
},
// 收藏
btnFav: function(event) {
var favBtn = $('[data-role="fav-count"]');
var $this = $(this);
var $goodsDetail = $this.closest('.detail-goods');
var pid = $goodsDetail.data('productid');
var colTxt = {
normal: '收藏商品',
coled: '已收藏',
hover: '取消收藏'
};
function tipFav(parent) {
var $tip = $('<span class="tip-fav">收藏失败</span>').appendTo(parent);
$tip.fadeIn();
setTimeout(function() {
$tip.fadeOut();
}, 2000);
}
if (favBtn.find('.fav-count').hasClass('coled')) {
capi.productCollect(pid, true).done(function(obj) {
if (!obj && obj.code !== 200) {
tipFav(pid);
return false;
}
favBtn.find('.fav-count').removeClass('coled');
favBtn.find('em').text(colTxt.normal);
favBtn.mouseenter(function() {
favBtn.find('em').text(colTxt.normal);
}).mouseleave(function() {
favBtn.find('em').text(colTxt.normal);
});
}).fail(function() {
tipFav(pid);
});
return false;
} else {
capi.productCollect(pid).done(function(obj) {
if (!obj && obj.code !== 200) {
tipFav(pid);
return false;
}
favBtn.find('.fav-count').addClass('coled');
favBtn.find('em').text(colTxt.coled);
favBtn.mouseenter(function() {
favBtn.find('em').text(colTxt.hover);
}).mouseleave(function() {
favBtn.find('em').text(colTxt.coled);
});
event.stopPropagation();
}).fail(function() {
tipFav(pid);
});
return false;
}
},
add2Cart: function() {
var $this = $(this);
var $curSize = $goodsSelWin.find('[data-role=sizes] .size-row:not(.none) .active');
var $num = $goodsSelWin.find('#num');
var allNum = $curSize.data('num');
var sku = $curSize.data('sku');
var skn = $this.closest('.detail-goods').data('skn');
var promotionId = $this.closest('.detail-body').find('.img-item.active').data('promotionid') || 0;
var isSwap = $this.closest('.product-detail-info').data('isswap');
if ($this.hasClass('btn-grey')) {
return capi.hideMDialog('#Y_goodsSelectWinDialog');
}
if (!promotionId) {
promotionId = $this.closest('.product-detail-info').data('promotionid') || 0;
}
if ($curSize.length <= 0) {
$this.closest('.detail-info').find('.size-p-tip').show();
$this.closest('.detail-info').find('.submit').css('marginTop', '0');
// new Alert('请选择尺码').show();
return false;
}
if ($num.val() > allNum) {
new Alert('库存不足,目前还有' + allNum + '个库存').show();
} else {
/* if (Number($('#addToCart').val()) === 1) {
addcart(dataJSON);
} else {
new Alert('该商品无法加入购物车').show();
}*/
// 替换促销商品
if (isSwap) {
capi.updateCartGiftItem(promotionId, skn, sku).then(function() {
capi.hideMDialog('#Y_goodsSelectWinDialog');
});
} else {
capi.addcart({
productSku: sku,
buyNumber: $num.val(),
promotionId: promotionId
}).then(function() {
capi.hideMDialog('#Y_goodsSelectWinDialog');
});
}
}
}
};
/** 弹窗事件绑定 **/
$cartListWrap./* find('li[data-role="pitem"]').*/on('click', giftBtn.join(','), GoodsWinAction.showGiftWin);
// $goodsSelWin.on('click', '.close', GoodsWinAction.closeWin);
$goodsSelWin.on('click', '.slide-img .img-list .img-item', GoodsWinAction.changeGoods);
$goodsSelWin.on('click', '.piclist li', GoodsWinAction.selThumb);
$goodsSelWin.on('click', '.pre, .next', GoodsWinAction.selThumbPrevNext);
$goodsSelWin.on('click', '[data-role=colors] .color', GoodsWinAction.selColor);
$goodsSelWin.on('click', '[data-role=sizes] .size-row span', GoodsWinAction.selSize);
$goodsSelWin.on('click', '.cut, .add', GoodsWinAction.changeNum);
$goodsSelWin.on('click', '.btn-fav-count', GoodsWinAction.btnFav);
$goodsSelWin.on('click', '.addcart', GoodsWinAction.add2Cart);
module.exports = GoodsWinAction;