admin
2020-12-22 28a4cfadc0a78d1bfec093e0694f420aaf3a725c
pages/msg-record/msg-record.js
@@ -1,43 +1,153 @@
var help = require('/util/help.js');
var api = require('/util/api.js');
var $this;
Page({
  data: {
    showLoading: true,
    msgList: [{
      contentList: [{
        title: "开奖期数", content: [{
          name: 'div',
          children: [{
            type: 'text',
            text: 'Hello World!Hello World!Hello World!Hello World!Hello World!',
          },
          {
            name: 'img',
            attrs: {
              src: 'https://gw.alicdn.com/tps/i3/TB1yeWeIFXXXXX5XFXXuAZJYXXX-210-210.png_160x160.jpg',
              style: 'border-radius:50%;width:30rpx;margin-left:5rpx;'
            }
          },
          {
            name: 'a',
            attrs: {
              href: 'https://www.baidu.com',
              style: 'color:#0080FF;'
            },
            children: [{
              type: 'text',
              text: '查看详情'
            }]
          }
          ],
        }],
      }, {}, {}, {}]
    }, {}, {}]
    /* 列表数据开始 */
    list: [],
    showLoading: false,
    page: 1,
    total: 0,
    showEmptyDefault: false,
    isRequesting: false,//是否正在请求中
    showFooter: false,
    /* 列表数据结束 */
  },
  onLoad() {
    my.setNavigationBar({
      title: "消息记录",
      backgroundColor: "#0080FF",
      borderBottomColor: "#0080FF"
    help.setPageNavBar("消息记录");
    $this = this;
  },
  onPullDownRefresh() {
    console.log('onPullDownRefresh', new Date());
    my.showToast({
      content: "下拉刷新触发"
    });
    this.requestUserMsgList(1);
  },
  /**
   * 转为为富文本数据
   * @param {*} list
   * @param {*} callback
   */
  convertToRichText(list, callback) {
    for (var i = 0; i < list.length; i++) {
      for (var j = 0; j < list[i].contentList.length; j++) {
        help.loadRichTextNode(list[i].contentList[j].content, function (res) {
          list[i].contentList[j].content = res;
          if (i == list.length - 1 && j == list[i].contentList.length) {
            callback(list);
          }
        });
      }
    }
  },
  /**
   * 清除未读消息
   */
  onClearUnRead() {
    $this.requestSetMsgRead();
  },
  /**
   * 设置消息已读
   */
  requestSetMsgRead() {
    api.setMsgRead({
      showErrorToast: true,
      showLoading: true,
      success: function (res) {
        if (res.code == 0) {
          my.showToast({
            content: '清除成功'
          })
        }
      }
    });
  },
  //请求活动
  requestUserMsgList(page, first) {
    $this.setData({
      page: page,
      isRequesting: true,
      showEmptyDefault: false
    });
    api.getUserMsgList({
      page: page,
      showErrorToast: true,
      showLoading: first,
      success: function (res) {
        console.log(res)
        if (res.code == 0) {
          var list = res.data.data;
          //数据预处理
          $this.convertToRichText(list, function (list) {
            //设置列表数据
            if (list != null && list.length > 0) {
              var totalList = $this.data.list.concat(list);
              if (page == 1) {
                totalList = list;
              }
              $this.setData({
                total: res.data.count,
                list: totalList,
                showFooter: totalList.length >= res.data.count
              })
            }
            //设置空列表默认显示
            if (page == 1 && (list == null || list.length == 0)) {
              $this.setData({
                showEmptyDefault: true
              })
            }
          });
        }
      },
      fail: function () {
        //请求失败需要将页码减1
        if ($this.data.page > 1) {
          $this.setData({
            page: $this.data.page - 1
          });
        }
      },
      complete: function () {
        my.stopPullDownRefresh();
        setTimeout(() => {
          $this.setData({
            isRequesting: false,
            showLoading: false
          });
        }, 1000);
      }
    });
  },
  //下拉刷新
  onPullDownRefresh() {
    this.requestUserMsgList(1, false);
  },
  //到达底部
  async scrollMytrip() {
    if (this.data.isRequesting)
      return;
    try {
      console.log('scrollMytrip:');
      const { page, list, } = this.data;
      // 判断是否还有数据需要加载
      if (list.length < this.data.total) {
        //开启加载
        this.setData({ showLoading: true });
        const newPage = page + 1;
        $this.requestUserMsgList(newPage);
      }
    } catch (e) {
      this.setData({ showLoading: false });
      console.log('scrollMytrip执行异常:', e);
    }
  },
});