picker.js
5.8 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
function init(ctx) {
let data = {};
let view = ctx.data.pickerData.view;
data.sourceType = ctx.data.pickerData.sourceType;
data.view = view;
ctx.setData({
pickerData: data
});
}
function pickerShow(ctx) {
let data = ctx.data.pickerData;
data.view.isShow = !data.view.isShow;
ctx.setData({
pickerData: data
});
}
function pickerHide(ctx) {
let data = ctx.data.pickerData;
data.view.isShow = false;
ctx.setData({
pickerData: data
});
}
const pickerAction = {
chooseColor: function(e) {
let pickData = this.data.pickerData;
// 先遍历将所有颜色、尺码置为未选中状态
pickData.view.goodsList && pickData.view.goodsList.map(item => {
item.selected = false;
item.size_list.map(subItem => {
subItem.selected = false;
subItem.enable = subItem.storage_number > 0;
});
});
// 将当前颜色置为选中状态
let tempItem = e.target.dataset.key;
// 遍历颜色尺码,根据库存切换显示状态
tempItem && tempItem.size_list && tempItem.size_list.map(item => {
item.enable = item.storage_number > 0;
});
tempItem.selected = true;
this.data.selectedProductId = tempItem.goods_id;
// 设置标志位,表示已经选择颜色
let colorSelected = true;
// 重置库存、当前选中sku的值
pickData.storageNumber = 0;
pickData.selectedSKU = 0;
this.data.selectedSKU = 0;
pickData.view.goodsList[Number(e.target.id)] = tempItem;
pickData.view.sizeList = tempItem.size_list;
pickData.view.minusButtonEnable = false;
// 将购买数量设为初始状态
pickData.view.buyNumber = 1;
this.setData({
colorSelected,
pickerData: pickData
});
},
chooseSize: function(e) {
let pickData = this.data.pickerData;
// 现遍历将所有尺码置为未选中状态
pickData.view.sizeList && pickData.view.sizeList.map(item => {
item.selected = false;
});
// 将当前尺码置为选中状态
let tempItem = e.currentTarget.dataset.key;
tempItem.selected = true;
pickData.view.sizeList[parseInt(e.currentTarget.id)] = tempItem;
// 设置数量加减按钮启用状态
pickData.view.plusButtonEnable = (!pickData.view.buy_limit_number || pickData.view.buy_limit_number > 1) && tempItem.storage_number > 1; // eslint-disable-line
pickData.view.minusButtonEnable = false;
pickData.view.buyButtonEnable = true;
if (pickData.view.bundle_count === 0) {
pickData.view.buyNumber = 1;
}
if (tempItem.storage_number <= 0) {
wx.showToast({
title: '该尺码已经售罄',
icon: 'none',
duration: 2000
});
pickData.view.buyButtonEnable = false;
}
this.setData({
pickerData: pickData,
selectedSKU: tempItem.storage_number > 0 ? tempItem.product_sku : 0,
storageNumber: tempItem.storage_number
});
},
bindMinus: function() {
let pickerData = this.data.pickerData;
let buyNumber = parseInt(pickerData.view.buyNumber);
if (pickerData.view.bundle_count > 0) {
if (buyNumber > pickerData.view.bundle_count) {
pickerData.view.minusButtonEnable = true;
} else {
this.minReachedLimitAction(pickerData.view.bundle_count);
pickerData.view.minusButtonEnable = false;
return;
}
}
if (buyNumber > 1) {
buyNumber = buyNumber - 1;
}
pickerData.view.buyNumber = buyNumber;
if (buyNumber === 1) {
pickerData.view.minusButtonEnable = false;
} else {
pickerData.view.minusButtonEnable = true;
}
if (pickerData.view.buy_limit_number !== 0 && buyNumber < pickerData.view.buy_limit_number) {
pickerData.view.plusButtonEnable = true;
}
if (buyNumber < this.data.storageNumber) {
pickerData.view.plusButtonEnable = true;
}
this.setData({pickerData});
},
// 增加购买数量
bindPlus: function() {
if (!this.data.colorSelected) {
return wx.showToast({
title: '请选择颜色',
icon: 'none',
duration: 1000
});
}
if (!this.data.selectedSKU) {
return wx.showToast({
title: '请选择尺码',
icon: 'none',
duration: 1000
});
}
let pickerData = this.data.pickerData;
let buyNumber = pickerData.view.buyNumber;
if (pickerData.view.buy_limit_number !== 0 && buyNumber >= pickerData.view.buy_limit_number) {
return this.plusReachedLimitAction(pickerData.view.buy_limit_number);
}
if (buyNumber >= this.data.storageNumber) {
return this.plusReachedMaxAction();
}
if (pickerData.view.buy_limit_number !== 0 && buyNumber < pickerData.view.buy_limit_number) {
pickerData.view.plusButtonEnable = true;
}
if (buyNumber < this.data.storageNumber) {
pickerData.view.plusButtonEnable = true;
buyNumber = parseInt(buyNumber) + 1;
}
if (buyNumber >= this.data.storageNumber) {
pickerData.view.plusButtonEnable = false;
}
if (buyNumber > 1) {
pickerData.view.minusButtonEnable = true;
}
pickerData.view.buyNumber = buyNumber;
this.setData({pickerData});
},
plusReachedMaxAction: function() {
return wx.showToast({
title: '对不起,没有更多库存了',
icon: 'none',
duration: 1000
});
},
plusReachedLimitAction: function(n) {
return wx.showToast({
title: '限购' + n + '件',
icon: 'none',
duration: 1000
});
},
minReachedLimitAction: function(n) {
return wx.showToast({
title: n + '件起售',
icon: 'none',
duration: 1000
});
},
clearPickerData() {
this.setData({
colorSelected: false,
selectedSKU: '',
storageNumber: 0
});
}
};
module.exports = {
init: init,
pickerShow: pickerShow,
pickerHide: pickerHide,
pickerAction
};