search.js
13.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
// pages/service/renting/search/search.js
const app = getApp();
Page({
/**
* 页面的初始数据
*/
data: {
searchValue: '',
postList: [],
searchHistory:[{keyword:'法国巴黎'},{keyword:'埃菲尔铁塔'},{keyword:'北京'},{keyword:'西雅图'},{keyword:'美国洛杉矶'},{keyword:'北京'},{keyword:'北京'},{keyword:'北京'},],
currentModal: 0,
currentPost: 0,
hidden_img: false,
hasMore: true,
pageNumber: 0,
avatar: [],
},
//初始化postList
initPostList() {
this.setData({
pageNumber: 0,
hasMore: true,
});
this.data.postList = []
},
inputFocus() {
this.setData({postList:[]})
},
//输入搜索内容
inputKey(e) {
this.setData({searchValue: e.detail.value})
},
//点击搜索(搜索结果)
startSearch() {
let self = this;
if(!self.data.hasMore) return;
self.setData({hasMore: false});
let url = '/home/home/search';
let params = {
keyword: self.data.searchValue,
page: self.data.pageNumber,
};
let header = {
"XX-Token": wx.getStorageSync('token')
};
if(self.data.searchValue === '') {
wx.showToast({title:'搜索内容不能为空~',icon: 'none'})
}else {
self.getSearchHistory();
app.post(url, params, header).then((res) => {
// console.log('搜索结果', res);
if(+res.page < res.page_size) {
self.data.hasMore = true;
}
const list = res.data.map((item, index) => {
return {...item, showlabel_box: false, more: item.more !== '' ? JSON.parse(item.more) : ''}
});
self.setData({
postList: self.data.postList.concat(list)
});
// console.log('搜索结果', self.data.postList);
})
}
},
//点击量
hit(current) {
let url = '/home/home/hits';
let params = {
id: this.data.postList[current].id,
};
let header = { "XX-Token": wx.getStorageSync('token') };
app.post(url, params, header).then((res) => {
// console.log('点击量',res);
if(res.message == "操作成功") {
const post_hits = this.data.postList[current].post_hits +1
this.setData({
[`postList[${current}].post_hits`]: post_hits
});
}
})
},
//清空输入框
clearInput() {
this.setData({searchValue: ''})
},
//搜索历史列表
getSearchHistory() {
let self = this;
let url = '/home/home/search_history';
let header = {
"XX-Token": wx.getStorageSync('token')
};
app.post(url, {}, header).then((res) => {
// console.log('搜索历史列表', res);
self.setData({
searchHistory: res
});
})
},
//点击搜索历史
clickHistory(e) {
const current = e.currentTarget.dataset.index;
const list = [{title:'温哥华+翡翠岛+路易斯湖五日游',time:'2018.4.28',name:'旦巴 在多伦多'}]//假数据
this.setData({
searchValue: this.data.searchHistory[current].keyword,
postList:list,
});
this.startSearch()
},
//清空历史搜索
clearHistory() {
let self = this;
let url = '/home/home/clear_search';
let header = {
"XX-Token": wx.getStorageSync('token')
};
app.post(url, {}, header).then((res) => {
// console.log('清空搜索历史列表', res);
self.getSearchHistory()
})
},
//个人信息弹框
showModal(e) {
let that = this;
const current = e.currentTarget.dataset.index;
if(that.data.currentModal === current) {
that.data.postList[current].showlabel_box = !that.data.postList[current].showlabel_box;
}else {
that.data.postList[current].showlabel_box = !that.data.postList[current].showlabel_box;
that.data.postList[that.data.currentModal].showlabel_box = false;
}
that.setData({
postList: that.data.postList,
currentModal: current
});
},
//复制微信号
clickCopy(e) {
const current = e.currentTarget.dataset.index;
wx.setClipboardData({
data: this.data.postList[current].wxn,
success:function (res) {
// console.log('复制微信号',res);
}
});
this.setData({
[`postList[${this.data.currentModal}].showlabel_box`]: false
});
},
//进入主页
enterHomePage(e) {
let self = this;
const current = e.currentTarget.dataset.index;
wx.navigateTo({
url: '/pages/service/myindex/myindex?user_id=' + self.data.postList[current].user_id
});
self.setData({
[`postList[${self.data.currentModal}].showlabel_box`]: false
});
},
//举报
report(e) {
let self = this;
const current = e.currentTarget.dataset.index;
const to_user_id = self.data.postList[current].user_id;
const post_id = self.data.postList[current].id;
wx.navigateTo({url:'/pages/report/report?to_user_id=' + to_user_id + '&post_id=' + post_id});
self.setData({
[`postList[${self.data.currentModal}].showlabel_box`]: false
});
},
//查看文章详情
goPostDetail(e) {
const current = e.currentTarget.dataset.index;
const list = this.data.postList.map((item,index) => {
return {...item, showlabel_box: false}
});//进入详情时关闭所有弹框
this.setData({
postList: list,
currentPost: current
});
this.hit(current);
wx.navigateTo({
url: '../../rentingDetail/rentingDetail?post_id=' + this.data.postList[current].id,
})
},
//收藏、取消收藏
delArticles(e) {
let self = this;
const current = e.currentTarget.dataset.index;
let url = '/home/home/collect';
let url_del = '/home/home/collect_del';
let header = {
"XX-Token": wx.getStorageSync('token')
};
let params = {
id: self.data.postList[current].id,
};
if(self.data.postList[current].is_hits) {
app.post(url_del, params, header).then((res) => {
// console.log('取消收藏', res);
if(res.message == "操作成功") {
self.setData({
[`postList[${current}].is_hits`]: 0
});
}
})
}else {
app.post(url, params, header).then((res) => {
// console.log('收藏', res);
if(res.message == "操作成功") {
self.setData({
[`postList[${current}].is_hits`]: 1
});
}
})
}
},
//点赞/取消赞
clickZan(e) {
let self = this;
let current = e.currentTarget.dataset.index;
let postList = self.data.postList;
let url = '/home/home/like';
let url_del = '/home/home/like_del';
let header = {
"XX-Token": wx.getStorageSync('token')
};
let params = {
id: self.data.postList[current].id,
};
if(postList[current].is_paise) {
app.post(url_del, params, header).then((res) => {
// console.log('取消赞', res);
if(res.message == "操作成功") {//局部刷新赞数和头像
const post_like = postList[current].post_like - 1;
self.setData({
[`postList[${current}].is_paise`]: 0,
[`postList[${current}].post_like`]: post_like,
});
const paise_user = [];
postList[current].paise_user.map((item) => {
if(item !== res.data) {
paise_user.push(item)
}
});
self.setData({
[`postList[${current}].paise_user`]: paise_user,
})
}
})
}else {
app.post(url, params, header).then((res) => {
// console.log('点赞', res);
if(res.message == "操作成功") {//局部刷新赞数和头像
const post_like = postList[current].post_like + 1;
self.setData({
[`postList[${current}].is_paise`]: 1,
[`postList[${current}].post_like`]: post_like,
});
if(postList[current].paise_user !== undefined) {
const paise_user = postList[current].paise_user;
self.setData({
[`postList[${current}].paise_user`]: paise_user.concat(res.data),
})
}else {
const paise_user = [];
paise_user.push(res.data);
self.setData({
[`postList[${current}].paise_user`]: paise_user,
})
}
}
})
}
},
//去评论
goComment() {
wx.navigateTo({
url: '/pages/service/comment/comment?post_id=' +
this.data.postList[this.data.currentPost].id +
'&is_post_list=' + true
})
},
//获取用户id
personInfo() {
let url = '/home/me/index';
let header = {
"XX-Token": wx.getStorageSync('token')
};
let params = {}
app.post(url, params, header).then((res) => {
console.log(res)
this.setData({
userId: res.id,
});
console.log(this.data.userId)
})
},
onLoad: function (options) {
const self = this;
self.setData({
id: +options.id,
order: +options.order,
select_id: +options.select_id,
});
self.personInfo();
self.getSearchHistory();
},
refreshPostCollect() {
const postList = this.data.postList;
const currentPost = this.data.currentPost;
// console.log('刷新前长度', postList[currentPost].is_hits);
if(this.data.is_collect) {
this.setData({
[`postList[${currentPost}].is_hits`]: 1,
})
}else {
this.setData({
[`postList[${currentPost}].is_hits`]: 0,
})
}
// console.log('刷新后长度', postList[currentPost].is_hits);
},
refreshPostComment() {
const postList = this.data.postList;
const currentPost = this.data.currentPost;
// console.log('刷新前长度', postList[currentPost].comments.length);
if(this.data.is_comment) {
this.setData({[`postList[${currentPost}].comments[0].length`]: this.data.length})
}
// console.log('刷新后长度', postList[currentPost].comments[0].length);
},
refreshPostLike() {
const postList = this.data.postList;
const currentPost = this.data.currentPost;
if(this.data.is_paise) {
const post_like = postList[currentPost].post_like +1;
this.setData({
[`postList[${currentPost}].is_paise`]: 1,
[`postList[${currentPost}].post_like`]: post_like,
[`postList[${currentPost}].paise_user`]: this.data.paise_user,
})
// console.log('点赞', postList[currentPost].post_like);
}else {
const post_like = postList[currentPost].post_like -1;
this.setData({
[`postList[${currentPost}].is_paise`]: 0,
[`postList[${currentPost}].post_like`]: post_like,
[`postList[${currentPost}].paise_user`]: this.data.paise_user,
})
// console.log('取消赞', postList[currentPost].post_like);
}
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
const self = this;
if (!self.data.hasMore) {
wx.showToast({
title: '没有更多数据了~',
icon: 'none'
});
console.log('reach bottom', self.data.hasMore);
} else {
console.log('reach bottom', self.data.hasMore);
self.data.pageNumber++;
self.startSearch();
}
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})