category.js
11.4 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
// category.js
import { API_HOST, SERVICE_HOST } from '../../libs/config';
import { GET, POST } from '../../libs/request';
import objectAssign from '../../vendors/object-assign';
import { shouldDiscardTap } from '../../utils/util';
import {
logEvent,
YB_CATEGORY_FL,
YB_MAIN_TAB_C,
YB_PAGE_OPEN_L
} from '../../libs/analytics.js'
//获取应用实例
let app = getApp();
const windowWidth = app.globalData.systemInfo.windowWidth;
const windowHeight = app.globalData.systemInfo.windowHeight;
const scrollHeight = windowHeight - 44;
let PV_ID = new Date().getTime() + '';
Page({
/**
* 页面的初始数据
*/
data: {
scrollHeight,
lastTapTimeStamp: 0,
isFetching: false,
categoryError: null,
currentChannelId: "1",
categoryList: null,
currentCateId: 0,
currentCateValue: '',
currentSubCateData: null,
cacheSubCateData: {},
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
let channelId = options.channelId;
if (!channelId) {
channelId = app.globalData.cid;
}
this.setData({ currentChannelId: channelId });
this.fetchCategoryList();
},
onShow:function(options){
// let cid = app.globalData.cid
// if (cid != this.data.currentChannelId) {
// this.setData({ currentChannelId: cid });
// if (this.data.categoryList) {
// this.updateChannelData(cid);
// }
// }
logEvent(YB_MAIN_TAB_C, { TAB_ID: 3 });
var pages = getCurrentPages()
var currentPage = pages[pages.length - 1]
var url = currentPage.route
let params = {
PAGE_NAME: 'category',
PAGE_PARAM: '',
FROM_PAGE_NAME: '',
FROM_PAGE_PARAM: '',
PV_ID: PV_ID,
PAGE_PATH: url
};
logEvent(YB_PAGE_OPEN_L, params);
},
/**
* 获取品类列表
*/
fetchCategoryList: function () {
let param = {
method: 'app.sort.get',
yh_channel: this.data.currentChannelId,
fromPage: 'aFP_Category'
}
this.setData({
isFetching: true,
});
GET(API_HOST, param)
.then(data => {
if (!data || !data.code || data.code != 200) {
this.setData({
isFetching: false,
categoryError: { code: data.code, message: data.message },
});
return;
}
//处理返回结果,1: boy 2: girl 3: kids 解析一级分类
let category = data.data;
let categoryList = {
"1": category.boy,
"2": category.girl,
"3": category.kids,
};
this.setData({
isFetching: false,
categoryList: categoryList,
}, () => {
this.updateChannelData(this.data.currentChannelId);
});
})
.catch(error => {
this.setData({
isFetching: false,
categoryError: error,
});
});
},
/**
* 获取子分类的KEY值
**/
getSubDetailCacheKey: function (channelId, categoryId) {
let key = "CHA_" + channelId + "_CAT_" + categoryId;
return key;
},
/**
* 获取品类一级分类的数据信息
*/
getSubCategoryInfo: function (categoryId) {
let cacheSubCateData = this.data.cacheSubCateData;
//检查缓存是否存在数据,如果不存在则获取
let key = this.getSubDetailCacheKey(this.data.currentChannelId, categoryId);
let categoryData = cacheSubCateData ? cacheSubCateData[key] : "";
//命中,则将缓存数据赋给当前分类数据
if (categoryData) {
this.setData({
currentSubCateData: categoryData,
});
}
//未命中,访问网络数据
else {
this.fetchSubCategoryInfoFromServer(categoryId);
}
},
/**
* 从服务器获取一级分类数据信息
*/
fetchSubCategoryInfoFromServer: function (categoryId) {
let param = {
method: 'app.sort.fromBigData',
channel_id: this.data.currentChannelId,
msort: categoryId,
contentCode: 'daaa8b1a5103a30419ebd79c06e6feac',
fromPage: 'aFP_Category'
}
this.setData({
isFetching: true,
});
GET(API_HOST, param)
.then(data => {
if (!data || !data.code || data.code != 200) {
this.setData({
isFetching: false,
contentError: { code: data.code, message: data.message },
});
return;
}
//处理返回的数据,主要是替换图片的宽高信息
let subCategoryData = data.data;
//处理分类数据
if (subCategoryData.sortInfo){
subCategoryData.sortInfo.map((item, index) => {
let default_images = item.default_images ? item.default_images.replace(/{width}/g, 144).replace(/{height}/g, 192).replace('{mode}', 2) :'';
item.default_images = default_images;
});
}
//处理热门推荐数据
if (subCategoryData.salesInfo) {
subCategoryData.salesInfo.map((item, index) => {
let default_images = item.default_images ? item.default_images.replace(/{width}/g, 144).replace(/{height}/g, 192).replace('{mode}', 2) :'';
item.default_images = default_images;
});
}
//处理热门推荐数据
if (subCategoryData.brandInfo) {
subCategoryData.brandInfo.map((item, index) => {
let brand_ico = item.brand_ico ? item.brand_ico.replace(/{width}/g, 210).replace(/{height}/g, 87).replace('{mode}', 2) : '';
item.brand_ico = brand_ico;
});
}
//记录缓存数据
let key = this.getSubDetailCacheKey(this.data.currentChannelId, categoryId);
let cacheSubCateData = this.data.cacheSubCateData;
cacheSubCateData[key] = subCategoryData;
//将分类列表赋值给data
this.setData({
isFetching: false,
currentSubCateData: data.data,
cacheSubCateData: cacheSubCateData,
});
})
.catch(error => {
this.setData({
isFetching: false,
contentError: error,
});
});
},
/**
* 用户点击左边分类
**/
onCategoryItemSelected: function (event) {
let currentCateData = event.currentTarget.dataset;
let currentCateId = currentCateData.id
let param = {
L1_CATE_ID: currentCateId,
}
logEvent(YB_CATEGORY_FL,param);
let currentChannel = this.data.categoryList[this.data.currentChannelId];
let cateData = currentChannel && currentChannel[currentCateData.index];
if (cateData && cateData.sub && cateData.sub.length > 0) {
this.setData({ currentCateId: currentCateId, currentCateValue: currentCateData.name });
this.getSubCategoryInfo(currentCateId);
} else {
let categoryName = '全部' + cateData.category_name;
let categoryId = cateData.category_id;
let parentCategoryId = cateData.parent_id;
let gender = this.data.currentChannelId == '1' ? '1,3' : '2,3';
let sort = cateData.relation_parameter && cateData.relation_parameter.sort ? cateData.relation_parameter.sort : ''
//拼接url
let queryString = "?gender=" + gender + "&sort=" + sort + "&title=" + categoryName +
"&categoryId=" + parentCategoryId + "&subCategoryId=" + categoryId;
wx.navigateTo({
url: '../goodsList/goodsList' + queryString
});
}
},
/**
* 点击热门推荐
**/
onHotRecommendItemTapped: function(event){
if (shouldDiscardTap(event.timeStamp, this.data.lastTapTimeStamp)) {
return;
}
this.setData({ lastTapTimeStamp: event.timeStamp });
let url = event.currentTarget.dataset.url;
if (!url) {
return;
}
let start = url.indexOf('?');
if (start == -1) {
return;
}
let queryString = url.substring(start);
if (!queryString) {
return;
}
let c2id = event.currentTarget.dataset.cate_id;
let c1id = this.data.currentCateId;
let F_ID = event.currentTarget.dataset.f_id;
let param={
F_ID,
L1_CATE_ID: c1id,
L2_CATE_ID:c2id
}
logEvent(YB_CATEGORY_FL,param);
wx.navigateTo({
url: '../goodsList/goodsList' + queryString
});
},
/**
* 点击二级分类信息
**/
onCategoryItemTapped: function (event) {
if (shouldDiscardTap(event.timeStamp, this.data.lastTapTimeStamp)) {
return;
}
this.setData({ lastTapTimeStamp: event.timeStamp });
let data = event.currentTarget.dataset.data;
let categoryName = data.category_name;
let categoryId = data.category_id;
let parentCategoryId = data.parent_id;
let firstProductSkn = data.product_skn;
let gender = this.data.currentChannelId == '1' ? '1,3' : '2,3';
let sort = data.relation_parameter && data.relation_parameter.sort ? data.relation_parameter.sort : ''
let c1id = this.data.currentCateId;
let param = {
F_ID:'1002',
L1_CATE_ID: c1id,
L2_CATE_ID: categoryId
}
logEvent(YB_CATEGORY_FL, param);
//拼接url
let queryString = "?gender=" + gender + "&sort=" + sort + "&title=" + categoryName +
"&categoryId=" + parentCategoryId + "&subCategoryId=" + categoryId + "&firstProductSkn=" + firstProductSkn;
wx.navigateTo({
url: '../goodsList/goodsList' + queryString
});
},
/**
* 点击二级分类MORE
**/
onCategoryMoreTapped: function (event) {
let currentCateId = event.currentTarget.dataset.id;
let currentCategory = null;
this.data.categoryList[this.data.currentChannelId].map((item, index) => {
let category_id = item.category_id;
if (category_id == currentCateId){
currentCategory = item;
}
});
if (!currentCategory){
return;
}
let categoryName = currentCategory.category_name;
let categoryId = currentCategory.category_id;
let gender = this.data.currentChannelId == '1' ? '1,3' : '2,3';
let sort = currentCategory.relation_parameter && currentCategory.relation_parameter.sort ? currentCategory.relation_parameter.sort : ''
let c1id = this.data.currentCateId;
let param = {
F_ID: '1002',
L1_CATE_ID: c1id,
L2_CATE_ID: "0"
}
logEvent(YB_CATEGORY_FL, param);
//拼接url
let queryString = "?gender=" + gender + "&sort=" + sort + "&title=" + categoryName + "&categoryId=" + categoryId + "&subCategoryId=";
wx.navigateTo({
url: '../goodsList/goodsList' + queryString
});
},
/**
* 点击热门品牌
**/
onHotBrandItemTapped: function(event) {
if (shouldDiscardTap(event.timeStamp, this.data.lastTapTimeStamp)) {
return;
}
this.setData({ lastTapTimeStamp: event.timeStamp });
let data = event.currentTarget.dataset.data;
let c1id = this.data.currentCateId;
let param = {
F_ID: '1003',
L1_CATE_ID: c1id,
L2_CATE_ID: data.id
}
logEvent(YB_CATEGORY_FL, param);
wx.navigateTo({
url: '../goodsList/brandStore?shopId=' + data.shop_id + '&shopName=' + data.brand_name
});
},
switchChannel: function(event) {
let currentChannelId = event.currentTarget.dataset.channel;
this.updateChannelData(currentChannelId);
},
updateChannelData: function (currentChannelId) {
if (!this.data.categoryList) {
return;
}
let currentCateId = this.data.categoryList[currentChannelId][0].category_id;
let currentCateValue = this.data.categoryList[currentChannelId][0].category_name;
// 获取品类一级分类的数据信息
this.getSubCategoryInfo(currentCateId);
this.setData({
currentChannelId,
currentCateId,
currentCateValue,
});
}
})