search-page.js 2.8 KB

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