search.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // pages/search/search.js
  2. // 获取应用实例
  3. const app = getApp()
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. inputValue: '',
  10. goodsList: [],
  11. staticUrl: '',
  12. historyList: [],
  13. showHistoryList: [],
  14. historyShow: false,
  15. // 是否显示全部
  16. showAll:true,
  17. result:false,
  18. topList:[]
  19. },
  20. //处理输入
  21. handleInput(e) {
  22. var input = e.detail.value;
  23. this.setData({
  24. inputValue: input
  25. })
  26. },
  27. //点击搜索
  28. handleSearchTap(e) {
  29. var inputValue = this.data.inputValue;
  30. if (inputValue.trim().length <= 0) {
  31. return;
  32. }
  33. this.search(inputValue);
  34. this.add2History(inputValue);
  35. },
  36. /**
  37. * 搜索
  38. * @param {*} value
  39. */
  40. search(value){
  41. var _this=this
  42. wx.showLoading({
  43. title: '搜索中',
  44. })
  45. wx.request({
  46. url: app.globalData.rootUrl + '/goods/search',
  47. method: "GET",
  48. data: {
  49. inputValue: value
  50. },
  51. success(e) {
  52. wx.hideLoading()
  53. _this.setData({
  54. goodsList: e.data.data,
  55. historyShow: false,
  56. result:true
  57. })
  58. }
  59. })
  60. },
  61. //点击取消
  62. handleCancleTap() {
  63. wx.navigateBack({
  64. delta: 1,
  65. })
  66. },
  67. /**
  68. * 添加购物车
  69. * @param {*} input 搜索值
  70. */
  71. add2History(input) {
  72. var historyList = this.data.historyList;
  73. for (var i = 0; i < historyList.length; i++) {
  74. if(historyList[i]==input){
  75. return;
  76. }
  77. }
  78. historyList.push(input);
  79. this.setData({
  80. historyList: historyList
  81. })
  82. this.storeHistory()
  83. },
  84. /**
  85. * 删除历史记录
  86. * @param {*} index
  87. */
  88. removeHistory(e) {
  89. var index = e.currentTarget.dataset.index;
  90. var historyList = this.data.historyList;
  91. historyList.splice(index, 1);
  92. this.setData({
  93. historyList: historyList,
  94. historyShow:historyList.length!=0
  95. })
  96. this.setSubShowHistoryList();
  97. this.storeHistory();
  98. },
  99. clearAll() {
  100. this.setData({
  101. historyList: [],
  102. showHistoryList: [],
  103. historyShow:false
  104. })
  105. wx.removeStorageSync('searchHistory')
  106. },
  107. //展示部分
  108. setSubShowHistoryList() {
  109. var historyList = this.data.historyList;
  110. var showHistoryList = [];
  111. var idx = historyList.length > 3 ? 3 : historyList.length;
  112. for (var i = 0; i < idx; i++) {
  113. showHistoryList.push(historyList[i]);
  114. }
  115. this.setData({
  116. showHistoryList: showHistoryList
  117. })
  118. },
  119. //全部历史
  120. setAllShowHistoryList() {
  121. this.setData({
  122. showAll: false,
  123. showHistoryList: this.data.historyList
  124. })
  125. },
  126. //缓存历史记录
  127. storeHistory() {
  128. var history = this.data.historyList;
  129. wx.setStorage({
  130. key: "searchHistory",
  131. data: history
  132. })
  133. },
  134. /**
  135. * 点击重新搜索
  136. * @param {*} e
  137. */
  138. reSearch(e) {
  139. var value=e.currentTarget.dataset.value;
  140. this.search(value)
  141. this.add2History(value);
  142. },
  143. getTopList(){
  144. var _this=this
  145. wx.request({
  146. url: app.globalData.rootUrl+'/goods/search/top/10',
  147. method:"GET",
  148. success(e){
  149. if(e.data.success){
  150. _this.setData({
  151. topList:e.data.data
  152. })
  153. }
  154. }
  155. })
  156. },
  157. initData() {
  158. var _this = this
  159. var staticUrl = app.globalData.staticUrl;
  160. this.setData({
  161. staticUrl: staticUrl,
  162. serverPrefix:app.globalData.serverPrefix
  163. })
  164. var historyList = wx.getStorageSync('searchHistory')
  165. if (historyList.length>0) {
  166. _this.setData({
  167. historyShow:true,
  168. historyList: historyList
  169. })
  170. }
  171. this.setSubShowHistoryList();
  172. this.getTopList();
  173. },
  174. handle2GoodsInfo(e){
  175. var goodsId = e.currentTarget.dataset.goodsId;
  176. wx.navigateTo({
  177. url: '/pages/sub_index_page/goods_info/goods_info?goodsId=' + goodsId,
  178. })
  179. },
  180. /**
  181. * 生命周期函数--监听页面加载
  182. */
  183. onLoad: function (options) {
  184. this.initData()
  185. },
  186. /**
  187. * 生命周期函数--监听页面初次渲染完成
  188. */
  189. onReady: function () {
  190. },
  191. /**
  192. * 生命周期函数--监听页面显示
  193. */
  194. onShow: function () {
  195. },
  196. /**
  197. * 生命周期函数--监听页面隐藏
  198. */
  199. onHide: function () {
  200. },
  201. /**
  202. * 生命周期函数--监听页面卸载
  203. */
  204. onUnload: function () {
  205. },
  206. /**
  207. * 页面相关事件处理函数--监听用户下拉动作
  208. */
  209. onPullDownRefresh: function () {
  210. },
  211. /**
  212. * 页面上拉触底事件的处理函数
  213. */
  214. onReachBottom: function () {
  215. },
  216. /**
  217. * 用户点击右上角分享
  218. */
  219. onShareAppMessage: function () {
  220. }
  221. })