const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    star_num: 3,
    datalist: [],
    activity_page: 0,
    activity: [],
    page_type: true
  },
  getdatalist() {
    let that = this;
    let url = 'index/index/index';
    let head = {
      'XX-Token': wx.getStorageSync('token')
    }
    let params = {
      volunteer_page: 0,
      volunteer_pageNum: 3,
      activity_page: that.data.activity_page,
      activity_pageNum: 4
    }
    app.post(url, params, head).then((res) => {
      if (res.news.length > 0) {
        that.setData({
          page_type: true
        })
      } else {
        that.setData({
          page_type: false
        })
      }
      if (that.data.news == null) {
        that.setData({
          datalist: res,
          activity: res.activity,
          news: res.news
        })
      } else {
        that.setData({
          news: that.data.news.concat(res.news)
        })
      }
    }).catch((err) => {

    })
  },
  //判断是否登陆
  get_token() {
    let that = this;
    wx.login({
      success: function(s) {
        let url = 'wxapp/public/getSessionKey';
        if (s.code) {
          var code = s.code;
          var param = {
            code: code
          }
          app.post(url, param).then((res) => {
            let url = 'wxapp/public/get_token'
            let params = {
              openid: res.openid
            }
            app.post(url, params).then((res) => {
              if (res.data) {
                wx.setStorageSync('token', res.data);
                that.setData({
                  datalist: [],
                  activity_page: 0,
                  activity: []
                })
                that.getdatalist();
              } else {
                wx.redirectTo({
                  url: '/pages/start/start'
                })
              }
            }).catch((err) => {

            })
          }).catch((errMsg) => {

          })
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this
    that.get_token()
  },

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

  },

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


  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    // let that = this
    // if (that.data.page_type == false) {
    //   wx.showToast({
    //     title: '暂无更多数据',
    //     icon: 'none',
    //     duration: 1000
    //   })
    //   return false
    // }
    // that.setData({
    //   activity_page: that.data.activity_page + 1
    // })
    // that.getdatalist()
  },

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

  }
})