search.js
20.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
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
var t = function (t) {
return t && t.__esModule ? t : {
default: t
};
}(require("../../../../utils/LoadMore.js")), ut = require("../../../../utils/util.js"),
a = getApp(), e = new t.default(), rq = a.request, oo = a.globalData.setting;
const com_fil = require("../com_screen.js");
Page({
data: {
url: a.globalData.setting.imghost,
resourceUrl: a.globalData.setting.resourceUrl,
currentPage: 1,
requestData: null,
allData: null,
openFilterModal: !1,
openSearchModal: !1,
baseUrl: "/api/weshop/goods/page?isnewwhere=1",
requestUrl: "",
old_req_url:'',
//hotWords: [ [ "手机", "小米", "iphone" ], [ "三星", "华为", "冰箱" ] ]
hotWords: null,
is_no_plus: 1,
tabname: "sort", //排序的字段
adname: "asc", //升降的字段
rq_data: null,
searchRecord: [],
keyword: '',
is_his: 0,
hiddenClear: true,
fliter_flag: false,
more_sort_arr:[
{name:'综合',value:'sort'},
{name:'新品',value:'on_time'},
{name:'评论',value:'comment_count'},
],
fil_cate_state:0,
fil_nation_state:0,
fil_brand_state:0,
fil_price_state:0,
},
onLoad: function (t) {
this.data.rq_data = t;
//群id
if(t && t.groupchat_id){
getApp().globalData.groupchat_id=t.groupchat_id
}
if (t.o) {
this.data.rq_data.o = JSON.parse(t.o);
};
if(t.o) {
var req = t.o;
if(req.cat_id){
var baseUrl= "/api/weshop/goods/page?isnewwhere=1&cat_id="+req.cat_id
this.setData({ baseUrl: baseUrl });
}
if(req.nation_id){
var baseUrl= "/api/weshop/goods/page?isnewwhere=1&nation_id="+req.nation_id
this.setData({ baseUrl: baseUrl });
}
if(req.brand_id){
var baseUrl= "/api/weshop/goods/page?isnewwhere=1&brand_id="+req.brand_id
this.setData({ baseUrl: baseUrl });
}
}else{
var baseUrl= "/api/weshop/goods/page?isnewwhere=1"
this.setData({ baseUrl: baseUrl });
}
//接受有没有导购的参数
var first_leader = t.first_leader;
if (first_leader) {
getApp().globalData.first_leader = first_leader;
//调用接口判断是不是会员
getApp().request.promiseGet("/api/weshop/shoppingGuide/get/" + oo.stoid + "/" + first_leader, {}).then(res => {
if (res.data.code == 0) {
getApp().globalData.guide_id = res.data.data.id;
getApp().globalData.guide_pick_id= res.data.data.pickup_id
}
})
}
//调用接口判断商家plus有没有过期
rq.promiseGet("/store/storemoduleendtime/page?store_id=" + oo.stoid + "&type=3", {}).then(res => {
if (res.data.code == 0) {
var arr = res.data.data.pageData;
if (arr.length > 0) {
var item = arr[0];
if (item.is_sy == 0) {
var now = Date.parse(new Date()); now = now / 1000;
if (item.end_time < now) {
th.setData({ is_no_plus: 0 })
}
}
}
}
})
var th = this;
a.getConfig2(function (rs) {
var arr = new Array(), arr2 = new Array();
if (rs.hot_keywords != null && ut.trim(rs.hot_keywords) != "") {
var arr1 = rs.hot_keywords.split('|');
for (var i = 0; i < arr1.length; i++) {
if (i % 5 == 0 && i != 0) {
arr.push(arr2);
arr2 = [];
} else {
arr2.push(arr1[i]);
}
}
if (arr2.length > 0) arr.push(arr2);
th.setData({ hotWords: arr });
}
//计算等级价相关
var swithc_list = rs.switch_list;
var sw_arr = JSON.parse(swithc_list);
//---如果后台又开等级卡的开关---
ut.get_plus_name_price(sw_arr,th);
if(sw_arr.is_retail_price){
th.setData({is_retail_price:1});
}
})
//e.init(this, "", "requestData");
var url = this.data.baseUrl;
//扫一扫过来,显示搜索的内容
var s_key = t.s_key;
if (s_key) {
s_key = s_key.trim();
this.search(s_key); this.openSearchModal();
return;
}
if (0 != t.brand_id && t.brand_id != undefined) { url += "&brand_id=" + t.brand_id; }
if (0 != t.nation_id && t.nation_id != undefined) { url += "&nation_id=" + t.nation_id; }
if (0 != t.max_price && t.max_price != undefined) { url += "&min_pirce=" + t.min_pirce + "&max_price=" + t.max_price; }
if (0 != t.ladder_id && t.ladder_id != undefined) {
url += "&ladder_id=" + t.ladder_id;
this.setData({
baseUrl: url,
})
}
if (url != this.data.baseUrl) return this.requestSearch(url);
this.openSearchModal();
// 获取历史搜索记录
this.getHistorySearch();
},
onShow: function () {
getApp().check_can_share();
},
changeTab: function (t) {
var ord = t.currentTarget.dataset.href;
var ad = t.currentTarget.dataset.ad;
var url = this.data.requestUrl;
if (this.data.tabname != ord) {
this.setData({ tabname: ord, adname: "desc" });
} else {
ad = ad == "desc" ? "asc" : "desc";
this.setData({ adname: ad });
}
this.resetData(), this.requestSearch(url);
},
//-----------真的调用地址进行搜索------------
requestSearch: function (t) {
// console.log('t===>', t);
if (this.data.loading) return false;
this.data.loading = 1;
var e = this, th = e;
if (getApp().globalData.userInfo) {
t += "&user_id=" + getApp().globalData.userInfo.user_id;
}
this.data.requestUrl = t;
var user_id = getApp().globalData.user_id;
if (!user_id) user_id = 0;
let data = {
is_mainshow: 1,
isonsale: 1,
store_id: oo.stoid,
orderField: e.data.tabname,
orderType: e.data.adname,
page: e.data.currentPage,
};
let o = this.data.rq_data.o;
if (o) {
if (o.cat_id) data.cat_id = o.cat_id;
if (o.pid && o.pid != 'three') data.parent_id = o.pid;
if (o.nation_id) data.nation_id = o.nation_id;
if (o.brand_id) data.brand_id = o.brand_id;
};
getApp().request.promiseGet(t, { data: data }).then(async res => {
th.data.loading = 0;
if (ut.ajax_ok(res)) {
if (!e.data.requestData) {
// e.data.requestData=[];
e.setData({
requestData: [],
});
};
for (let i in res.data.data.pageData) {
let item = res.data.data.pageData[i];
item.original_img = th.data.url + item.original_img;
/*--
var url="/api/weshop/activitylist/getGoodActInfo";
var req_data={
store_id:oo.stoid,goodsidlist:item.goods_id,is_detail:1,user_id:user_id
};
//获取商品的实际活动
await getApp().request.promiseGet(url, {data:req_data}).then(res=>{
if(res.data.code==0 && res.data.data && res.data.data.length==1){
item.prom_type=res.data.data[0].prom_type;
item.prom_id=res.data.data[0].act_id;
}
})--*/
var prom_type = item.prom_type; //0普通商品 1秒杀 6拼单 2团购 4积分购
var prom_id = item.prom_id;
var now = ut.gettimestamp();
var url = "";
switch (prom_type) {
case 1:
url = "/api/ms/flash_sale/getNew/" + oo.stoid + "/" + user_id + "/" + prom_id;
break;
case 2:
url = "/api/weshop/goods/groupBuy/getNewActInfo/" + oo.stoid + "/" + item.goods_id + "/" + prom_id+"/"+user_id;
break;
case 4:
url = "/api/weshop/integralbuy/get/" + oo.stoid + "/" + prom_id;
break;
case 6:
url = "/api/weshop/teamlist/get/" + oo.stoid + "/" + prom_id;
await getApp().request.promiseGet("/api/weshop/teamlist/pageteam/1", {
data: {
store_id: oo.stoid,
is_end: 0,
is_show: 1,
user_id: getApp().globalData.user_id,
pageSize: 1000,
}
}).then(pd => {
let pd_list = pd.data.data.pageData;
if (res.data.code == 0 && pd_list.length > 0) {
var flag = pd_list.some(pd => {
return pd.goods_id == item.goods_id
})
if (!flag) {
item.prom_type = 0;
item.prom_id = 0;
item.prom_price = null;
}
}
})
break;
case 8:
var presell_id = prom_id;
var url1 = "/api/weshop/marketing/marketingPresellList/list";
var rd = {
store_id: oo.stoid,
presell_id: presell_id,
goods_id: item.goods_id
}
//------获取预售从表----------
await getApp().request.promiseGet(url1, {
data: rd
}).then(res => {
if (res.data.code == 0 && res.data.data && res.data.data.length > 0) {
var arr = res.data.data[0];
item.prom_price = arr.presell_price;
item.sales_sum = arr.buy_goodnum + (arr.virtual_qty ? arr.virtual_qty : 0);
}
})
break;
default:
break;
};
if (url != null && url != '') {
await getApp().request.promiseGet(url, {}).then(async res => {
var prom = null;
if (res.data.code == 0 && res.data.data) {
prom = res.data.data;
}
if (prom != null && prom.is_end == 0 && prom.end_time > now && (prom.start_time < now || (prom_type!=2 && prom.show_time && prom.show_time < now))) {
item.prom_price = res.data.data.price;
if(prom_type==4){
item.prom_price= res.data.data.addmoney;
}
if (res.data.data.user_price) item.prom_price = res.data.data.user_price;
var vNum = prom.virtual_num ? prom.virtual_num : 0;
var vNum1 = prom.virtualNum ? prom.virtualNum : 0;
var vNum2 = prom.virtual ? prom.virtual : 0;
item.sales_sum = prom.buy_num + (vNum + vNum1 + vNum2);
} else {
item.prom_type = 0;
item.prom_id = 0;
item.prom_price = null;
}
})
};
if (!th.data.requestData) th.data.requestData = [];
th.data.requestData.push(item);
e.setData({ requestData: e.data.requestData });
}
}
e.closeSearchModal();
})
},
onReachBottom: function () {
console.log('触发上滑加载事件');
this.data.currentPage++
console.log('触发上滑加载事件currentPage', this.data.currentPage);
this.data.openSearchModal || e.canloadMore() && this.requestSearch(this.data.requestUrl);
},
openFilterModal: function () {
this.setData({
openFilterModal: !0
});
},
closeFilterModal: function () {
this.setData({
openFilterModal: !1
});
},
filterGoods: function (t) {
this.resetData(), this.requestSearch(t.currentTarget.dataset.href), this.closeFilterModal();
},
resetData: function () {
e.resetConfig(),
// this.data.requestData = null,
this.data.currentPage = 1;
this.setData({
requestData: null,
});
},
//---回复最初的设置---显示全部分类--
restoreData: function () {
this.setData({
requestData: this.data.allData,
});
this.data.currentPage = 2;
},
openSearchModal: function () {
this.setData({
openSearchModal: !0
});
},
closeSearchModal: function () {
this.setData({
openSearchModal: !1
});
},
submitSearch: function (t) {
var val = t.detail.value.word;
if (val == undefined) val = t.detail.value;
val = val ? val.trim() : '';
if (!val) {
a.showWarning("请输入关键词");
}
this.search(val);
},
//热搜关键字
searchHotWord: function (t) {
var word = t.currentTarget.dataset.word;
if (word) {
this.setData({
keyword: word,
hiddenClear: false,
});
};
this.historyRecord(word);
this.search(word);
},
//-----点击搜索按钮----
search: function (t) {
if ("string" != typeof t || "" == t) return a.showWarning("请输入关键词");
this.data.key_str = t;
this.resetData();
this.data.old_req_url=this.data.baseUrl + "&key_str=" + encodeURIComponent(t);
this.set_screen(this.data.baseUrl + "&key_str=" + encodeURIComponent(t))
this.requestSearch(this.data.baseUrl + "&key_str=" + encodeURIComponent(t));
this.historyRecord();
},
//---------分享配置--------
onShareAppMessage: function (e) {
getApp().globalData.no_clear = 1;
var curPage = this;
var pagePath = curPage.route; //当前页面url
if (pagePath.indexOf('/') != 0) {
pagePath = '/' + pagePath;
}
if (this.data.key_str) {
pagePath += "?s_key=" + this.data.key_str;
}
if (getApp().globalData.user_id) {
if (pagePath.indexOf("?") > 0) {
pagePath += "&first_leader=" + getApp().globalData.user_id;
} else {
pagePath += "?first_leader=" + getApp().globalData.user_id;
}
}
//群id分享
if (getApp().globalData.groupchat_id) {
pagePath+="&groupchat_id="+getApp().globalData.groupchat_id
}
// console.log("11-11"+pagePath);
return {
title: "商品搜索",
path: pagePath,
}
},
//---------图片失败,默认图片--------
bind_bnerr: function (e) {
var _errImg = e.target.dataset.errorimg;
var _errObj = {};
_errObj[_errImg] = this.data.url + "/miniapp/images/default_g_img.gif";
this.setData(_errObj) //注意这里的赋值方式,只是将数据列表中的此项图片路径值替换掉 ;
},
//--- 获取卡类列表 ---
getPlusCardType: function (func) {
var storid = oo.stoid;
var th = this;
getApp().request.promiseGet("/api/weshop/plus/vip/mem/bership/list?" + "storeId=" + storid, {}).then(res => {
var plusCard = res.data.data;
var arr = [1219, 2089, 3031];
var new_arr = new Array();
var card_name_map = new Map();
var user = getApp().globalData.userInfo;
for (var i = 0; i < plusCard.length; i++) {
if ((!user || user.card_field == null || user.card_field == "") && (plusCard[i].IsStopBuy == true)) {
continue;
}
var name = "card" + plusCard[i].CorrPrice.toLowerCase();
card_name_map.set(name, plusCard[i].CardName);
new_arr.push(plusCard[i]);
}
var ob = { "card_list": new_arr, "name_map": card_name_map };
func(ob);
})
},
getInput(e) {
let val = e.detail.value;
if (val) {
this.setData({
keyword: e.detail.value,
hiddenClear: false,
});
} else {
this.setData({
hiddenClear: true,
});
}
},
historyRecord(w) {
let keyword = w;
let self = this;
if (!keyword) {
keyword = this.data.key_str.trim();
}
let searchRecord = this.data.searchRecord;
if (!searchRecord || searchRecord.length == 0) searchRecord = wx.getStorageSync('searchRecord') || [];
if (keyword) {
var index = searchRecord.indexOf(keyword);
if (index > -1) {
// 已经存在该值
searchRecord.splice(index, 1);
searchRecord.unshift(keyword);
// 将历史记录添加到缓存中
wx.setStorage({
key: 'searchRecord',
data: searchRecord,
success: function (res) {
self.setData({
searchRecord,
})
}
})
return;
} else {
if (searchRecord.length >= 10) {
searchRecord.pop(); // 删除最早的一条记录
};
searchRecord.unshift(keyword);
// // 将历史记录添加到缓存中
wx.setStorage({
key: 'searchRecord',
data: searchRecord,
success: function (res) {
self.setData({
searchRecord,
})
}
})
};
};
},
closePoster() {
this.setData({
fliter_flag: false,
});
},
show_fliter() {
this.setData({
fliter_flag: true
})
},
fliter_close() {
this.setData({
fliter_flag: false,
})
},
// 获取历史搜索记录
getHistorySearch() {
this.setData({
searchRecord: wx.getStorageSync('searchRecord') || [] //若无存储则为空
});
},
// 清除历史搜素记录
clearSearchRecord() {
wx.clearStorageSync('searchRecord');
this.setData({
searchRecord: []
});
},
go_url: function (e) {
var url = e.currentTarget.dataset.url;
getApp().goto(url);
},
clearInput() {
this.setData({
keyword: '',
hiddenClear: true,
openSearchModal: true,
});
},
inputFocus(e) {
this.setData({
openSearchModal: true,
});
},
/*-- 和筛选相关的 --*/
bind_pk_sort(e){ com_fil.bind_pk_sort(e,this); },
set_screen(url){ com_fil.set_screen(url,this); },
filterGoods(e){ com_fil.filterGoods(e,this); },
open_more(e){ com_fil.open_more(e,this); },
clear_fil(){ com_fil.clear_fil(this); }
});