searchKeyword.js
9.42 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
// pages/reserve/search.js\
const map_key='SDEBZ-I74W4-QLGUX-XXYXU-HIR23-3QFBJ'
// 引入SDK核心类
var QQMapWX = require('../../utils/qqmap-wx-jssdk');
// 实例化API核心类
var qqmapsdk = new QQMapWX({
key:map_key // 必填
});
import util from '../../utils/util';
import App from '../../utils/api';
import PubSub from '../../utils/PubSub'
Page({
/**
* 页面的初始数据
*/
data: {
history:[],
suggestion:[],
hasSellerMore:false,
keyword:'',
sellerSplit:-1,
navHeight:100,
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
//filter=null&city={"code":"610100","pid":"610000","name":"西安市","status":"1","last_date":"1598949575"}&hows={"params":{"pnum":{"index":3,"val":4,"type":"pnum"},"date":{"index":0,"val":1606979898,"type":"date"},"dur":{"index":2,"val":3,"type":"dur"}},"paramObj":{"when_date":"2020-12-03","several":4,"daily":3},"paramStr":"12月03日(今天)晚餐 4位"}
console.log('searchKeyword',options)
this.options=options
this.city=JSON.parse(options.city)
this.selectComponent('#getpos').getPos();
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
PubSub.emit('custom-tab-bar')
this.getHistory();
this.setData({
navHeight:this.selectComponent('#nav').getHeight()
})
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
getHistory(){
//this.city=util.cache('CACHE_LOCALTION_KEY')
App.Customer.GetHistorySearch().then(res=>{
let mks=[]
res.data.forEach(h=>{
mks.push({ // 获取返回结果,放到mks数组中
title: h.name,
id: h.sid,
addr: '',
city: '',
district: 0,
latitude: h.latitude,
longitude: h.longitude,
type:h.type==1?-1:h.type,
})
})
this.setData({
history:mks
})
})
},
//在Page({})中使用下列代码
//数据回填方法
backfill: function (e) {
var id = e.currentTarget.id;
for (var i = 0; i < this.data.suggestion.length; i++) {
if (i == id) {
console.log('backfill,this.data.suggestion[i]:',this.data.suggestion[i])
// let pages = getCurrentPages();
// let p = pages[pages.length - 2];
// p.setSuggestion(this.data.suggestion[i])
//util.cache('suggestion',this.data.suggestion[i]);
this.setData({
backfill: this.data.suggestion[i].title
});
this.setSuggestion({fun:'setSuggestion',suggestion:this.data.suggestion[i]})
wx.navigateBack();
break;
}
}
},
inputVal:'',
//触发关键词输入提示事件
getsuggest: function (e) {
//调用关键词提示接口
this.inputVal=e.detail.value;
if(this.inputVal.length<1){
return this.setData({
suggestion: [],
keyword:''
})
}
let prarms={
keyword:this.inputVal,
region:this.location.city||'',
region_fix:1,
page_size:20,
page_index:1,
key:map_key,
}
if(this.city){
prarms.name=this.city.name
prarms.code=this.city.code
prarms.pid=this.city.pid
}
if(this.location.latitude){
prarms.location=this.location.latitude+','+this.location.longitude
}
App.Customer.KeywordTips(prarms).then(res=>{
if(this.inputVal.length<1){
return this.setData({
suggestion: []
})
}
var sug = [],hasSellerMore=false,sellerSplit=-1
try{
res.data=res.data.data
if(res.data instanceof Array){
for (var i = 0; i < res.data.length; i++) {
if(i==2 && res.data[i].type==-1){
hasSellerMore=true
continue;
}
if((i==1 || i==2) && res.data[i-1].type==-1 && res.data[i].type!=-1){
sellerSplit=i-1
}
let item=res.data[i]
item.latitude=item.location.lat
item.longitude=item.location.lng
item.addr=item.address
item.titles=item.title.match(new RegExp('(.*)('+this.inputVal+')(.*)'))
sug.push(item);
}
}
}catch(e){}
this.setData({ //设置suggestion属性,将关键词搜索结果以列表形式展示
suggestion: sug,
hasSellerMore:hasSellerMore,
keyword:this.inputVal,
sellerSplit:sellerSplit,
});
})
},
nearby_search: function () {
App.Customer.SearchMap({
boundary:'region('+this.location.city+',1)',
//boundary:'nearby('+this.location.latitude+','+this.location.longitude+',1000,1)',
location:this.location.latitude+','+this.location.longitude,
keyword:this.inputVal,
page_size:20,
page_index:1,
orderby:'_distance',
key:map_key,
}).then(res=>{
var mks = []
for (var i = 0; i < res.data.length; i++) {
mks.push({ // 获取返回结果,放到mks数组中
title: res.data[i].title,
id: res.data[i].id,
addr: res.data[i].address,
city: res.data[i].city,
district: res.data[i].district,
latitude: res.data[i].location.lat,
longitude: res.data[i].location.lng,
type:res.data[i].type,
category:res.data[i].category,
})
}
this.setData({ //设置suggestion属性,将关键词搜索结果以列表形式展示
suggestion:mks,
});
})
},
location:'',
onpos(e){
console.log(e);
this.location=e.detail
this.GetRecommendSellerLists()
this.getCity()
},
getCity(){
qqmapsdk.reverseGeocoder({
location:this.location,
success:(res)=>{
this.location['city']='';
if(res.status==0){
this.location['city']=res.result.ad_info.city
}
},
})
},
gotoList(){
wx.navigateTo({
url:util.http_build_query({
lat:this.location.latitude,
lng:this.location.longitude,
keyword:this.inputVal,
multiIndex:0,
city:this.options.city,
hows:this.options.hows,
filter:this.options.filter,
},'./indexList')
})
},
searchItemClick(e){
//console.log('searchItemClick',e)
let item=e.detail.data
if(e.detail.act=='detail'){
if(item.type==-1){
wx.navigateTo({
url:util.http_build_query({
id:item.id,
},'./detail')
})
}else{
this.setSuggestion({fun:'setSuggestion',suggestion:item})
}
}else{
wx.navigateTo({
url:util.http_build_query({
id:item.id,
activeTab:'reserve',
},'./reserve')
})
}
},
historyEmpty(){
App.Customer.DelHistorySearch().then(()=>{
App.Customer.GetHistorySearch({},'clear')
this.setData({
history:[]
})
})
},
clearKeyword(){
this.setData({
keyword:'',
suggestion:[],
})
},
GetRecommendSellerLists(){
if(!this.location.latitude) return;
let param={}
param={
lat:this.location.latitude,
lng:this.location.longitude
}
App.Customer.GetRecommendSellerLists(param).then(res=>{
this.setData({
rsl:res.data
})
})
},
hasGoto:false,
bindtransition(e){
console.log(e)
if('change'==e.type){
this.hasGoto=false
}
if('animationfinish'==e.type){
if(this.hasGoto && (e.detail.current+1)==this.data.rsl.length){
wx.switchTab({
url: '/pages/reserve/index'
})
return;
}else{
this.hasGoto=true
}
}
},
gotoSellerDetail(e){
let url='/pages/reserve/detail?id='+e.currentTarget.id
wx.navigateTo({
url:url,
})
},
sbtrEvent(e){
console.log(e);
if(e.detail.act=='oncall'){
this.selectComponent('#as').show(e.detail.seller);
}
},
async hasInCity(suggestion){
return new Promise((success,fail)=>{
if(this.city){
qqmapsdk.reverseGeocoder({
location: {
latitude: suggestion.latitude,
longitude: suggestion.longitude
},
success:(res)=>{
if(res.status!=0) return success({flag:true,'msg':'1001'})
console.log('qqmapsdk.reverseGeocoder',res.result.address_component.city,this.city.name)
if(res.result.address_component.city==this.city.name){
return success({flag:true,msg:''})
}
success({flag:false,msg:''})
},
fail:()=>{
success({flag:true,msg:'1002'})
},
})
}else{
success({flag:true,msg:'1003'})
}
})
},
async setSuggestion(data){//只通知前页变更数据
let suggestion=data.suggestion
//地址解析
wx.showLoading()
let hasInCity=await this.hasInCity(suggestion)
console.log('hasInCity',hasInCity)
if(!hasInCity.flag){
return setTimeout(()=>{
util.toast('该位置不在当前城市,建议换个位置','none');
},300)
}
wx.navigateTo({
url:util.http_build_query({
hasSuggestion:1,
keyword:suggestion.title,
lat:suggestion.latitude,
lng:suggestion.longitude,
centre_pos:`${suggestion.latitude},${suggestion.longitude}`,
multiIndex:0,
...this.options,
},'/pages/reserve/indexList')
})
},
})