search.js
5.8 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
// pages/search/search.js
const app = getApp()
import {
request
} from "../../request/index.js"
const a = getApp()
Page({
data: {
imagesUrl: a.globalData.baseUrl,
list: [],
inputVal: '',
searchRecord: [],
showDialog: false,
messageAlert: '',
msg: '',
content: '',
page: 1,
searchValue: '',
S4_num: '',
},
onShow: function () {
let that = this
let token = wx.getStorageSync('token')
setTimeout(() => {
if (token) {
that.getList()
} else {
that.setData({
content: "您尚未登录,请登录后操作"
})
that.popConfirm()
}
}, 1300);
},
onLoad: function (options) {
console.log(options);
this.setData({
S4_num: options.S4_num || ''
})
},
openHistorySearch() {
this.setData({
searchRecord: wx.getStorageSync('searchRecord') || []
})
},
// 删除
async delClear() {
let that = this
try {
const {
data
} = await request({
url: 'api/shop/delSou'
})
console.log(data);
that.setData({
msg: data.msg,
searchRecord: []
})
that.popMaskTest()
setTimeout(() => {
that.getList()
}, 1000);
} catch (err) {
console.log(err);
that.setData({
msg: err.msg
})
that.popMaskTest()
}
},
// 获取input内容
getInput(e) {
this.setData({
searchValue: e.detail.value
})
},
// 点击搜索按钮
async searchInput() {
let that = this;
if (that.data.searchValue != '') {
if (that.data.S4_num == 1) {
wx.navigateTo({
url: '/pages/Vmore4SList/Vmore4SList?kw=' + that.data.searchValue
})
} else {
wx.navigateTo({
url: '/pages/searchList/searchList?kw=' + that.data.searchValue + '&change=1'
})
}
that.setData({
page: 1,
list: [],
searchValue: '',
})
} else {
that.setData({
msg: '请输入搜索内容'
})
that.popMaskTest()
}
},
//点击搜索按钮提交表单跳转并储存历史记录
async searchSubmitFn(e) {
console.log(e, "输入框数据");
let that = this
let searchRecord = that.data.searchRecord || []
let inputVal = e.detail.value
if (inputVal == '') {
//输入为空时的处理
that.setData({
messageAlert: "您未输入内容请输入搜索信息",
showDialog: !this.data.showDialog
})
} else {
if (searchRecord.length < 5) {
searchRecord.unshift({
value: inputVal,
id: searchRecord.length
})
} else {
searchRecord.pop() //删掉旧的时间最早的第一条
searchRecord.unshift({
value: inputVal,
id: searchRecord.length
})
}
this.setData({
searchRecord: searchRecord,
searchValue: ''
})
wx.setStorageSync('searchRecord', searchRecord)
const {
data: {
data
}
} = await request({
url: 'api/shop/sou',
data: {
kw: inputVal,
page: that.data.page,
time_id: 2,
price_id: 2
}
})
console.log(data);
if (data.length > 0) {
that.setData({
list: data
})
} else {
that.setData({
msg: '没有找到与' + '"' + inputVal + '"' + '相关的结果'
})
that.popMaskTest()
}
}
},
popMaskTest() {
wx.showToast({
title: this.data.msg,
duration: 1300,
icon: 'none',
mask: true //是否有透明蒙层,默认为false
//如果有透明蒙层,弹窗的期间不能点击文档内容
})
},
// 点击搜索历史进行搜索
toSearch(e) {
let that = this;
console.log(e);
let kw = e.currentTarget.dataset.value
if (that.data.S4_num == 1) {
wx.navigateTo({
url: '/pages/Vmore4SList/Vmore4SList?kw=' + kw
})
} else {
wx.navigateTo({
url: '/pages/searchList/searchList?kw=' + kw + '&change=1'
})
}
that.setData({
searchValue: ''
})
},
// 获取搜索记录
getList() {
let that = this
request({
url: 'api/shop/souLog',
})
.then(res => {
if (res.data.data.length > 0) {
that.setData({
searchRecord: res.data.data
})
console.log(this.data.searchRecord, res.data.data, "搜索")
} else {
that.setData({
msg: '暂无搜索记录'
})
that.popTest()
}
}).catch(err => {
console.log(err);
that.setData({
msg: err.msg
})
that.popTest()
})
},
popTest() {
wx.showToast({
title: this.data.msg,
icon: 'none', //如果要纯文本,不要icon,将值设为'none'
duration: 1300
})
},
popConfirm() {
wx.showModal({
title: '提示',
content: this.data.content,
success: function (res) {
if (res.confirm) {
console.log('点击确认回调')
wx.navigateTo({
url: '/pages/authorization/authorization?num=1'
})
} else {
console.log('点击取消回调')
}
}
})
},
history() {
//将搜索值放入历史记录中,只能放前五条this.data
request({
// url: 'api/shop/sou',
data: {
kw: inputVal
}
})
.then(res => {
console.log(res);
this.setData({
searchRecord: res.data.data
})
console.log(that.data.searchRecord, "搜索")
}).catch(err => {
console.log(err);
})
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})