good.js
18.5 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
/**
* 购物车商品操作
* @author: feng.chen<feng.chen@yoho.cn>
* @date: 2016/12/29
*/
'use strict';
let $ = require('yoho-jquery'),
tip = require('js/plugin/tip'),
chosePanel = require('js/common/chose-panel-new'),
loading = require('js/plugin/loading'),
isModify = false,
dialog = require('js/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('touchend', '.chk.select', function(e) {
self.checkSelectClick(e);
e.stopPropagation();
e.preventDefault();
});
$('.good-item').on('touchend', '.chk.edit', function(e) {
self.checkClick(e);
e.stopPropagation();
e.preventDefault();
});
$('.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);
});
$('.good-item').on('click', '.bottom .re-add', function(e) {
self.reAddGood(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')) {
$('.bundle-title .chk.edit').addClass('checked');
$('.good-item .chk.edit').addClass('checked');
} else {
$('.bundle-title .chk.edit').removeClass('checked');
$('.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) {
new Promise(resolve => {
let promise = self.clearLowStock();
if (promise) {
promise.then(() => {
resolve();
});
} else {
resolve();
}
}).then(() => {
if ($('.good-item.low-stocks').length) {
tip.show('您全选的商品中存在库存不足商品,已帮您自动取消勾选');
}
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 modeType = isModify ? 1 : 0;
let skuData = self.getSelectGoodData(eles, selectAll);
loading.showLoading();
return $.ajax({
type: 'post',
url: '/cart/index/new/select',
data: {
skuList: JSON.stringify(skuData),
modeType: modeType
},
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 : '网络异常');
});
},
reAddGood(eles) {
let self = this;
loading.showLoading();
$.ajax({
type: 'post',
url: '/cart/index/add',
data: {
productSku: $(eles.delegateTarget).data('id')
},
dataType: 'json',
complete: () => {
loading.hideLoading();
},
success: function(result) {
if (result.code === 200) {
self.handle.refreshPage();
} else {
tip.show(result.message);
}
},
error: function() {
tip.show('网络异常');
}
});
},
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'),
batch_no = $good.data('poolbatchno'),
activity_id = $good.data('activityid');
let goodInfo = {};
goodInfo.goods_type = $('#cartType').val();
if (typeof selectAll !== 'undefined') {
goodInfo.selected = selectAll ? 'Y' : 'N';
} else {
goodInfo.selected = $this.hasClass('checked') ? 'N' : 'Y';
}
if ($this.hasClass('presale') && isModify) {
goodInfo.selected = 'N';
}
goodInfo.product_sku = id;
goodInfo.promotion_id = promotion;
goodInfo.buy_number = $good.find('.good-num').val();
// 套餐活动 ID
if (activity_id) {
goodInfo.activity_id = activity_id;
}
// 套餐活动批次
if (batch_no) {
goodInfo.batch_no = batch_no;
}
isModify = false;
return goodInfo;
}));
},
showEditPannelWithSku(data, id, isSelected, isEditNum, promotionId, e) {
let self = this;
chosePanel.show({
data,
cartSkuId: id,
disableNum: !isEditNum,
buttonText: {
text: '确认'
}
}).then(result => {
if (result && result.sku) {
let goodData = {
new_product_sku: result.sku.skuId,
new_product_skn: result.skn,
old_product_sku: id,
buy_number: result.buyNum,
selected: isSelected,
promotionId,
isEdit: 1
},
url;
// 套餐需要 activity_id batch_no
if (data.activity_id) {
Object.assign(goodData, {
activity_id: data.activity_id,
batch_no: data.batch_no
});
}
if (promotionId) {
url = '/cart/index/new/modifyPriceGift';
} else {
url = '/cart/index/new/modify';
}
$.ajax({
method: 'POST',
url: url,
data: goodData
}).done(function(res) {
if (res && res.code === 200) {
$(e.delegateTarget).data('id', result.sku.skuId);
isModify = true;
if (!$(e.delegateTarget).find('.chk.select').hasClass('checked')) {
self.selectGood($(e.delegateTarget).find('.chk.select'));
} else {
self.handle.refreshPage();
}
}
}).fail(function() {
tip.show('网络出了点问题~');
});
}
}, () => {});
},
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);
isModify = true;
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) {
let self = this;
let skn = $(e.delegateTarget).data('skn');
let id,
count,
canEditNum,
minNum,
promotionId;
let isSelected = $(e.delegateTarget).find('.chk.select').hasClass('checked');
let activityId = $(e.delegateTarget).data('activityid');
let batchNo = $(e.delegateTarget).data('poolbatchno');
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 ||
$(e.delegateTarget).find('.flag.bundle').length ? false : true;
e.stopPropagation();
loading.showLoading();
$.ajax({
url: '/cart/index/new/goodinfo',
data: {
skn: skn,
buy_num: count,
mnum: minNum > 1 ? minNum : void 0,
promotion_id: promotionId
},
type: 'POST',
success: function(data) {
// 套餐需要传 activityId
if (activityId) {
Object.assign(data, {
activity_id: activityId,
batch_no: batchNo
});
}
self.showEditPannelWithSku(data, id, isSelected, canEditNum, promotionId, 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;