search.js 4.0 KB
// pages/homeblock/search/search.js
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    select:1,
    url:'',
    searchshow:false,
    recordshow:true,
    list:[],
    history:[],
    favorite:[],
    keyword:'',
    searchlist:[],
    page:1,
    searchlist:false,
    searchlistarr:[],
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      url: app.globalData.url
    })

    this.getsearchtext()
  },
  clear(){
   console.log(3478734878)
    this.setData({
      keyword:'',
      // searchshow: false,
      // recordshow: true,

    })
  },
  //获取搜索内容
  getsearchtext(){

    let that = this;
    let url = '/index/Index/search_default', params = {
      sort:that.data.select,
      city:wx.getStorageSync('city')

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

      that.setData({
        list: res.list,
        history: res.history,
        favorite: res.favorite
      })
      console.log(this.data.history)

    }).catch((err) => {

    })
  },
  select(e){
   this.setData({
     select:e.currentTarget.dataset.id,
     searchshow: false,
      recordshow: true,
     searchlist:false
   })

    this.getsearchtext()
  },
  //搜索页面
  searchlist(){
    this.setData({
      searchlist:true,
      searchshow:false
    })
    this.getlist()
  },

  getlist(){
    let that = this;
    let url = '/index/Index/search', params = {
      sort: that.data.select,
      keyword:that.data.keyword,
      page:that.data.page
    }
    app.post(url, params).then((res) => {
      console.log(res);
      that.setData({
        searchlistarr:res.list
      })

      let newsearchlistarr = that.data.searchlistarr;
      for (var obj of newsearchlistarr){
        obj.sel=false;

      }
      that.setData({
        searchlistarr: newsearchlistarr
      })



    }).catch((err) => {

    })
  },

//收藏和取消收藏
  collect(e){
    let that = this;
    let id=e.currentTarget.dataset.id;
    let newsearchlistarr = this.data.searchlistarr;
   
    for (var obj of newsearchlistarr){
      obj.sel = !obj.sel
      if(obj.id==id){
        // obj.sel=true
      }else{
        obj.sel = false
      }
    }
    this.setData({
      searchlistarr: newsearchlistarr
    })

   
    let url = '/index/Common/favorite', params = {
      sort: that.data.select,
      id:id
    }
    app.post(url, params).then((res) => {
      console.log(res);
      wx.showToast({
        title: res,
        icon:'none'
      })



    }).catch((err) => {

    })
  },

  //搜索关键词
  getkeyword(){
    let that = this;
    let url = '/index/Index/search_recommend', params = {
      sort: that.data.select,
      keyword:that.data.keyword

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



    }).catch((err) => {
      this.setData({
        searchshow: false,
        recordshow: true,
        
      })

    })
  },

  enterword(e){

   
    this.setData({
      searchshow:true,
      recordshow:false,
      searchlist:false,
      keyword:e.detail.value
    })

    this.getkeyword()
  },

  searchhistory(e){
    this.setData({
      keyword:e.currentTarget.dataset.keyword,
      recordshow:false,
      searchshow:true
    })

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})