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

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

  /**
   * 生命周期函数--监听页面加载
   */
  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
    }

    that.searchlist()
  },
  searchtextk(){
    if(this.data.word==''){
      wx.showToast({
        title:'请输入搜索内容',
        icon:"none"
      })
      return false
    }
    this.searchlist()
  },
  // 点击搜索
  searchtext(e){
    this.setData({
      word:e.currentTarget.dataset.name
    })
    this.searchlist()
  },

  searchlist(){
    let that=this;
    console.log(that.data.word)
    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) => {
    })
  },
  // 获取历史搜索
  gethistory(){
        let that = this;
        var url = 'sundry/get_keyword_log';
        var params = {
          
        }
        app.post(url, params).then((res) => {
          console.log(res);
          that.setData({
            historylist:res
          })
        }).catch((err) => {

        })
  },

  // 删除历史搜索
  delete(){
    let that = this;
    wx.showModal({
      title: '提示',
      content: '是否要删除历史搜索',
      success (res) {
      if (res.confirm) {
        var url = 'sundry/delete_keyword_log';
        var params = {
        
        }
        app.post(url, params).then((res) => {
          console.log(res);
          wx.showToast({
            title: '删除成功',
            icon:"none"
          })
         that.setData({
          historylist:[]
         })

         console.log(that.data.historylist)
        }).catch((err) => {
          console.log(err)
    
        })
      } else if (res.cancel) {
      console.log('用户点击取消')
      }
      }})
  },
  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 () {
        this.gethistory()
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})