messageDetail.js 2.59 KB
import MessageService from '../MessageService.js'
import { formatTimeByDefined } from '../../../utils'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '',
    messageInfo: {
      isLoading: false,
      hasMore: true,
      currentPage: 1,
      messageList: [],
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    let title = options.title;
    let messgeType = options.type;
    this.setData({
      title,
    });

    this.fetchMessageList(messgeType);
  },

  // 消息列表
  fetchMessageList: function (messgeType) {
    let that = this;
    let messageInfo = that.data.messageInfo;
    if (messageInfo.isLoading || !messageInfo.hasMore) {
      return;
    }
    messageInfo.isLoading = true;
    that.setData({
      messageInfo
    })

    wx.showLoading({
      title: '',
    })

    let params = {
      type: messgeType,
      uid: '500031152',
      page: messageInfo.currentPage,
      limit: 20,
      debug: 'XYZ'
    }

    let api = new MessageService();
    api.fetchNewMessageList(params, () => {
      wx.hideLoading();
    })
      .then(data => {
        if (data && data.code == 200 && data.data.list) {
          let msgList = data.data.list;
          const list = msgList.map(item => {
            let createTime = formatTimeByDefined(item.createTime, 'Y.M.D h:m:s');
            item.createTime = createTime;
            return item;
          })
          messageInfo.messageList = messageInfo.messageList.concat(list);

          let hasMore = messageInfo.currentPage < data.totalPage;
          messageInfo.hasMore = hasMore;
          if (hasMore) {
            messageInfo.currentPage = messageInfo.currentPage + 1;
          }
          messageInfo.isLoading = false;

          that.setData({
            messageInfo,
          })
        }
      })
      .catch(error => {
        messageInfo.isLoading = false;
        console.error(error);
      })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

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

  }
})