小程序上拉加载下一页

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    articles: '',
    page: 1,
    category_id: '',
    keywords: '',
    article_end_active: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var category_id = null
    var keywords = null
    var url = 'app/articles/list?page=' + that.data.page
    if (options.category_id != null) {
      that.data.category_id = options.category_id
      url = 'app/articles/list?page=' + that.data.page + '&category_id=' + options.category_id
    } else if (options.keywords != null) {
      that.data.keywords = options.keywords
      url = 'app/articles/list?page=' + that.data.page + '&keywords=' + options.keywords
    }

    wx.request({
      url: app.globalData.globalUrl + url,
      success: res => {
        if (res.data.error_code == 0) {
          var result = res.data.data
          that.setData({
            articles: result.articles,
            article_end_active: that.data.article_end_active
          })
        }
      }
    })
  },

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

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

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

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    that.data.page = that.data.page + 1
    var url = 'app/articles/list?page=' + that.data.page
    if (that.data.category_id != null) {
      url = 'app/articles/list?page=' + that.data.page + '&category_id=' + that.data.category_id
    } else if (that.data.keywords != null) {
      url = 'app/articles/list?page=' + that.data.page + '&keywords=' + that.data.keywords
    }
    wx.request({
      url: app.globalData.globalUrl + url,
      success: res => {
        if (res.data.error_code == 0) {
          var result = res.data.data
          if (result.articles.length < 20) {
            that.data.article_end_active = 'active'
          }
          that.setData({
            articles: that.data.articles.concat(result.articles),
            article_end_active: that.data.article_end_active
          })
        }
      }
    })
  },

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

猜你喜欢

转载自blog.csdn.net/tang05709/article/details/80585682