index.js 3.98 KB
import { YOHOOD_HOST, API_HOST } from '../../libs/config';
import { GET, POST } from '../../libs/request';
import jumpToMiniapp from '../../router/jump-to-miniapp';

Page({
  data: {
    imageArray: [ 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2700915385,2771074487&fm=26&gp=0.jpg', 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2700915385,2771074487&fm=26&gp=0.jpg'],
    floorData: {},
    newsList: {
      type: 1, //页数
      list: [],
    },
  },
  onLoad: function(options) {
    //Do some initialize when page load.
    this.fetchHomedata();
    this.fetchNewsList();
  },
  onReady: function() {
    //Do some when page ready.
    
  },
  onShow: function() {
    //Do some when page show. 
  },

  fetchHomedata() {
    let that = this;
    GET(YOHOOD_HOST + '/activity/home', {})
      .then(json => {
        
        if (json && json.code == 200) {
          let floorData = json.data;
          console.log('====================================');
          console.log(floorData);
          console.log('====================================');
          that.setData({
            floorData
          })

          wx.setStorage({
            key: "mars",
            data: floorData.mars 
          });
          wx.setStorage({
            key: "social",
            data: floorData.social 
          });
          wx.setStorage({
            key: "map",
            data: floorData.map 
          });
        }
      })
      .catch(error => {

      })
  },

  fetchNewsList(isLoadMore) {
    let that = this;
    let newsList = this.data.newsList;
    let type = newsList.type;

    if (isLoadMore && type <= 10) { //上限10页
      type += 1;
    } else {
      type = 1;
    }
    newsList.type = type;
    let param = {
      type,
    };

    GET(YOHOOD_HOST + '/default/H5LoadNews', param)
      .then(json => {
        if (json) {
          var list = newsList.list;
          for(var i = 0; i< json.length; i++) {
            let item = json[i];
            item.create_time = that.formatDateForNews(parseInt(item.create_time));
            list.push(item);
          }
          that.setData({
            newsList
          })
        }
      })
      .catch(error => {

      })
  },

  formatDateForNews: function (timestamp) {
    var time = new Date(timestamp * 1000);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    return this.add0(m) + '.' + this.add0(d) + '.' + this.add0(y);
  },
  add0: function (m) {
    if (m < 10 && m > 0){
      return '0' + m;
    } else if (m === 0){
      return '00';
    } else {
      return m;
    }
  },

  tapBanner(e){
    let href = e.detail.href || '';
    let articleId = e.detail.articleId || '';
    if (articleId) {//跳资讯详情
      wx.navigateTo({
        url: '/pages/index/newsDetail?id=' + articleId
      });
    } else {
      wx.navigateTo({
        url: `../webview/webview?url=${encodeURIComponent(href)}`,
      })
    }
  },

  jumpYohobuyGoodsDetail(e) {
    let productSkn = e.detail.productSkn || '';
    if (productSkn.length) {
      jumpToMiniapp({ app: 'yohobuy', page: 'productDetail', data: {productSkn:productSkn} })
    }
  },

  jumpStarBrand(e) {
    let index = e.detail.index;
    let href = e.detail.href || '';
    if (index == 0) { //参展明星
      wx.navigateTo({
        url: `../webview/webview?url=${encodeURIComponent(href)}`,
      })

    } else if (index == 1) {// 参展品牌
      wx.navigateTo({
        url: '../brands/brands'
      })
    }
  },

  //跳资讯详情
  jumpNewsDetail(e) {
    let newsId = e.detail || '';
    wx.navigateTo({
      url: '/pages/index/newsDetail?id=' + newsId
    });
  },

  onHide: function() {
    //Do some when page hide.
    
  },
  onUnload: function() {
    //Do some when page unload.
    
  },

  onReachBottom: function() {
    this.fetchNewsList(true);
  },  
  
  onPullDownRefresh: function() {
    //Do some when page pull down.
    wx.stopPullDownRefresh();

    this.fetchHomedata();
    this.fetchNewsList();
  }
})