myNews.js 1.98 KB
// pages/myNews/myNews.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:0,
    pageCount:0,
    messageList:[]
  },

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    //触底以后加载更多
    if ((this.data.page+1) < this.data.pageCount) {
      var pageIndex = this.data.page + 1
      this.setData({
        page: pageIndex
      })
      this.getNewsList();
    } else {
      wx.showToast({
        title: '没有更多消息啦。',
        icon: 'success',
        duration: 2000
      });
    }
  },

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

  //获取用户消息列表
  getNewsList() {
    var that = this;
    var config = wx.getStorageSync('config');
    //获取用户消息列表
    wx.request({
      url: app.url + 'ajax_get_user_message.php',
      header: { 'content-type': 'application/json' },
      data: {
        unionId: config.unionId,
        page: that.data.page,
        readFlg: 0,
      },
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        console.log(res.data)
        if (res.statusCode == 200) {
          that.setData({
            pageCount: res.data.result.pageCount,
            messageList: that.data.messageList.concat(res.data.result.messageList)
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  //消息详细页面
  newsDetail(e){
    let messageId = e.currentTarget.dataset['index']
    wx.navigateTo({
      url: '../myNewsDetail/myNewsDetail?messageId=' + messageId,
    })
    wx.setNavigationBarTitle({
      title: '消息详情'
    })
  }
})