good.js
15.3 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
/**
* 购物车商品操作
* @author: feng.chen<feng.chen@yoho.cn>
* @date: 2016/12/29
*/
'use strict';
let $ = require('yoho-jquery'),
tip = require('../../plugin/tip'),
chosePanel = require('../chose-panel'),
loading = require('../../plugin/loading'),
dialog = require('../../plugin/dialog');
let goodObj = {
init(handle) {
let self = this;
self.handle = handle;
$('.good-item').on('click', '.num-opt .btn', function(e) {
self.modifyNumClick(e);
});
$('.good-item').on('click', '.chk.select', function(e) {
self.checkSelectClick(e);
});
$('.good-item').on('click', '.chk.edit', function(e) {
self.checkClick(e);
});
$('.btn-fav').on('click', function() {
self.favClick();
});
$('.good-item').on('click', '.edit-size-info', function(e) {
self.sizeInfoClick(e);
});
$('.good-item').on('click', '.name,.color-size-row,.img', function(e) {
self.goodClick(e);
});
$('.check-all').on('click', function(e) {
self.checkedAllClick(e);
});
$('.btn-del').on('click', function() {
self.delClick();
});
$('.btn-remove').on('click', function() {
self.removeInvalidClick();
});
self.clearLowStock();
},
checkClick(e) {
$(e.currentTarget).toggleClass('checked');
},
removeInvalidClick() {
let self = this;
dialog.showDialog({
dialogText: '您确定要清空失效商品吗?',
hasFooter: {
leftBtnText: '取消',
rightBtnText: '确定'
}
}, function() {
((callback) => {
let promise = self.removeAllDisabled();
if (promise) {
promise.then(() => {
callback();
}, () => {
dialog.hideDialog();
});
} else {
return callback();
}
})(() => {
dialog.showDialog({
dialogText: '清空成功',
autoHide: true,
fast: true
});
});
});
},
delClick() {
let self = this;
if (!$('.good-item .chk.edit.checked').length) {
tip.show('请至少选择一件商品');
return;
}
dialog.showDialog({
dialogText: '您确定要从购物车中删除吗?',
hasFooter: {
leftBtnText: '取消',
rightBtnText: '确定'
}
}, function() {
self.delGood($('.good-item .chk.edit.checked')).then(() => {
dialog.showDialog({
dialogText: '删除成功',
autoHide: true,
fast: true
});
}, () => {
dialog.hideDialog();
});
});
},
goodClick(e) {
if ($(e.delegateTarget).hasClass('in-valid') && !$(e.delegateTarget).hasClass('in-valid-low')) {
tip.show('商品已下架');
} else {
window.location.href = $(e.delegateTarget).data('link');
}
},
checkSelectClick(e) {
let self = this;
if (!$(e.currentTarget).hasClass('checked') && $(e.delegateTarget).find('.low-stocks').length > 0) {
tip.show('库存不足,无法购买');
return false;
}
self.selectGood($(e.currentTarget));
},
sizeInfoClick(e) {
let self = this;
let isGift = $(e.delegateTarget).hasClass('is-gift');
self.openChosepanel(e, isGift);
},
favClick() {
let self = this;
if (!$('.good-item .chk.edit.checked').length) {
tip.show('请至少选择一件商品');
return;
}
self.favGood($('.good-item .chk.edit.checked')).then(() => {
dialog.showDialog({
dialogText: '收藏成功',
autoHide: true,
fast: true
});
}, () => {
dialog.hideDialog();
});
},
checkedAllClick(e) {
let self = this;
if (self.handle.editMode) {
$(e.currentTarget).find('.chk.edit').toggleClass('checked');
if ($(e.currentTarget).find('.chk.edit').hasClass('checked')) {
$('.good-item .chk.edit').addClass('checked');
} else {
$('.good-item .chk.edit').removeClass('checked');
}
} else {
$(e.currentTarget).find('.chk.select').toggleClass('checked');
let checked = $(e.currentTarget).find('.chk.select').hasClass('checked');
if (checked) {
let promise = self.clearLowStock();
if (promise) {
tip.show('您全选的商品中存在库存不足商品,已帮您自动取消勾选');
promise.then(() => {
self.selectGood($('.good-item:not(.low-stocks) .chk.select'), true);
});
} else {
self.selectGood($('.good-item:not(.low-stocks) .chk.select'), true);
}
} else {
self.selectGood($('.good-item .chk.select'), false);
}
}
},
selectGood(eles, selectAll) {
let self = this;
if (self.handle.posting) {
return;
}
self.handle.posting = true;
let skuData = self.getSelectGoodData(eles, selectAll);
loading.showLoading();
return $.ajax({
type: 'post',
url: '/cart/index/new/select',
data: {
skuList: JSON.stringify(skuData)
},
complete: () => {
self.handle.posting = false;
loading.hideLoading();
}
}).then(data => {
if (data.code === 200) {
self.handle.refreshPage(data.data);
} else if (data.code === 400) {
tip.show('网络异常');
}
}, err => {
tip.show(err.responseJSON ? err.responseJSON.message : '网络异常');
});
},
delGood(eles) {
let self = this;
let skuData = self.getSelectGoodData(eles).map(ele => {
delete ele.goods_type;
delete ele.selected;
return ele;
});
loading.showLoading();
return $.ajax({
type: 'post',
url: '/cart/index/new/del',
dataType: 'json',
data: {
skuList: JSON.stringify(skuData)
},
complete: () => {
loading.hideLoading();
}
}).then(data => {
if (data.code === 200) {
self.handle.refreshPage(data.data);
} else if (data.code === 400) {
tip.show('网络异常');
}
}, err => {
tip.show(err.responseJSON ? err.responseJSON.message : '网络异常');
});
},
favGood(eles) {
let self = this;
let skuData = self.getSelectGoodData(eles).map(ele => {
delete ele.goods_type;
delete ele.selected;
return ele;
});
loading.showLoading();
return new Promise((resolve, reject) => {
$.ajax({
type: 'post',
url: '/cart/index/new/col',
data: {
skuList: JSON.stringify(skuData)
},
complete: () => {
loading.hideLoading();
}
}).then((data) => {
if (data.code === 200) {
self.handle.refreshPage(data.data);
return resolve();
} else if (data.code === 400) {
tip.show('网络异常');
return reject();
} else if (data.code === 401) {
tip.show(data.message);
setTimeout(() => {
window.location.href = data.data;
}, 1000);
return reject();
}
}, err => {
tip.show(err.responseJSON ? err.responseJSON.message : '网络异常');
return reject();
});
});
},
getSelectGoodData(eles, selectAll) {
return Array.from(eles.map((i, ele) => {
let $this = $(ele),
$good = $this.closest('.good-item'),
id = $good.data('id'),
promotion = $good.data('promotion');
let goodInfo = {};
goodInfo.goods_type = $('#cartType').val();
if (selectAll !== undefined) {
goodInfo.selected = selectAll ? 'Y' : 'N';
} else {
goodInfo.selected = $this.hasClass('checked') ? 'N' : 'Y';
}
goodInfo.product_sku = id;
goodInfo.promotion_id = promotion;
goodInfo.buy_number = $good.find('.good-num').val();
return goodInfo;
}));
},
showEditPannelWithSku(html, id, isSelected, isEditNum, isGift, e) {
let self = this;
if (html.length < 2) {
tip.show('出错啦!');
return false;
}
// 删掉页面上原有的pannel
chosePanel.remove();
$(html).appendTo('#chose-panel');
chosePanel.init();
chosePanel.setEditModeWithSknId(id, isSelected, isGift);
if (!isEditNum) {
chosePanel.disableNumEdit();
}
chosePanel.show('', (cartGoodData) => {
$(e.delegateTarget).data('id', cartGoodData.new_product_sku);
let goodNum = parseInt($(e.delegateTarget).find('.good-num').val(), 10);
let max = parseInt($(e.delegateTarget).find('.good-num').data('max'), 10);
if (!$(e.delegateTarget).find('.chk.select').hasClass('checked') && goodNum <= max) {
self.selectGood($(e.delegateTarget).find('.chk.select'));
} else {
self.handle.refreshPage();
}
});
},
modifyNumClick(e) {
let self = this;
if (self.handle.posting) {
return;
}
let isGift = $(e.delegateTarget).hasClass('is-gift');
if (isGift) {
return;
}
let goodNumEle = $(e.delegateTarget).find('.good-num');
let minNumber = goodNumEle.data('min'),
maxNumber = goodNumEle.data('max');
let minusEl = $(e.delegateTarget).find('.btn-opt-minus'),
plusEl = $(e.delegateTarget).find('.btn-opt-plus');
let oldGoodNum = $(e.delegateTarget).find('.good-num').val(),
goodNum = oldGoodNum,
sku = $(e.delegateTarget).data('id');
$(e.currentTarget).hasClass('btn-opt-plus') ? goodNum++ : goodNum--;
if (goodNum <= minNumber) {
$(minusEl).addClass('disabled');
} else if (minNumber < oldGoodNum && $(minusEl).hasClass('disabled')) {
$(minusEl).removeClass('disabled');
}
if (goodNum >= maxNumber) {
$(plusEl).addClass('disabled');
} else if (maxNumber > oldGoodNum && $(plusEl).hasClass('disabled')) {
$(plusEl).removeClass('disabled');
}
if (goodNum < minNumber) {
if (minNumber === 1) {
tip.show('您选择的数量不能为零~');
} else if (minNumber > 1) {
tip.show(`量贩商品,${minNumber}件起购`);
}
return;
}
if (goodNum > maxNumber && $(e.currentTarget).hasClass('btn-opt-plus')) {
tip.show('您选择的数量超过了最大库存量~');
return;
}
self.handle.posting = true;
goodNumEle.val(goodNum);
$(e.delegateTarget).find('.count').text(`x${goodNum}`);
$.ajax({
type: 'post',
url: '/cart/index/new/modifyNum',
data: {
sku,
increaseNum: goodNum > oldGoodNum ? goodNum - oldGoodNum : 0,
decreaseNum: oldGoodNum > goodNum ? oldGoodNum - goodNum : 0,
},
success: (data) => {
if (data.code !== 200) {
tip.show('网络异常');
} else {
goodNum = parseInt($(e.delegateTarget).find('.good-num').val(), 10);
let max = parseInt($(e.delegateTarget).find('.good-num').data('max'), 10);
if (!$(e.delegateTarget).find('.chk.select').hasClass('checked') && goodNum <= max) {
self.handle.posting = false;
self.selectGood($(e.delegateTarget).find('.chk.select'));
} else {
self.handle.refreshPage().then(() => {
self.handle.posting = false;
});
}
}
},
error: (err) => {
self.handle.posting = false;
tip.show(err.responseJSON ? err.responseJSON.message : '网络异常');
},
complete: () => {
dialog.hideDialog();
}
});
},
openChosepanel(e, isGift) {
let self = this;
let skn = $(e.delegateTarget).data('skn');
let id,
count,
canEditNum,
minNum,
promotionId;
let isSelected = $(e.delegateTarget).find('.chk.select').hasClass('checked');
id = $(e.delegateTarget).data('id');
count = $(e.delegateTarget).find('.good-num').val();
minNum = $(e.delegateTarget).data('mnum');
promotionId = $(e.delegateTarget).data('promotion');
// 加价购或者赠品不能编辑数量
canEditNum = $(e.delegateTarget).find('.flag.gift').length || $(e.delegateTarget).find('.flag.price-gift').length ? false : true;
e.stopPropagation();
loading.showLoading();
$.ajax({
url: '/cart/index/new/goodinfo',
data: {
skn: skn,
buy_num: count,
mnum: minNum > 1 ? minNum : undefined,
promotion_id: promotionId
},
type: 'POST',
success: function(data) {
let html = require('common/chose-panel.hbs')(data);
self.showEditPannelWithSku(html, id, isSelected, canEditNum, isGift, e);
},
error: function(err) {
tip.show(err.responseJSON ? err.responseJSON.message : '网络异常');
},
complete: function() {
loading.hideLoading();
}
});
},
clearLowStock() {
let self = this;
let goods = $('.good-item.low-stocks').find('.chk.select.checked');
if (goods.length) {
return self.selectGood(goods);
}
return false;
},
removeAllDisabled() {
let self = this;
let goods = $('.good-item.in-valid').find('.disable');
if (goods.length) {
return self.delGood(goods);
}
return false;
}
};
module.exports = goodObj;