Commit 98db674d3911e8a99513ad6365b37f4792a15620
1 parent
76134058
搜索页增加历史搜索记录
Showing
3 changed files
with
90 additions
and
2 deletions
pages/goods/search/search.js
... | ... | @@ -22,7 +22,12 @@ Page({ |
22 | 22 | tabname: "sort", //排序的字段 |
23 | 23 | adname: "asc", //升降的字段 |
24 | 24 | rq_data:null, |
25 | + | |
26 | + | |
27 | + searchRecord: [], | |
28 | + keyword: '', | |
25 | 29 | }, |
30 | + | |
26 | 31 | onLoad: function(t) { |
27 | 32 | this.data.rq_data=t; |
28 | 33 | |
... | ... | @@ -104,6 +109,9 @@ Page({ |
104 | 109 | if (0 != t.max_price && t.max_price!=undefined) { url += "&min_pirce=" + t.min_pirce + "&max_price=" + t.max_price;} |
105 | 110 | if (url != this.data.baseUrl) return this.requestSearch(url); |
106 | 111 | this.openSearchModal(); |
112 | + | |
113 | + // 获取历史搜索记录 | |
114 | + this.getHistorySearch(); | |
107 | 115 | }, |
108 | 116 | changeTab: function(t) { |
109 | 117 | var ord = t.currentTarget.dataset.href; |
... | ... | @@ -176,18 +184,23 @@ Page({ |
176 | 184 | }); |
177 | 185 | }, |
178 | 186 | submitSearch: function(t) { |
187 | + console.log(111); | |
179 | 188 | this.search(t.detail.value.word); |
189 | + | |
190 | + | |
180 | 191 | }, |
181 | 192 | |
182 | 193 | //热搜关键字 |
183 | 194 | searchHotWord: function(t) { |
184 | - this.search(t.currentTarget.dataset.word); | |
195 | + this.historyRecord(); | |
196 | + this.search(t.currentTarget.dataset.word); | |
185 | 197 | }, |
186 | 198 | |
187 | 199 | //-----点击搜索按钮---- |
188 | 200 | search: function(t) { |
189 | 201 | if ("string" != typeof t || "" == t) return a.showWarning("请输入搜索关键词"); |
190 | 202 | this.data.key_str=t; |
203 | + this.historyRecord(); | |
191 | 204 | this.resetData(), this.requestSearch(this.data.baseUrl + "&key_str=" + encodeURIComponent(t)); |
192 | 205 | }, |
193 | 206 | |
... | ... | @@ -253,4 +266,56 @@ Page({ |
253 | 266 | func(ob); |
254 | 267 | }) |
255 | 268 | }, |
269 | + | |
270 | + | |
271 | + getInput(e) { | |
272 | + this.setData({ | |
273 | + keyword: e.detail.value | |
274 | + }); | |
275 | + }, | |
276 | + | |
277 | + historyRecord() { | |
278 | + let self = this; | |
279 | + let keyword = this.data.key_str; | |
280 | + let searchRecord = this.data.searchRecord; | |
281 | + | |
282 | + if(keyword) { | |
283 | + if(searchRecord.indexOf(keyword) > -1) { | |
284 | + // 已经存在该值 | |
285 | + return; | |
286 | + } else { | |
287 | + if(searchRecord.length >= 10) { | |
288 | + searchRecord.pop(); // 删除最早的一条记录 | |
289 | + }; | |
290 | + searchRecord.unshift(keyword); | |
291 | + | |
292 | + // // 将历史记录添加到缓存中 | |
293 | + wx.setStorage({ | |
294 | + key: 'searchRecord', | |
295 | + data: searchRecord, | |
296 | + success: function(res) { | |
297 | + self.setData({ | |
298 | + searchRecord, | |
299 | + }) | |
300 | + } | |
301 | + }) | |
302 | + }; | |
303 | + }; | |
304 | + }, | |
305 | + | |
306 | + | |
307 | + | |
308 | + | |
309 | + getHistorySearch() { | |
310 | + this.setData({ | |
311 | + searchRecord: wx.getStorageSync('searchRecord') || [] //若无存储则为空 | |
312 | + }); | |
313 | + }, | |
314 | + | |
315 | + clearSearchRecord() { | |
316 | + wx.clearStorageSync('searchRecord'); | |
317 | + this.setData({ | |
318 | + searchRecord: [] | |
319 | + }); | |
320 | + }, | |
256 | 321 | }); |
257 | 322 | \ No newline at end of file | ... | ... |
pages/goods/search/search.wxml
... | ... | @@ -162,7 +162,7 @@ |
162 | 162 | <view class="search-modal"> |
163 | 163 | <view class="search-bar"> |
164 | 164 | <form bindsubmit="submitSearch"> |
165 | - <input autoFocus class="search-input" name="word" placeholder="搜索商品"></input> | |
165 | + <input autoFocus class="search-input" name="word" placeholder="搜索商品" bindinput="getInput"></input> | |
166 | 166 | <button class="search-btn" formType="submit"> |
167 | 167 | <image class="wh100 search-img" src="{{url}}/miniapp/images/sea.png"></image> |
168 | 168 | </button> |
... | ... | @@ -176,5 +176,17 @@ |
176 | 176 | </view> |
177 | 177 | </block> |
178 | 178 | </view> |
179 | + <view class="search-hot"> | |
180 | + <view class="hot-title flex jc_sb ai_c"> | |
181 | + <text>历史搜索</text> | |
182 | + <text class="iconfont icon-shanchu xc-ash" bindtap="clearSearchRecord" wx:if="{{searchRecord.length != 0}}"></text> | |
183 | + </view> | |
184 | + <block wx:if="{{searchRecord.length>0}}"> | |
185 | + <view class="hot-row flex flex-wrap" wx:key="{{index}}"> | |
186 | + <view class="hot-item history ellipsis-1" bindtap="searchHotWord" data-word="{{item}}" wx:for="{{searchRecord}}" wx:key="{{index}}">{{item}}</view> | |
187 | + </view> | |
188 | + </block> | |
189 | + <view wx:else class="fs24">暂无历史搜索记录</view> | |
190 | + </view> | |
179 | 191 | </view> |
180 | 192 | </view> |
181 | 193 | \ No newline at end of file | ... | ... |
pages/goods/search/search.wxss