search-page.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // pages/search-page/search-page.js
  2. var http = require('../../utils/http.js');
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. hotSearchList: [],
  9. prodName:"",
  10. recentSearch: [],
  11. },
  12. /**
  13. * 生命周期函数--监听页面加载
  14. */
  15. onLoad: function (options) {
  16. },
  17. /**
  18. * 生命周期函数--监听页面初次渲染完成
  19. */
  20. onReady: function () {
  21. },
  22. /**
  23. * 生命周期函数--监听页面显示
  24. */
  25. onShow: function () {
  26. var ths = this;
  27. //热门搜索
  28. var params = {
  29. url: "/search/hotSearchByShopId",
  30. method: "GET",
  31. data: {
  32. number:10,
  33. shopId:1,
  34. sort:1,
  35. },
  36. callBack: function (res) {
  37. ths.setData({
  38. hotSearchList:res,
  39. });
  40. },
  41. };
  42. http.request(params);
  43. // 获取历史搜索
  44. this.getRecentSearch();
  45. },
  46. /**
  47. * 获取历史搜索
  48. */
  49. getRecentSearch: function () {
  50. let recentSearch = wx.getStorageSync('recentSearch');
  51. this.setData({
  52. recentSearch
  53. });
  54. },
  55. /**
  56. * 搜索提交
  57. */
  58. toSearchProdPage: function () {
  59. if (this.data.prodName.trim()) {
  60. // 记录最近搜索
  61. let recentSearch = wx.getStorageSync('recentSearch') || [];
  62. recentSearch = recentSearch.filter(item => item !== this.data.prodName)
  63. recentSearch.unshift(this.data.prodName);
  64. if (recentSearch.length>10){
  65. recentSearch.pop();
  66. }
  67. wx.setStorageSync('recentSearch', recentSearch);
  68. // 跳转到商品列表页
  69. wx.navigateTo({
  70. url: '/pages/search-prod-show/search-prod-show?prodName=' + this.data.prodName,
  71. })
  72. }
  73. },
  74. /**
  75. * 清空搜索历史
  76. */
  77. clearSearch: function () {
  78. wx.removeStorageSync('recentSearch');
  79. this.getRecentSearch();
  80. },
  81. /**
  82. * 生命周期函数--监听页面隐藏
  83. */
  84. onHide: function () {
  85. },
  86. /**
  87. * 生命周期函数--监听页面卸载
  88. */
  89. onUnload: function () {
  90. },
  91. /**
  92. * 页面相关事件处理函数--监听用户下拉动作
  93. */
  94. onPullDownRefresh: function () {
  95. },
  96. /**
  97. * 页面上拉触底事件的处理函数
  98. */
  99. onReachBottom: function () {
  100. },
  101. /**
  102. * 用户点击右上角分享
  103. */
  104. onShareAppMessage: function () {
  105. },
  106. // 返回首页
  107. goBackIndex:function(){
  108. wx.navigateBack({
  109. // url: '/pages/search-page/search-page',
  110. })
  111. },
  112. //输入商品名获取数据 || 绑定输入值
  113. getSearchContent:function(e){
  114. this.setData({
  115. prodName: e.detail.value
  116. })
  117. // this.data.prodName=e.detail.value
  118. },
  119. //点击搜素历史
  120. onHistSearch:function(e){
  121. var name = e.currentTarget.dataset.name;
  122. this.setData({
  123. prodName: name
  124. });
  125. this.toSearchProdPage();
  126. }
  127. })