From 98db674d3911e8a99513ad6365b37f4792a15620 Mon Sep 17 00:00:00 2001 From: taiyuan Date: Mon, 28 Dec 2020 10:09:07 +0800 Subject: [PATCH] 搜索页增加历史搜索记录 --- pages/goods/search/search.js | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- pages/goods/search/search.wxml | 14 +++++++++++++- pages/goods/search/search.wxss | 11 +++++++++++ 3 files changed, 90 insertions(+), 2 deletions(-) diff --git a/pages/goods/search/search.js b/pages/goods/search/search.js index 27a77af..12f178a 100644 --- a/pages/goods/search/search.js +++ b/pages/goods/search/search.js @@ -22,7 +22,12 @@ Page({ tabname: "sort", //排序的字段 adname: "asc", //升降的字段 rq_data:null, + + + searchRecord: [], + keyword: '', }, + onLoad: function(t) { this.data.rq_data=t; @@ -104,6 +109,9 @@ Page({ if (0 != t.max_price && t.max_price!=undefined) { url += "&min_pirce=" + t.min_pirce + "&max_price=" + t.max_price;} if (url != this.data.baseUrl) return this.requestSearch(url); this.openSearchModal(); + + // 获取历史搜索记录 + this.getHistorySearch(); }, changeTab: function(t) { var ord = t.currentTarget.dataset.href; @@ -176,18 +184,23 @@ Page({ }); }, submitSearch: function(t) { + console.log(111); this.search(t.detail.value.word); + + }, //热搜关键字 searchHotWord: function(t) { - this.search(t.currentTarget.dataset.word); + this.historyRecord(); + this.search(t.currentTarget.dataset.word); }, //-----点击搜索按钮---- search: function(t) { if ("string" != typeof t || "" == t) return a.showWarning("请输入搜索关键词"); this.data.key_str=t; + this.historyRecord(); this.resetData(), this.requestSearch(this.data.baseUrl + "&key_str=" + encodeURIComponent(t)); }, @@ -253,4 +266,56 @@ Page({ func(ob); }) }, + + + getInput(e) { + this.setData({ + keyword: e.detail.value + }); + }, + + historyRecord() { + let self = this; + let keyword = this.data.key_str; + let searchRecord = this.data.searchRecord; + + if(keyword) { + if(searchRecord.indexOf(keyword) > -1) { + // 已经存在该值 + return; + } else { + if(searchRecord.length >= 10) { + searchRecord.pop(); // 删除最早的一条记录 + }; + searchRecord.unshift(keyword); + + // // 将历史记录添加到缓存中 + wx.setStorage({ + key: 'searchRecord', + data: searchRecord, + success: function(res) { + self.setData({ + searchRecord, + }) + } + }) + }; + }; + }, + + + + + getHistorySearch() { + this.setData({ + searchRecord: wx.getStorageSync('searchRecord') || [] //若无存储则为空 + }); + }, + + clearSearchRecord() { + wx.clearStorageSync('searchRecord'); + this.setData({ + searchRecord: [] + }); + }, }); \ No newline at end of file diff --git a/pages/goods/search/search.wxml b/pages/goods/search/search.wxml index 66d132a..4f25ce3 100644 --- a/pages/goods/search/search.wxml +++ b/pages/goods/search/search.wxml @@ -162,7 +162,7 @@
- + @@ -176,5 +176,17 @@ + + + 历史搜索 + + + + + {{item}} + + + 暂无历史搜索记录 + \ No newline at end of file diff --git a/pages/goods/search/search.wxss b/pages/goods/search/search.wxss index cb72331..1711e17 100644 --- a/pages/goods/search/search.wxss +++ b/pages/goods/search/search.wxss @@ -298,3 +298,14 @@ /* line-height: 38rpx; */ flex-grow: 1; } + +.history { + background-color: #f0f0f0; + color: #7b7b7b; + border: none; + margin-bottom: 8rpx; +} + +.history:first-of-type { + margin-left: 0; +} -- libgit2 0.21.4