goodsCollect.js
7.62 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
//goodsCollect.js
import { API_HOST, SERVICE_HOST } from '../../../libs/config';
import { GET, POST } from '../../../libs/request';
import { getChannelCode, getGenderCode } from '../../../utils/home';
//获取应用实例
var app = getApp()
const windowWidth = app.globalData.systemInfo.windowWidth;
const windowHeight = app.globalData.systemInfo.windowHeight;
const screenHeight = app.globalData.systemInfo.screenHeight;
Page({
/**
* 页面的初始数据
*/
data: {
isFetching: false,
categoryList: null,
productList: null,
currentProductList: null,
currentCategoryId: '',
total: -1,
current_page_name: 'goodsCollect',
deleteAnimation: '', // 控制是否给相应的单元增加删除动画
enableScroll: true,
AnimatingSku: '',
tempSku: '',
screenHeight
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.fetchCollectList();
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
this.animation = tt.createAnimation({
duration: 300,
timingFunction: 'ease',
})
},
fetchCollectList: function () {
let gender = getGenderCode(getApp().globalData.selectedChannel);
let yh_channel = getChannelCode(getApp().globalData.selectedChannel);
let param = {
method: 'app.favorite.product',
gender,
yh_channel,
limit: 500,
}
this.setData({
isFetching: true,
});
GET(API_HOST, param)
.then(data => {
if (!data || !data.code || data.code != 200) {
this.setData({
isFetching: false,
});
return;
}
this.data.total = data.data.total;
data.data.category_list.splice(0, 0, {
category_name: '全部',
category_id: ''
});
this.data.categoryList = data.data.category_list;
this.data.productList = data.data.product_list;
this.setData({
isFetching: false,
total: this.data.total,
categoryList: this.data.categoryList,
productList: this.data.productList,
currentProductList: this.data.productList,
});
})
.catch(error => {
this.setData({
isFetching: false,
});
});
},
onCategoryItemSelected: function (e) {
// console.log(e);
this.moveStart(e);
let id = e.currentTarget.dataset.id;
let productList = [];
if (id != "") {
this.data.productList.forEach((item) => {
if (id == item.category_id) {
productList.push(item);
}
});
}
this.setData({
currentCategoryId: id,
currentProductList: id != "" ? productList : this.data.productList,
});
},
moveStart: function (e) {
// console.log('moveStart');
let enableScroll = true;
this.data.touchX = e.changedTouches[0].clientX;
this.data.touchY = e.changedTouches[0].clientY;
var tempAniamtion = this.animation.left('0px').step();
this.setData({
deleteAnimation: tempAniamtion.export(),
enableScroll,
AnimatingSku: '',
});
},
moveEnd: function (e) {
// console.log('moveEnd');
var X = e.changedTouches[0].clientX;
var Y = e.changedTouches[0].clientY;
var curGoods = e.currentTarget.dataset.type;
var data = null, tempAniamtion = null;
var angle = this.angle({ X: X, Y: Y }, { X: this.data.touchX, Y: this.data.touchY });
if (Math.abs(angle) > 30) return;
if (this.data.touchX - X > 3) {
// console.log('左滑动');
if (this.data.AnimatingSku) {
let enableScroll = false;
tempAniamtion = this.animation.left('0px').step();
this.setData({
AnimatingSku: '',
deleteAnimation: tempAniamtion.export(),
enableScroll,
})
} else {
// console.log('show');
let enableScroll = false;
tempAniamtion = this.animation.left('-70px').step();
this.setData({
AnimatingSku: curGoods.product_skn,
tempSku: curGoods.product_skn,
deleteAnimation: tempAniamtion.export(),
enableScroll,
})
}
} else if (this.data.touchX - X < -3) {
// console.log('右滑动');
let enableScroll = true;
tempAniamtion = this.animation.left('0px').step();
this.setData({
deleteAnimation: tempAniamtion.export(),
enableScroll,
AnimatingSku: '',
});
}
},
navtoProductDetailPage: function (event) {
let data = event.currentTarget.dataset.item;
tt.navigateTo({
url: '/pages/goodsDetail/goodsDetail?productSkn=' + data.product_skn + '&page_name=' + this.data.current_page_name,
});
},
navToNewArriaval: function () {
tt.switchTab({
url: '/pages/index/index' + '?page_name=' + this.data.current_page_name + '&page_param=' + '',
})
},
//删除喜欢的商品
deleteGoodsRequest: function (event) {
var goodsItem = event.currentTarget.dataset.type;
var category_id = goodsItem.category_id;
var product_id = goodsItem.product_id;
let param = {
method: 'app.favorite.cancel',
fav_id: product_id,
type: 'product'
};
GET(API_HOST, param)
.then(data => {
let total = this.data.total > 0 ? this.data.total - 1 : 0;
if (total == 0) {
this.setData({
isFetching: false,
total: total,
categoryList: null,
productList: null,
currentProductList: null
});
return;
}
var productListTemp = [];
let productLists = this.data.productList;
let categoryLists = this.data.categoryList;
var index = 0;
productLists.forEach((item, i) => {
if (product_id == item.product_id) {
index = i;
return;
}
});
productLists.splice(index, 1); //从数组中移除选中的商品
if (this.data.currentCategoryId == '') { //全部
productListTemp = productLists;
} else {
productLists.forEach((item) => {
if (category_id == item.category_id) {
productListTemp.push(item);
}
});
if (productListTemp.length == 0) {
productListTemp = productLists;
}
}
var isExist = false;
productListTemp.forEach((item) => {
if (category_id == item.category_id) {
isExist = true; //分类还存在
return;
}
});
if (!isExist) {
var index = 0;
categoryLists.forEach((item, i) => {
if (item.category_id == category_id) {
index = i;
return;
}
});
categoryLists.splice(index, 1); //从数组中移除选中的分类id
this.setData({
currentCategoryId: ''
});
}
this.setData({
isFetching: false,
total: total,
categoryList: categoryLists,
productList: productLists,
currentProductList: productListTemp,
});
that.wetoast.toast({
title: data.message,
titleClassName: 'wetoast-title',
duration: 1000
});
})
.catch(error => {
that.wetoast.toast({
title: error.code + error.message,
titleClassName: 'wetoast-title',
duration: 1000
});
});
},
/**
* 计算滑动角度
* @param {Object} start 起点坐标
* @param {Object} end 终点坐标
*/
angle: function (start, end) {
var _X = end.X - start.X,
_Y = end.Y - start.Y
return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
},
})