var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: true,
    autoplay: true,
    token: '',
    library: {
      libraryUrl: app.libraryUrl,
      bannerLibrary: [],
    },
    news: {
      newsUrl: app.newsUrl,
      newsList: []
    },
    venue: {
      venueUrl: app.venueUrl
    },
    activity: {
      activityUrl: app.activityUrl
    }
  },
  doLogin: function () {
    var self = this;
    wx.login({
      success(res) {
        if (res.code) {
          app.restAjax.post(app.restAjax.path('{url}/wxminiapp/auth/login', [app.loginUrl]), {
            jsCode: res.code
          }, null, function (code, data) {
            var tokenArray = data.data.split('_');
            var token = tokenArray[0];
            var isRandomUsername = tokenArray[1];
            self.setData({
              token: token
            })
            wx.setStorageSync('token', token);
            wx.setStorageSync('isRandomUsername', isRandomUsername)
          }, function (code, data) {
            app.dialog.msg(data.msg);
          });
        } else {
          app.dialog.msg('登录失败!'+ res.errMsg);
        }
      }
    })
  },
  doGetLibrary: function() {
    var self = this;
    app.restAjax.get(app.restAjax.path('{libraryUrl}/app/intangiblelibrary/listpageintangiblelibraryrelease', [self.data.library.libraryUrl]), {
      page: 1,
      rows: 5
    }, null, function(code, data) {
      self.setData({
        'library.bannerLibrary': data.rows
      });
    }, function(code, data){
      app.dialog.msg(data.msg);
    });
  },
  doGetVenue: function() {
    app.restAjax.get(app.restAjax.path('', []), {

    }, null, function(code, data) {
      
    }, function(code, data) {
      app.dialog.msg(data.msg);
    });
  },
  doGetActivity: function() {

  },
  doGetNewsList: function() {
    var self = this;
    app.restAjax.get(app.restAjax.path('{newsUrl}/app/newscontent/listpagenewscontentrelease', [self.data.news.newsUrl]), {
      page: 1,
      rows: 5
    }, null, function(code, data) {
      self.setData({
        'news.newsList': data.rows
      })
    }, function(code, data) {
      app.dialog.msg(data.msg);
    });
  },
  goNewsDetail: function () {
    wx.navigateTo({
      url: '../newsDetail/newsDetail',
    })
  },
  goNewsList: function() {
    wx.navigateTo({
      url: '../newsList/newsList',
    })
  },
  goVolunteer: function () {
    wx.navigateTo({
      url: '../volunteer/volunteer',
    })
  },
  goBroadcast: function () {
    wx.navigateTo({
      url: '../broadcast/broadcast',
    })
  },
  goVenue: function () {
    wx.switchTab({
      url: '../venue/venue',
    })
  },
  goBroadcastDetail: function () {
    wx.navigateTo({
      url: '../broadcastDetail/broadcastDetail',
    })
  },
  goActivity: function () {
    wx.switchTab({
      url: '../activity/activity',
    })
  },
  goHeritage: function () {
    wx.switchTab({
      url: '../heritage/heritage',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    self.doLogin();
    self.doGetLibrary();
    self.doGetNewsList();
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})