messageDetail.js 1.9 KB
import MessageService from '../MessageService.js'
import { formatTimeByDefined } from '../../../utils'
import Yas from '../../../utils/yas';

let yas;

Page({

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

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

    this.fetchMessageList(messgeType);
    yas.pageOpenReport();
  },

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

    let params = {
      type: messgeType,
      page: messageInfo.currentPage,
      limit: 20
    }

    let api = new MessageService();
    api.fetchNewMessageList(params)
      .then(data => {
        if (data) {
          let msgList = data.list;
          if (msgList) {
            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;
      })
  },
  onShow: function () {

  }
});