category.js
10.7 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
import api from '../../common/api.js';
import { shouldDiscardTap } from '../../utils/util';
const app = getApp()
Page({
data: {
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();
},
/**
* 获取品类列表
*/
fetchCategoryList: function () {
let param = {
method: 'app.sort.get',
yh_channel: this.data.currentChannelId,
fromPage: 'aFP_Category'
}
this.setData({
isFetching: true,
});
api.get({data: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
}, () => {
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,
});
api.get({data: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;
tt.navigateTo({
url: '../productList/index' + 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);
tt.navigateTo({
url: '../productList/index' + queryString
});
},
/**
* 点击全部xxx
**/
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;
tt.navigateTo({
url: '../productList/index' + 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=";
tt.navigateTo({
url: '../productList/index' + queryString
});
},
/**
* 点击品牌列表页
**/
onBrandItemSelected: function (event) {
tt.navigateTo({
url: './brands' + '?currentChannelId=' + this.data.currentChannelId
});
},
/**
* 点击热门品牌
**/
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);
if (data && data.shop_id) {
tt.navigateTo({
url: '../brandStore/brandStore?shopId=' + data.shop_id + '&shopName=' + data.brand_name
});
} else {
tt.navigateTo({
url: '../brandStore/brand?brandId=' + data.id + '&brandName=' + data.brand_name
});
}
},
switchChannel: function(event) {
let currentChannelId = event.detail.currentTarget.dataset.channel;
this.updateChannelData(currentChannelId);
},
updateChannelData: function (currentChannelId) {
if (!this.data.categoryList || typeof(currentChannelId) == "undefined") {
return;
}
let currentChannel = this.data.categoryList[currentChannelId];
let currentCateId = currentChannel[0].category_id;
let currentCateValue = currentChannel[0].category_name;
// 获取品类一级分类的数据信息
this.getSubCategoryInfo(currentCateId);
this.setData({
currentChannelId,
currentCateId,
currentCateValue,
});
}
})