index.js 3.2 KB

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