searchresult.js 2.8 KB
// pages/searchresult/searchresult.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    word: '',
    recommend: [],
    search:true,
    goodlist:[]

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getrecommend()
  },
  enterword(e) {
    console.log(e.detail.value)
    this.setData({
      word: e.detail.value
    })

    console.log(this.data.word)


  },
  // 获取历史搜索
  searchtext() {
    let that = this;
    if(that.data.word==''){
      wx.showToast({
        title: '请输入关键字',
        icon:"none"
      })
      return false
    }
    var url = 'index/searchShopList';
    var params = {
      keyword: that.data.word,
      page: 1,
      limit: 15,
      business_id: '',
      series_id: '',
      grade_id: '',
      composite_id: ''
    }
    app.post(url, params).then((res) => {
      console.log(res);
      if(res.data.length!=0){
        that.setData({
          search:false,
          page:1,
          goodlist:[],
        
        })

        that.setData({
          goodlist: that.data.goodlist.concat(res.data)
        })
        var arr = that.data.goodlist;
        console.log('666',arr)
        let one = [];
        let two = [];
        let newArr = [];
        arr.forEach((el,i)=>{
          console.log(i)
        if(i%2 == 0){
          one.push(el)
        }else{
          two.push(el)
        }
        })
        newArr = [...newArr,...one,...two];
        that.setData({
          goodlist:newArr
        })
        console.log('2222',that.data.goodlist)
  
      }



    }).catch((err) => {

    })

  },
  goodtail(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/shopdetail/shopdetail?id=' + id,
    })

  },
  // 获取推荐店铺
  getrecommend() {
    let that = this;
    var url = 'shop/selectShopRecommend';
    var params = {
      limit: 10

    }
    app.post(url, params).then((res) => {
      console.log(res);
      that.setData({
        recommend: res
      })



    }).catch((err) => {

    })
  },

  goodtail(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/shopdetail/shopdetail?id=' + id,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})