search.js 8.6 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,
    indexk:'',
    // 关键词
    wordlist:[]
  },



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

  // 选择视频还是图片
  bindPickerChange: function(e) {
    console.log('picker发送选择改变,携带值为', e.detail.value)
    this.setData({
      indexk: e.detail.value,
      type_idk: Number(e.detail.value) + 1,
      page: 1,
      searcharr: [],
      show: true,
      sousuoword: true,
      addressarr: [],
      textarr: [],
      pricearr: [],
    })
    this.data.address.forEach(function(value,index,array){
      value.sel=false
    })
    this.data.text.forEach(function (value, index, array) {
      value.sel = false
    })
    this.data.price.forEach(function (value, index, array) {
      value.sel = false
    })
    this.setData({
      address: this.data.address,
      price: this.data.price,
      text: this.data.text
    })
  },

  // 获取关键字列表
  getkeywordlist() {
    let that = this;
    var url = 'index/search';
    var params = {
    }
    app.post(url, params, "post").then((res) => {
      console.log(res);
      that.setData({
      wordlist:res
      })
    }).catch((err) => {

    })
  },

  // 搜素地名关键字
  seladdress(e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    let parent_id = e.currentTarget.dataset.parent_id;
    let name = e.currentTarget.dataset.name;
    console.log(id,index,parent_id,name);
    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
    })
    wx.navigateTo({
      url: '/pages/searchsecond/searchsecond?textarr='+this.data.textarr+'&pricearr='+this.data.pricearr+'&keyword='+this.data.keyword+'&type_id='+parent_id+'&index='+this.data.indexk+'&addressarr='+this.data.addressarr+'&sontype_id='+id,
    })
  },

  // 搜索内容关键词
  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
    })
    wx.navigateTo({
      url: '/pages/searchsecond/searchsecond?textarr='+this.data.textarr+'&pricearr='+this.data.pricearr+'&keyword='+this.data.keyword+'&type_id='+this.data.type_idk+'&index='+this.data.indexk+'&addressarr='+this.data.addressarr,
    })
  },

  // 搜索价格关键词
  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
    })
    wx.navigateTo({
      url: '/pages/searchsecond/searchsecond?textarr='+this.data.textarr+'&pricearr='+this.data.pricearr+'&keyword='+this.data.keyword+'&type_id='+this.data.type_idk+'&index='+this.data.indexk+'&addressarr='+this.data.addressarr,
    })
  },

  // 输入关键字搜索
  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() {
    app.globalData.addressarr = this.data.addressarr;
    app.globalData.textarr = this.data.textarr;
    app.globalData.pricearr = this.data.pricearr;
    app.globalData.keyword = this.data.keyword;
    app.globalData.type_id = this.data.type_id;
    app.globalData.index = this.data.index;
    console.log('77474',this.data.type_id)
    wx.navigateTo({
      url: '/pages/searchsecond/searchsecond?textarr='+this.data.textarr+'&pricearr='+this.data.pricearr+'&keyword='+this.data.keyword+'&type_id='+this.data.type_idk+'&index='+this.data.indexk+'&addressarr='+this.data.addressarr,
    })
  },

  // 搜索列表
  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)
      })
      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,
    })
  },

  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() {
    this.data.address.forEach(function (value, index, array) {
      value.sel = false
    })
    this.data.text.forEach(function (value, index, array) {
      value.sel = false
    })
    this.data.price.forEach(function (value, index, array) {
      value.sel = false
    })
    this.setData({
      address: this.data.address,
      price: this.data.price,
      text: this.data.text,
      keyword:'',
      textarr:[],
      addressarr:[],
      pricearr:[],
      type_id:1,
      index:0
    })
  },

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

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    // wx.redirectTo({
    //   url: '/pages/search/search',
    // })
      // wx.navigateBack
  },

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

  },

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

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

  }
})