const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
   datalist:[],
   activity_page:0,
   activity:[]
  },
  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)=>{
    let activity = res.activity;
    let tem = [];
    let temactivity = [];
    for (let i = 0; i < activity.length; i++) {
     tem.push(activity[i])
     if ((i + 1) != activity.length){
      if ((i + 1) % 2 == 0) {
       temactivity.push(tem);
       tem = [];
      } 
     }else{
      temactivity.push(tem);
      tem = [];
     }
     
    
    }
    let dataactivity = that.data.activity;
    let temdataactivity = dataactivity.concat(temactivity);
    that.setData({
     datalist:res,
     activity: temdataactivity,
     activity_page: res.activity_page
    })
   }).catch((err)=>{
    console.log(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)=>{
       console.log(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)=>{
       console.log(err);
      })
     }).catch((errMsg) => {
      console.log(errMsg);
     })
    }
   }
  });
 },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   let windowHeight = wx.getSystemInfoSync().windowHeight;// 屏幕的高度
   let windowWidth = wx.getSystemInfoSync().windowWidth // 屏幕的宽度
   this.setData({
    scroll_height: windowHeight * 750 / windowWidth
   }) 
   
  },

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   let that = this;
  
   that.get_token();
  
  },

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

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

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

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

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