shopSearch.js 2.7 KB

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