cart-goods-win.js
9.6 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
/**
* Created by yoho on 2017-01-05.
*/
var $ = require('yoho-jquery'),
Alert = require('../common/dialog').Alert,
$payWapper = $('.pay-wapper'),
giftsWinTpl = require('hbs/cart/cart-gifts-win-tpl.hbs'),
productInfoTpl = require('hbs/cart/cart-product-info-tpl.hbs'),
$goodsSelWin = $('#Y_goodsSelectWin'),
capi = require('./cart-api'),
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) {
$goodsSelWin.find('.detail-body').empty().html(giftsWinTpl(plist));
capi.showMDialog('#Y_goodsSelectWinDialog');
}
/** 赠品加价购弹窗 **/
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 $selectedItem;
var selectedSkn;
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) {
if (!pinfo && pinfo.code !== 200) {
return new Alert('获取商品失败,请稍后再试!').show();
}
promotionInfo = pinfo.data;
promotionInfo.isSwap = isSwap;
if (selectedSkn) {
promotionInfo.selectedSkn = selectedSkn;
}
$wrap.data('_promotionInfo', promotionInfo);
renderAndShowGiftWin(promotionInfo);
}).fail(function() {
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');
$this.siblings('li').removeClass('active');
$this.addClass('active');
capi.getProductInfo(id, skn).then(res => {
// 赠品价格为 0, 加价购价格为 活动价
if ($this.data('isgift')) {
res.salePrice = 0;
} else if ($this.data('ispricegift')) {
if ($this.data('subjoinprice')) {
res.salePrice = '¥' + $this.data('subjoinprice');
}
}
$goodsSelWin.find('.product-detail-info').empty().append(productInfoTpl(res));
});
},
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('[data-role=sizes] .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).find('span').each(function() {
if ($(this).hasClass('null-atcivec')) {
$goodsSelWin.find('.addcart').addClass('none');
$goodsSelWin.find('.btn_sellout').removeClass('none');
} else {
$goodsSelWin.find('.addcart').removeClass('none');
$goodsSelWin.find('.btn_sellout').addClass('none');
return false;
}
});
},
selSize: function() {
var $this = $(this);
// var idx = $this.index();
var shopNumAll = $this.data('num');
$this.siblings('span').removeClass('active');
$this.addClass('active');
if (shopNumAll > 0) {
$goodsSelWin.find('.addcart').removeClass('none');
$goodsSelWin.find('.btn_sellout').addClass('none');
} else {
$goodsSelWin.find('.addcart').addClass('none');
$goodsSelWin.find('.btn_sellout').removeClass('none');
}
},
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);
}
},
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('.product-detail-info').data('promotionid') || 0;
var isSwap = $this.closest('.product-detail-info').data('swap');
if ($curSize.length <= 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);
} else {
capi.addcart({
productSku: sku,
buyNumber: $num.val(),
promotionId: promotionId
});
}
}
}
};
/** 弹窗事件绑定 **/
$payWapper./* 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', '.addcart', GoodsWinAction.add2Cart);
module.exports = GoodsWinAction;