picker.js
6.69 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
const app = getApp();
Component({
properties: {
pickerData: {
type: Object,
value: null,
observer: function (value, oldValue) {
if (value.sourceType !== 'goodsDetail')
this.setData({pickerData: value})
}
},
isloadingForPayment: {
type: Boolean,
value: false
},
pickerShow: {
type: Boolean,
value: false,
observer: function (value) {
this.setData({_show: value})
}
}
},
data: {_show: false, colorSelected: false, storageNumber: 0, selectedSKU: 0},
methods: {
colorTap(event) {
var that = this;
var tempData = that.data.pickerData;
//先遍历将所有颜色、尺码置为未选中状态
tempData.view.goodsList && tempData.view.goodsList.map((item, index) => {
item.selected = false
item.size_list.map((subItem, index) => {
subItem.selected = false
subItem.enable = subItem.storage_number > 0 ? true : false
});
});
//将当前颜色置为选中状态
var tempItem = event.target.dataset.key
console.log(tempItem)
//遍历颜色尺码,根据库存切换显示状态
tempItem && tempItem.size_list && tempItem.size_list.map((item, index) => {
item.enable = item.storage_number > 0 ? true : false
})
tempItem.selected = true
//设置标志位,表示已经选择颜色
let colorSelected = true
//重置库存、当前选中sku的值
tempData.storageNumber = 0
tempData.selectedSKU = 0
tempData.view.goodsList[Number(event.target.id)] = tempItem
tempData.view.sizeList = tempItem.size_list
tempData.view.minusButtonEnable = false
//将购买数量设为初始状态
tempData.view.buyNumber = 1;
this.setData({pickerData: tempData, colorSelected, selectedSKU: 0});
},
sizeTap(event) {
var that = this;
var tempData = that.data.pickerData
//现遍历将所有尺码置为未选中状态
tempData.view.sizeList && tempData.view.sizeList.map((item, index) => {
item.selected = false
});
//将当前尺码置为选中状态
var tempItem = event.currentTarget.dataset.key
tempItem.selected = true
tempData.view.sizeList[parseInt(event.currentTarget.id)] = tempItem
//设置数量加减按钮启用状态
tempData.view.plusButtonEnable = tempItem.storage_number > 1
tempData.view.minusButtonEnable = false
tempData.view.buyButtonEnable = true
//将购买数量设为初始状态
tempData.view.buyNumber = 1
if (tempItem.storage_number <= 0) {
this.triggerEvent('errorToast', {
title: '该尺码已经售罄',
titleClassName: 'wetoast-title',
duration: 1000
})
tempData.view.buyButtonEnable = false
}
console.log(111111);
console.log(tempItem.product_sku)
this.setData({
pickerData: tempData,
storageNumber: tempItem.storage_number,
selectedSKU: tempItem.storage_number > 0 ? tempItem.product_sku : 0
});
},
show() {
this.setData({_show: true});
this.triggerEvent('showChange');
},
hide(event) {
if (event.target.id === "picker-bg") {
this.setData({_show: true});
this.triggerEvent('showChange');
}
},
bindMinus() {
let pickerData = this.data.pickerData;
let buyNumber = parseInt(pickerData.view.buyNumber);
if (buyNumber > 1) {
buyNumber = buyNumber - 1;
}
pickerData.view.buyNumber = buyNumber;
if (buyNumber === 1) {
pickerData.view.minusButtonEnable = false;
} else {
pickerData.view.minusButtonEnable = true;
}
if (buyNumber < this.data.storageNumber) {
pickerData.view.plusButtonEnable = true;
}
this.setData({pickerData});
},
bindPlus() {
if (!this.data.colorSelected) {
if (this.data.pickerData.view.goodsList.length) {
this.data.pickerData.view.goodsList.map((value, index, array) => {
if (value.selected) {
this.setData({colorSelected: true})
}
})
} else {
this.triggerEvent('errorToast', {
title: '请选择颜色',
titleClassName: 'wetoast-title',
duration: 1000
})
return;
}
}
if (!this.data.selectedSKU) {
this.triggerEvent('errorToast', {
title: '请选择尺码',
titleClassName: 'wetoast-title',
duration: 1000
})
return;
}
let pickerData = this.data.pickerData;
let buyNumber = pickerData.view.buyNumber;
if (buyNumber >= this.data.storageNumber) {
this.triggerEvent('errorToast', {
title: '对不起,没有更多库存了',
titleClassName: 'wetoast-title',
duration: 1000
})
return;
}
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});
},
goPayment() {
this.triggerEvent('goPayment', this.data)
},
addShopCart() {
this.triggerEvent('addShopCart', this.data)
},
chooseSizeColorCompleted() {
this.triggerEvent('chooseSizeColorCompleted', this.data)
}
}
})