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

  /**
   * 页面的初始数据
   */
  data: {
    show: true,
    showlist: 0,
    array: ['视频', '图片'],
    index: 0,
    type_id: 1,
    keyword: '',
    historylist: [],
    searcharr: [],
    page: 1,
    sousuoword: true,
    address: [],
    price: [],
    text: [],
    addressarr: [],
    textarr: [],
    pricearr: [],
    selad:-1,
    selte:-1,
    selword:-1
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    if (options.index!=undefined){
      this.setData({
        index: options.index,
        type_id: Number(options.index) + 1
      })
    }

    
    this.getkeywordlist()
  },

  // 选择视频还是图片
  bindPickerChange: function(e) {
    console.log('picker发送选择改变,携带值为', e.detail.value)
    this.setData({
      index: e.detail.value,
      type_id: Number(e.detail.value) + 1,
      page: 1,
      searcharr: [],
      show: true,
      sousuoword: true
    })
  },

  // 获取关键字列表
  getkeywordlist() {
    let that = this;
    var url = 'user/words';
    var params = {


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

      for (var obj of that.data.address) {
        obj.sel = false
      }
      for (var obj of that.data.text) {
        obj.sel = false
      }

      let newarr = [];
      that.data.price.forEach(function(value, index, array) {
        var obj = {}
        obj.name = value;
        obj.sel = false;
        newarr.push(obj)
      })
      that.setData({
        address:that.data.address,
        text:that.data.text,
        price:newarr
      })

      console.log(that.data.address,that.data.text,that.data.price)

    }).catch((err) => {

    })
  },

  // 搜素地名关键字
  seladdress(e) {
    let that=this;
    
    let id=e.currentTarget.dataset.id;
    let index=e.currentTarget.dataset.index;
    that.data.address.forEach(function(value,indexk,array){
      if(id==value.id){
        value.sel=!value.sel;
        if(value.sel==true){
          that.data.addressarr.push(value.id)
        }else{
          that.data.addressarr.forEach(function(value,indexl,array){
            if(id==value){
              that.data.addressarr.splice(indexl,1)
            }
          })
        }
      }
    })

    that.setData({
      address:that.data.address,
      addressarr:that.data.addressarr
    })
  },

  // 搜索内容关键词
  seltext(e){
    let that = this;
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    that.data.text.forEach(function (value, indexk, array) {
      if (id == value.id) {
        value.sel = !value.sel;
        if (value.sel == true) {
          that.data.textarr.push(value.id)
        } else {
          that.data.textarr.forEach(function (value, indexl, array) {
            if (id == value) {
              that.data.textarr.splice(indexl, 1)
            }
          })
        }
      }
    })

    that.setData({
      text: that.data.text,
      textarr: that.data.textarr
    })

    console.log(that.data.text);
    console.log(that.data.textarr)
  },

  // 搜索价格关键词
  selprice(e){
    let that = this;
    console.log(that.data.price)
    let price=e.currentTarget.dataset.price;
    console.log(price)
    let index = e.currentTarget.dataset.index;
    that.data.price.forEach(function (value, indexk, array) {
      
      if (price == value.name) {
        console.log(value.name)
        value.sel = !value.sel;
        if (value.sel == true) {
          that.data.pricearr.push(value.name)
        } else {
          that.data.pricearr.forEach(function (value, indexl, array) {
            if (price == value) {
              that.data.pricearr.splice(indexl, 1)
            }
          })
        }
      }
    })

    that.setData({
      price: that.data.price,
      pricearr: that.data.pricearr
    })

    
  },



  // 输入关键字搜索

  enterword(e) {
    console.log(e)
    this.setData({
      keyword: e.detail.value,
      sousuoword: true
    })

    console.log(this.data.keyword)
  },

  // 输入内容
  shuru(e) {
    console.log(e);
    this.setData({
      keyword: e.detail.value,
      sousuoword: true
    })

  },
  sousuo() {

    // if (this.data.keyword == '') {
    //   wx.showToast({
    //     title: '请输入搜索内容',
    //     icon: 'none'
    //   })
    // } else {
      

    // }
    this.setData({
      page: 1,
      searcharr: [],
      sousuoword: false
    })

    this.getsousuo();
  },



  // 搜索列表
  getsousuo() {
    let that = this;
    console.log(that.data.type_id)
    let url = 'user/search';
    let param = {
      type_id: that.data.type_id,
      keyword: that.data.keyword,
      address:that.data.addressarr,
      price:that.data.pricearr,
      text:that.data.textarr,
      page: that.data.page,
      pageNum: 10
    }
    app.post(url, param, 'post').then((res) => {
      console.log(res);
      that.setData({
        searcharr: that.data.searcharr.concat(res)
      })
      // that.setData({
      //   sousuoword: true
      // })

      if (res.length != 0 && this.data.type_id == 1) {
        that.setData({
          showlist: 1,
          show: false
        })
      } else if (res.length != 0 && this.data.type_id == 2) {
        that.setData({
          showlist: 2,
          show: false
        })
      } else if (res.length != 0 && this.data.type_id == 3) {
        that.setData({
          showlist: 3,
          show: false
        })
      } else {
        that.setData({
          show: false
        })
      }
    }).catch((err) => {

    })
  },



  // 播放视频
  govideo(e) {
    console.log(e)
    let url = e.currentTarget.dataset.url;
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/videodetail/videodetail?id=' + id,
    })
    // wx.navigateTo({
    //   url: '/pages/videoplay/videoplay?url=' + url,
    // })

  },

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

  gopic(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/picdetail/picdetail?id=' + id,
    })
  },
  // 点击加载更多
  jiazai() {

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let newpage = this.data.page;
    newpage++;
    this.setData({
      page: newpage
    })

    this.getsousuo()

  },

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

  }
})