// pages/push/push.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        current: 0,
        info: '',
        hasMore: true,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      var _this = this;
      _this.getShareList(_this);
    },
    loadmore: function(){
      var that = this;
      if (that.data.current == 0) {
        that.getShareList(that);
      }
    },
    getShareList: function(_this){
      var page = _this.data.page || 1;
      if(!_this.data.hasMore){return }
      _this.setData({
        hasMore: false
      })
      wx.request({
        url: app.globalData.hostname + 'share_list',
        data: {
          user_id: wx.getStorageSync('openid'),
          page: page,
        },
        header: {
          'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
        },
        method: 'POST',
        success: function (res) {
          if (res.data.code == 200) {
            var newInfo = res.data.data;
            var info = _this.data.info || [];
            newInfo.forEach((item) => {
              item.formatAddtime = dateFormat(item.addtime);
            })
            _this.setData({
              info: info.concat(newInfo),
              hasMore: true,
              page: ++page,
            })
            //省略号
          } else {
            if (page == 1) {
              _this.setData({
                info: [],
              })
            }
          }
        }
      })
    },
   onReachBottom:function(){
     var that = this;
     if (that.data.current == 0){
       that.getShareList(that);
      }
   },
    //转发
    onShareAppMessage: function (res) {
        return {
            title: app.globalData.tuikeTitle,
            path: '/pages/index/index?tuike_uid=' + wx.getStorageSync('openid'),
            success: function (res) {
                //转发成功
            },
        }
    },

    //选项卡
    bindTab: function (e) {
        var current = e.currentTarget.dataset.current
        this.setData({
            current: current
        })
    }
})
//转换时间
function dateFormat(unix) {
  var tempDate = new Date(unix * 1000);
  var year = tempDate.getFullYear();
  var month = tempDate.getMonth() + 1;
  var date = tempDate.getDate();
  var hour = tempDate.getHours();
  var minute = tempDate.getMinutes();
  var second = tempDate.getSeconds();
  month = month - 10 < 0 ? '0' + month : month;
  date = date - 10 < 0 ? '0' + date : date;
  hour = hour - 10 < 0 ? '0' + hour : hour;
  minute = minute - 10 < 0 ? '0' + minute : minute;
  // second = second - 10 < 0 ? '0' + second : second;
  var addTime = year + "-" + month + "-" + date + " " + hour + ":" + minute;
  // var addTime = year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
  return addTime
}