index.js 3.2 KB

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