search.js
15.5 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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
import {API_HOST, SERVICE_HOST} from '../../libs/config';
import {GET, POST} from '../../libs/request';
import {parseBrandListData, cancelFilterSelectState} from '../../utils/productList';
import {getChannelCode, getGenderCode, getRecPosCode, getRecommandContentCode} from '../../utils/home';
import objectAssign from '../../vendors/object-assign';
import {
logEvent,
YB_PAGE_OPEN_L,
} from '../../libs/analytics.js'
//获取应用实例
let app = getApp()
const windowWidth = app.globalData.systemInfo.windowWidth;
const windowHeight = app.globalData.systemInfo.windowHeight;
const DEVICE_WIDTH_RATIO = windowWidth / 320;
let listWidth = Math.ceil(137.5 * DEVICE_WIDTH_RATIO);
let listHeight = Math.ceil(254 * DEVICE_WIDTH_RATIO);
const IMAGE_WIDTH = 145;
const IMAGE_HEIGHT = 193;
const IMAGE_RATIO = IMAGE_HEIGHT / IMAGE_WIDTH;
let listImageTop = 31;
let listImageWidth = listWidth;
let listImageHeight = Math.ceil(listWidth * IMAGE_RATIO);
let listMarginHorizontal = (windowWidth - listWidth * 2) / 3;
let PV_ID = new Date().getTime() + '';
Page({
data:{
windowWidth,
windowHeight,
listWidth,
listHeight,
listImageWidth,
listImageHeight,
listImageTop,
listMarginHorizontal,
status: 0, // 0-search keyword, 1-fuzzy search, 2-product list
keyword: '',
order: '',
from_page_name: '',
from_page_param: '',
current_page_name: 'search',
current_page_param: '',
isChangedQuery: false,
terms_suggestion: [],
suggestion_query: '',
hotKeyword: {
isLoading: false,
error: null,
data: null,
},
fuzzySearch: {
isLoading: false,
error: null,
data: null,
},
searchHistory: [],
list: {
isLoading: false,
error: null,
data: null,
pageSize: 60,
currentPage: 0,
pageCount: 0,
total: 0,
endReached: false,
},
filter: {
item1: {
key: 'item1',
name: '默认',
asc: '',
isRadio: true,
selected: true,
},
item2: {
key: 'item2',
name: '新品',
asc: 's_t_desc',
isRadio: true,
selected: false,
},
item3: {
key: 'item3',
name: '价格',
defaults: 'asc',
asc: 's_p_asc',
desc: 's_p_desc',
isRadio: false,
isAsc: true,
selected: false,
},
item4: {
key: 'item4',
name: '折扣',
defaults: 'desc',
asc: 'p_d_desc',
desc: 'p_d_asc',
isRadio: false,
isAsc: false,
selected: false,
},
},
filterGenderItem: {
key: 'filterGenderItem',
name: '男生',
isRadio: true,
selected: true,
hiddenGenderSelectView: true,
},
genderFilter: {
item1: {
key: 'item1',
name: '男生/BOYS',
selected: true,
value: '1',
title: '男生',
},
item2: {
key: 'item2',
name: '女生/GIRLS',
selected: false,
value: '2',
title: '女生',
}
},
},
resetChannelFilterInfo: function (gender) {
let filterGenderItem = this.data.filterGenderItem;
let genderFilter = this.data.genderFilter;
filterGenderItem.name = gender;
for (let itemKey in genderFilter) {
if (genderFilter.hasOwnProperty(itemKey)) {
if (genderFilter[itemKey].title == gender) {
genderFilter[itemKey].selected = true;
} else {
genderFilter[itemKey].selected = false;
}
}
}
this.setData({ filterGenderItem, genderFilter });
},
onLoad:function(options){
// 生命周期函数--监听页面加载
var that = this
let from_page_name = options.page_name ? options.page_name : '';
let from_page_param = options.page_param ? options.page_param : '';
this.setData({from_page_name, from_page_param});
var pages = getCurrentPages()
var currentPage = pages[pages.length - 1]
var url = currentPage.route
let params = {
PAGE_NAME: that.data.current_page_name,
PAGE_PARAM: that.data.current_page_param,
FROM_PAGE_NAME: that.data.from_page_name,
FROM_PAGE_PARAM: that.data.from_page_param,
PV_ID: PV_ID,
PAGE_PATH: url
};
logEvent(YB_PAGE_OPEN_L, params);
this.fetchHotKeyword();
},
onReady:function(){
// 生命周期函数--监听页面初次渲染完成
},
onShow:function(){
// 生命周期函数--监听页面显示
},
onHide:function(){
// 生命周期函数--监听页面隐藏
},
onUnload:function(){
// 生命周期函数--监听页面卸载
},
onPullDownRefresh: function() {
// 页面相关事件处理函数--监听用户下拉动作
var that = this
let params = {
PAGE_NAME: that.data.current_page_name,
PAGE_PARAM: that.data.current_page_param,
FROM_PAGE_NAME: that.data.from_page_name,
FROM_PAGE_PARAM: that.data.from_page_param,
PV_ID: PV_ID,
};
logEvent(YB_PAGE_OPEN_L, params);
},
onReachBottom: function() {
// 页面上拉触底事件的处理函数
this.fetchListData();
},
//搜索框文字改变
inputChanged: function(event) {
let keyword = event.detail.value;
let status = keyword ? 1 : 0;
this.setData({status, keyword});
this.fuzzySearchList();
},
//搜索框获取焦点焦点
focusChanged: function(event) {
let status = this.data.keyword ? 1 : 0;
this.setData({status});
this.fuzzySearchList();
},
//搜索框键盘点击确定
inputConfirmed: function(event) {
let keyword = event.detail.value;
let newList = objectAssign(this.data.list, {currentPage: 0, pageCount: 0, total: 0, endReached: false,});
let newFilter = {
item1: objectAssign(this.data.filter.item1, {selected: true}),
item2: objectAssign(this.data.filter.item2, {selected: false}),
item3: objectAssign(this.data.filter.item3, { selected: false }),
item4: objectAssign(this.data.filter.item4, { selected: false }),
};
this.setData({status: 2, keyword, list: newList, filter: newFilter, order: ''});
let paramters = {
PAGE_NAME: 'searchResult',
PAGE_PARAM: keyword ? keyword : '',
FROM_PAGE_NAME: this.data.from_page_name,
FROM_PAGE_PARAM: this.data.from_page_param,
PV_ID: PV_ID,
};
logEvent(YB_PAGE_OPEN_L, paramters);
this.fetchListData();
},
//热门搜索点击
hotKeywordItemTapped: function(event) {
let keyword = event.currentTarget.dataset.keyword;
let newList = objectAssign(this.data.list, {currentPage: 0, pageCount: 0, total: 0, endReached: false,});
let newFilter = {
item1: objectAssign(this.data.filter.item1, {selected: true}),
item2: objectAssign(this.data.filter.item2, {selected: false}),
item3: objectAssign(this.data.filter.item3, { selected: false }),
item4: objectAssign(this.data.filter.item4, { selected: false }),
};
this.setData({status: 2, keyword, list: newList, filter: newFilter, order: ''});
let paramters = {
PAGE_NAME: 'searchResult',
PAGE_PARAM: keyword ? keyword : '',
FROM_PAGE_NAME: this.data.from_page_name,
FROM_PAGE_PARAM: this.data.from_page_param,
PV_ID: PV_ID,
};
logEvent(YB_PAGE_OPEN_L, paramters);
this.fetchListData();
},
//点击模糊搜索出来的条目
fuzzySearchItemTapped: function(event) {
let keyword = event.currentTarget.dataset.keyword;
let newList = objectAssign(this.data.list, {currentPage: 0, pageCount: 0, total: 0, endReached: false,});
let newFilter = {
item1: objectAssign(this.data.filter.item1, {selected: true}),
item2: objectAssign(this.data.filter.item2, {selected: false}),
item3: objectAssign(this.data.filter.item3, { selected: false }),
item4: objectAssign(this.data.filter.item4, { selected: false }),
};
this.setData({status: 2, keyword, list: newList, filter: newFilter, order: ''});
let paramters = {
PAGE_NAME: 'searchResult',
PAGE_PARAM: keyword ? keyword : '',
FROM_PAGE_NAME: this.data.from_page_name,
FROM_PAGE_PARAM: this.data.from_page_param,
PV_ID: PV_ID,
};
logEvent(YB_PAGE_OPEN_L, paramters);
this.fetchListData();
},
//取消登录
cancelSearch: function(event) {
wx.navigateBack({
delta: 1
});
},
//点击筛选
//点击筛选
filterTapped: function (event) {
let data = event.currentTarget.dataset;
let key = data.key;
let item = this.data.filter[key];
// 单选
if (item.isRadio) {
// 已选中 结束
if (item.selected) {
return;
}
// 选中当前filter
item.selected = true;
// 遍历所有filter,设置非当前filter为不选中
let newFilters = cancelFilterSelectState(this.data.filter, key);
newFilters[key] = item;
let newList = objectAssign(this.data.list, { currentPage: 0, pageCount: 0, total: 0, endReached: false, });
this.setData({ filter: newFilters, order: item.asc, list: newList });
this.fetchListData();
// 多选
} else {
// 已选中
if (item.selected) {
// 修改当前filter排序
item.isAsc = !item.isAsc;
let order = item.isAsc ? item.asc : item.desc;
let newFilters = this.data.filter;
newFilters[key] = item;
let newList = objectAssign(this.data.list, { currentPage: 0, pageCount: 0, total: 0, endReached: false, });
this.setData({ order, filter: newFilters, list: newList });
this.fetchListData();
// 未选中
} else {
// 选中当前filter
item.selected = true;
// 读取当前filter默认排序
let defaultKey = item.defaults;
let order = item[defaultKey];
item.isAsc = defaultKey == 'asc';
// 遍历所有filter,设置非当前filter为不选中
let newFilters = cancelFilterSelectState(this.data.filter, key);
newFilters[key] = item;
let newList = objectAssign(this.data.list, { currentPage: 0, pageCount: 0, total: 0, endReached: false, });
this.setData({ order, filter: newFilters, list: newList });
this.fetchListData();
}
}
},
//商品点击进入商品详情
productCellTapped: function(event) {
let data = event.currentTarget.dataset;
let productSkn = data.productSkn;
wx.navigateTo({
url: '../goodsDetail/goodsDetail?productSkn=' + productSkn + '&page_name=' + this.data.current_page_name + '&page_param=' + this.data.current_page_param
});
},
//查询热搜词
fetchHotKeyword: function() {
let param = {
method: 'app.search.getTerms',
}
let hotKeyword = objectAssign(this.data.hotKeyword, {isLoading: true});
this.setData({hotKeyword});
GET(API_HOST, param)
.then(data => {
if (!data || !data.code || data.code != 200) {
let hotKeyword = objectAssign(this.data.hotKeyword, {isLoading: false, error: {code: data.code, message: data.message}});
this.setData({hotKeyword});
return;
}
let hotKeyword = objectAssign(this.data.hotKeyword, {isLoading: false, error: null, data: data.data.hotTerms});
this.setData({hotKeyword});
})
.catch(error => {
let hotKeyword = objectAssign(this.data.hotKeyword, {isLoading: false, error});
this.setData({hotKeyword});
});
},
//查询模糊搜索条目列表
fuzzySearchList: function() {
let param = {
method: 'app.search.fuzzy',
keyword: this.data.keyword,
}
let fuzzySearch = objectAssign(this.data.fuzzySearch, {isLoading: true});
this.setData({fuzzySearch});
GET(API_HOST, param)
.then(data => {
if (!data || !data.code || data.code != 200) {
let fuzzySearch = objectAssign(this.data.fuzzySearch, {isLoading: false, error: {code: data.code, message: data.message}});
this.setData({fuzzySearch});
return;
}
let fuzzySearch = objectAssign(this.data.fuzzySearch, {isLoading: false, error: null, data: data.data});
this.setData({fuzzySearch});
})
.catch(error => {
let fuzzySearch = objectAssign(this.data.fuzzySearch, {isLoading: false, error});
this.setData({fuzzySearch});
});
},
//根据关键词,查询商品列表
fetchListData: function() {
if (this.data.list.isLoading || this.data.list.endReached) {
return;
}
let page = this.data.list.currentPage + 1;
let param = {
method: 'app.search.li',
query: this.data.keyword,
new_filter: 1,
order: this.data.order,
page,
limit: this.data.list.pageSize,gender: this.data.gender,
gender: this.data.filterGenderItem.name == '男生'?'1,3':'2,3',
}
let list = objectAssign(this.data.list, {isLoading: true,});
this.setData({list});
GET(API_HOST, param)
.then(json => {
if (!json || !json.code || json.code != 200) {
let list = objectAssign(this.data.list, {isLoading: false, error: {code: json.code, message: json.message}});
this.setData({list});
return;
}
json = json.data;
let currentPage = json && json.page ? json.page : 1;
let pageCount = json && json.page_total ? json.page_total : 0;
let total = json && json.total ? json.total : 0;
let endReached = currentPage == pageCount;
let isChangedQuery = json && json.isChangedQuery && json.isChangedQuery=='Y' ? true : false;
let terms_suggestion = json && json.suggestion && json.suggestion.terms_suggestion ? json.suggestion.terms_suggestion : [];
let suggestion_query = json && json.suggestion_query ? json.suggestion_query : '';
let data = json.product_list;
data = parseBrandListData(data, listImageWidth, listImageHeight);
if (currentPage > 1) {
let oldList = this.data.list.data;
data = [...oldList, ...data];
}
let newList = {
isLoading: false,
data,
currentPage,
pageCount,
total,
endReached,
};
let list = objectAssign(this.data.list, newList);
this.setData({
list,
isChangedQuery,
terms_suggestion,
suggestion_query,
});
})
.catch(error => {
let list = objectAssign(this.data.list, {isLoading: false, error,});
this.setData({list});
});
},
hiddenGenderFilter: function (event) {
let filterGenderItem = this.data.filterGenderItem;
filterGenderItem.hiddenGenderSelectView = true;
this.setData({ filterGenderItem });
},
filterGenderItemTapped: function (event) {
let filterGenderItem = this.data.filterGenderItem;
filterGenderItem.hiddenGenderSelectView = !filterGenderItem.hiddenGenderSelectView;
this.setData({ filterGenderItem });
},
maskTouchStart: function (event) {
this.hiddenGenderFilter();
},
genderFilterTapped: function (event) {
let data = event.currentTarget.dataset;
let key = data.key;
let item = this.data.genderFilter[key];
if (item.selected) {
this.hiddenGenderFilter();
return;
}
let filterGenderItem = this.data.filterGenderItem;
filterGenderItem.name = item.title;
// 选中当前filter
item.selected = true;
// 遍历所有filter,设置非当前filter为不选中
let newFilters = cancelFilterSelectState(this.data.genderFilter, key);
newFilters[key] = item;
let newList = objectAssign(this.data.list, { currentPage: 0, pageCount: 0, total: 0, endReached: false, });
this.setData({ filterGenderItem, genderFilter: newFilters, list: newList, gender: item.value });
this.hiddenGenderFilter();
this.fetchListData();
}
})