good.js
7.99 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
/**
* 购物车商品操作
* @author: feng.chen<feng.chen@yoho.cn>
* @date: 2016/12/29
*/
'use strict';
let $ = require('yoho-jquery'),
dialog = require('../../plugin/dialog'),
tip = require('../../plugin/tip'),
chosePanel = require('../chose-panel'),
loading = require('../../plugin/loading');
let goodObj = {
previousEditSkn: 0,
init(handle) {
let self = this;
self.handle = handle;
$('.good-item').on('click', '.num-opt .btn', function(e) {
let goodNumEle = $(e.delegateTarget).find('.good-num');
let minNumber = goodNumEle.data('min');
let maxNumber = goodNumEle.data('max');
let minusEl = $(e.delegateTarget).find('.btn-opt-minus');
let plusEl = $(e.delegateTarget).find('.btn-opt-plus');
let oldGoodNum = $(e.delegateTarget).find('.good-num').val();
let goodNum = oldGoodNum;
$(this).hasClass('btn-opt-plus') ? goodNum++ : goodNum--;
if (goodNum < minNumber) {
if (minNumber === 1) {
tip.show('您选择的数量不能为零~')
} else if(minNumber > 1) {
tip.show(`量贩商品,${minNumber}件起购`)
}
return;
}
if (goodNum > maxNumber) {
tip.show('您选择的数量超过了最大库存量~')
return;
}
if (goodNum === minNumber) {
$(minusEl).addClass('disabled');
} else if($(minusEl).hasClass('disabled')) {
$(minusEl).removeClass('disabled');
}
if (goodNum === maxNumber) {
$(plusEl).addClass('disabled');
} else if($(plusEl).hasClass('disabled')) {
$(plusEl).removeClass('disabled');
}
self.modifyNum($(e.delegateTarget).data('id'), oldGoodNum, goodNum);
$(e.delegateTarget).find('.good-num').val(goodNum);
});
$('.good-item').on('click', '.chk.select', function() {
self.selectGood($(this));
});
$('.good-item').on('click', '.chk.edit', function() {
$(this).toggleClass('checked');
});
$('.check-all').on('click', function() {
if (self.handle.editMode) {
$(this).find('.chk.edit').toggleClass('checked');
if ($(this).find('.chk.edit').hasClass('checked')) {
$('.good-item .chk.edit').addClass('checked');
} else {
$('.good-item .chk.edit').removeClass('checked');
}
} else {
$(this).find('.chk.select').toggleClass('checked');
self.selectGood($('.good-item:not(.low-stocks) .chk.select'), $(this).find('.chk.select').hasClass('checked'));
}
});
$('.btn-del').on('click', function() {
if (!$('.good-item .chk.edit.checked').length) {
tip.show('请至少选择一件商品');
return;
}
self.delGood($('.good-item .chk.edit.checked'));
});
$('.good-item').on('click', '.size-info', function(e) {
let $this = $(this),
skn = $(e.delegateTarget).data('skn');
let id,
count,
canEditNum;
//如果点击的是上次编辑的商品,直接显示chose-pannel
if (skn === self.previousEditSkn) {
chosePanel.show();
return;
}
let isSelected = $(e.delegateTarget).find('.chk.select').hasClass('checked');
id = $(e.delegateTarget).data('id');
count = $(e.delegateTarget).find('.good-num').val();
//加价购或者赠品不能编辑数量
canEditNum = $(e.delegateTarget).find('.flag.gift').length || $(e.delegateTarget).find('.flag.price-gift').length ? false : true;
e.stopPropagation();
loading.showLoadingMask();
$.ajax({
url: '/cart/index/new/goodinfo',
data: {
skn: skn,
buy_num: count
},
type: "POST",
success: function(res) {
self.showEditPannelWithSku(res, id, isSelected, canEditNum);
self.previousEditSkn = skn;
},
error: function() {
loading.hideLoadingMask();
tip.show('网络异常');
},
complete: function() {
loading.hideLoadingMask();
}
});
})
},
selectGood(eles, selectAll) {
let self = this;
let skuData = self.getSelectGoodData(eles, selectAll);
$.ajax({
type: 'post',
url: '/cart/index/new/select',
data: {
skuList: JSON.stringify(skuData)
}
}).then(function(data) {
if (data.code === 200) {
self.handle.refreshPage(data.data);
} else if (data.code === 400) {
tip.show('网络异常');
}
}, function() {
tip.show('网络异常');
});
},
delGood(eles) {
let self = this;
let skuData = self.getSelectGoodData(eles).map(ele => {
delete ele.goods_type;
delete ele.selected;
return ele;
});
$.ajax({
type: 'post',
url: '/cart/index/new/del',
data: {
skuList: JSON.stringify(skuData)
}
}).then(function(data) {
if (data.code === 200) {
self.handle.refreshPage(data.data);
} else if (data.code === 400) {
tip.show('网络异常');
}
}, function() {
tip.show('网络异常');
});
},
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');
console.log
let goodInfo = {};
if (!$this.hasClass('checked') && $good.find('.low-stocks').length > 0) {
tip.show('库存不足,无法购买');
return false;
}
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) {
if (html.length < 2) {
tip.show('出错啦!');
return false;
}
//删掉页面上原有的pannel
chosePanel.remove();
$(html).appendTo('#mainCart');
chosePanel.init();
chosePanel.setEditModeWithSknId(id, isSelected);
if (!isEditNum) {
chosePanel.disableNumEdit();
}
chosePanel.show();
},
modifyNum (sku, num, newNum) {
$.ajax({
type: 'post',
url: '/cart/index/new/modifyNum',
data: {
sku,
increaseNum: newNum > num ? newNum - num : 0,
decreaseNum: num > newNum ? num - newNum : 0,
}
}).then(function(data) {
if (data.code === 200) {
$(`.good-item[data-id="${sku}"]`).find('.good-num').val(newNum)
} else if (data.code === 400) {
tip.show('网络异常');
}
}, function() {
tip.show('网络异常');
});
}
}
module.exports = goodObj;