index.js 3.0 KB

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