search_result.js 4.6 KB
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nav_index: 0,
    page: 1,
    c_index: 0, //筛选分类下标
    price: 'price desc', //价格排序方式
    paynum: 'paynum desc' //销量排序方式
  },


  select_nav(e) {
    let index = Number(e.currentTarget.dataset.index)
    let id = e.currentTarget.dataset.id
    if (index == this.data.nav_index) {
      return
    } else {
      this.setData({
        cid: id,
        nav_index: index,
        page: 1,
        page_type: false,
        c_index: 0, //筛选分类下标
        price: 'price desc', //价格排序方式
        paynum: 'paynum desc' //销量排序方式
      })
      this.good_list(id, 0)
    }
  },
  // 排序
  reorder(e) {
    let that = this
    let index = Number(e.currentTarget.dataset.index)
    if (index == 1) {
      if (that.data.price == "price") {
        that.setData({
          price: 'price desc'
        })
      } else {
        that.setData({
          price: 'price'
        })
      }
    } else if (index == 2) {
      if (that.data.paynum == "paynum") {
        that.setData({
          paynum: 'paynum desc'
        })
      } else {
        that.setData({
          paynum: 'paynum'
        })
      }
    }
    this.setData({
      c_index: e.currentTarget.dataset.index,
      page: 1,
      page_type: false
    })
    //调用排序接口
    this.good_list(this.data.cid, e.currentTarget.dataset.index)
  },
  //获取页面信息
  get_info(pid) { //
    wx.showLoading({
      title: '加载中'
    })
    let that = this
    let index = that.data.nav_index
    let url = app.interface.index
    let params = {
      pid: pid
    }
    app.post(url, params).then((res) => {
      wx.hideLoading()
      that.setData({
        nav_arr: res.msg,
        cid: res.msg[index].id
      })
      that.good_list(res.msg[index].id, that.data.c_index)
    })
  },
  //获取商品列表
  good_list(pid, c_index) {
    wx.showLoading({
      title: '加载中',
    })
    let that = this
    let url = that.data.search_type ? app.interface.search:app.interface.good_list
    let params = that.data.search_type ?{
      name: pid,
      page: that.data.page
    }:{
      pid: pid,
      page: that.data.page
    }
    if (c_index == 1) {
      params.price = that.data.price
    } else if (c_index == 2) {
      params.paynum = that.data.paynum
    }
    app.post(url, params).then((res) => {
      let list = res.msg;
      for(let obj of list){
        obj.sales = obj.paynum
      }
      if (that.data.page == 1) {
        wx.hideLoading()
        that.setData({
          shop_arr: list
        })
        that.pageScrollToBottom()
      } else {
        if (res.msg.length == 0) {
          wx.hideLoading()
          that.setData({
            page_type: true
          })
        } else {
          setTimeout(() => {
            wx.hideLoading()
            that.setData({
              shop_arr: that.data.shop_arr.concat(list)
            })
          }, 1000)
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.setNavigationBarTitle({
      title: options.title ? options.title : '搜索'
    })
    if (options.index){
      this.setData({
        nav_index: Number(options.index),
        nav_type: true,
        pid: options.pid
      })
      this.get_info(options.pid)
    } else {
      if (options.name){
        this.setData({
          cid: options.name,
          search_type: true
        })
        this.good_list(options.name, 0)
      } else {
        this.setData({
          cid: options.pid
        })
        this.good_list(options.pid, 0)
      }
     
    }

    
  },

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let that = this
    if (that.data.page_type) {
      wx.showToast({
        title: '暂无更多数据',
        icon: 'none',
        duration: 1000
      })
      return
    }
    that.setData({
      page: that.data.page + 1
    })
    that.good_list(that.data.cid, that.data.c_index)
  },

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

  }
})