index.js 4.05 KB
import { ACTIVITY_HOST, 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: {
      page: 0,
      limit: 20,
      list: [],
      isLast: false,
    },
  },
  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;
    if (!isLoadMore) {
      newsList.page = 0;
      newsList.limit = 20;
      newsList.list = [];
      newsList.isLast = false;
      that.setData({
        newsList,
      });
    }

    if (newsList.isLast) {
      return;
    }
    let param = {
      page: newsList.page + 1,
      type: 1,
    };

    GET(YOHOOD_HOST + '/default/H5LoadNews', param)
      .then(json => {
        if (json) {
          var 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);
          }
          newsList.list = list;
          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 || '';
    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.
    
  },
  onPullDownRefresh: function() {
    //Do some when page pull down.
    wx.stopPullDownRefresh();

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