index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // pages/searchGood/index.js
  2. import {
  3. getSearchKeyword,
  4. getProductslist,
  5. getProductHot
  6. } from '../../api/store.js';
  7. const app = getApp();
  8. Page({
  9. /**
  10. * 页面的初始数据
  11. */
  12. data: {
  13. parameter: {
  14. 'navbar': '1',
  15. 'return': '1',
  16. 'title': '搜索商品',
  17. 'color': false
  18. },
  19. host_product: [],
  20. searchValue: '',
  21. focus: true,
  22. bastList: [],
  23. hotSearchList: [],
  24. limit: 8,
  25. page: 1,
  26. loading: false,
  27. loadend: false,
  28. },
  29. /**
  30. * 生命周期函数--监听页面加载
  31. */
  32. onLoad: function (options) {
  33. },
  34. getRoutineHotSearch: function () {
  35. var that = this;
  36. getSearchKeyword().then(res => {
  37. that.setData({
  38. hotSearchList: res.data
  39. });
  40. });
  41. },
  42. getProductList: function () {
  43. var that = this;
  44. if (this.data.loading) return;
  45. if (this.data.loadend) return;
  46. this.setData({
  47. loading: true,
  48. loadTitle: '正在搜索'
  49. });
  50. getProductslist({
  51. keyword: that.data.searchValue,
  52. page: this.data.page,
  53. limit: this.data.limit
  54. }).then(res => {
  55. wx.hideLoading();
  56. var list = res.data,
  57. loadend = list.length < that.data.limit;
  58. that.data.bastList = app.SplitArray(list, that.data.bastList);
  59. that.setData({
  60. bastList: that.data.bastList,
  61. loading: false,
  62. loadend: loadend,
  63. page: that.data.page + 1,
  64. loadTitle: loadend ? '已全部加载' : '加载更多',
  65. });
  66. }).catch(err => {
  67. wx.hideLoading();
  68. that.setData({
  69. loading: false,
  70. loadTitle: "加载更多"
  71. });
  72. });
  73. },
  74. getHostProduct: function () {
  75. var that = this;
  76. getProductHot().then(res => {
  77. that.setData({
  78. host_product: res.data
  79. });
  80. });
  81. },
  82. /**
  83. * 生命周期函数--监听页面初次渲染完成
  84. */
  85. onReady: function () {
  86. },
  87. setHotSearchValue: function (event) {
  88. this.setData({
  89. searchValue: event.currentTarget.dataset.item
  90. });
  91. this.getProductList();
  92. },
  93. setValue: function (event) {
  94. this.setData({
  95. searchValue: event.detail.value
  96. });
  97. },
  98. searchBut: function () {
  99. var that = this;
  100. this.setData({
  101. focus: false
  102. });
  103. if (that.data.searchValue.length > 0) {
  104. that.setData({
  105. page: 1,
  106. loadend: false,
  107. bastList: []
  108. });
  109. wx.showLoading({
  110. title: '正在搜索中'
  111. });
  112. that.getProductList();
  113. } else {
  114. return app.Tips({
  115. title: '请输入要搜索的商品',
  116. icon: 'none',
  117. duration: 1000,
  118. mask: true,
  119. });
  120. }
  121. },
  122. /**
  123. * 生命周期函数--监听页面显示
  124. */
  125. onShow: function () {
  126. this.getRoutineHotSearch();
  127. this.getHostProduct();
  128. },
  129. /**
  130. * 生命周期函数--监听页面隐藏
  131. */
  132. onHide: function () {
  133. },
  134. /**
  135. * 生命周期函数--监听页面卸载
  136. */
  137. onUnload: function () {
  138. },
  139. /**
  140. * 页面相关事件处理函数--监听用户下拉动作
  141. */
  142. onPullDownRefresh: function () {
  143. },
  144. /**
  145. * 页面上拉触底事件的处理函数
  146. */
  147. onReachBottom: function () {
  148. this.getProductList();
  149. },
  150. /**
  151. * 用户点击右上角分享
  152. */
  153. onShareAppMessage: function () {
  154. }
  155. })