index.js 11.1 KB

import { getChannelCode, getGenderCode, getRecPosCode, getRecommandContentCode, getResourceCode,getHomeContentCode} from '../../utils/home';
import { shouldDiscardTap, getGoodDetailParam, getImageUrl, formatImageUrl,getGuangType, getYHStorageSync} from '../../utils/util';
import homeService from './indexService.js'
import { parseProductListData } from '../../utils/productListUtil';
import router from '../../router/index.js'

//获取应用实例
let app = getApp()
const windowWidth = app.globalData.systemInfo.windowWidth;
const windowHeight = app.globalData.systemInfo.windowHeight;
const pixelRatio = app.globalData.systemInfo.pixelRatio;

let activityItemWidth = 186;
let activityItemHeight = 237;

let CHANNEL = "gender_channel"

Page({
  data: {
    currentDate:null,
    selectedChannel: 'boy',
    homelist:[],
    newChannels:{
      boy:{
        data:[],//列表list
        isLoading: false,
        recommend: {
          isLoading: false,
          error: null,
          data: null,
        },
      },
      girl:{
        data:[],
        isLoading: false,
        recommend: {
          isLoading: false,
          error: null,
          data: null,
        },
      }
    },
  },
  onLoad: function () {
    this.getLastChannel();
    this.fetchNewHomeData();

    //超过一天
    var currentDate = new Date();
    var pretimestamp = this.getStorage("pretimestamp");
    var preDate;
    if (pretimestamp) {
      preDate = new Date(pretimestamp);
    }
    if (!preDate
    || currentDate.getFullYear() !== preDate.getFullYear()
    || currentDate.getMonth() !== preDate.getMonth()
    || currentDate.getDate() !== preDate.getDate()){
      this.setData({
        currentDate,
      });
      this.showIndexDialog();
    }
  },

  //获取新首页数据
  fetchNewHomeData: function (force) {
    var channel = this.data.selectedChannel;
    let newChannels = this.data.newChannels;
    let currentData = newChannels[channel];
    if (currentData && currentData.data && currentData.data.length > 0 && !force) {
      this.setHomeList();
      return;
    }
    let cotentCode = getHomeContentCode(channel);
    let gender = getGenderCode(channel);
    let yh_channel = getChannelCode(channel);
    if (!force) {
      this.setLoadingHomeData(true);
    }
    let params = {
      content_code: cotentCode,
      gender,
      yh_channel,
      // union_id: app.globalData.WXUnion_ID || this.getStorage('unionID')
    }

    homeService.getHomeFloorlist(params)
      .then(json => {

        this.setLoadingHomeData(false);

        if (json && json.code && json.code == 200) {
          let homeList = json.data.list;
          homeList = homeService.parseHomeList(homeList, windowWidth, windowHeight, activityItemWidth, activityItemHeight);

          if (!homeList || homeList.length <= 0) {
            this.wetoast.toast({
              title: json.message,
              titleClassName: 'wetoast-title',
              duration: 1500
            });
          }
          let newChannels = this.data.newChannels;
          let currentData = newChannels[channel];
          currentData.data = homeList;
          newChannels[channel] = currentData;

          this.setData({
            newChannels: newChannels
          })
          this.setHomeList(force);
        } else {
          this.setHomeList(force);
          this.wetoast.toast({
            title: json.message,
            titleClassName: 'wetoast-title',
            duration: 1500
          });
        }
      })
      .catch(error => {
        this.setLoadingHomeData(false);
        this.setHomeList(force);
        this.wetoast.toast({
          title: error.message,
          titleClassName: 'wetoast-title',
          duration: 1500
        });
      })
  },

  setLoadingHomeData: function (isLoading) {
    let channel = this.data.selectedChannel;
    let _newChannels = this.data.newChannels;
    let currentData = _newChannels[channel];
    if (currentData) {
      currentData.isLoading = isLoading;
    }
    this.setData({
      newChannels: _newChannels,
    })
  },

   //可优化
   setHomeList: function (force) {
    let channel = this.data.selectedChannel;
    let newChannels = this.data.newChannels;
    let currentData = newChannels[channel];
    if (currentData && currentData.data) {
      this.setData({
        homelist: currentData.data,
      })
    }
    console.log('==============楼层数据===============');
    console.log(currentData.data);
    console.log('====================================');

    this.fetchRecommend(force);
  },

  //请求(男生、女生)猜你喜欢数据
  fetchRecommend: function (force) {
    let currentChannel = this.data.selectedChannel;
    let currentChannelData = this.data.newChannels[currentChannel];
    let recommend = currentChannelData.recommend;
    if (recommend.data && recommend.data.length > 0 && !force) {
      return;
    }

    let param = {};
    let gender = getGenderCode(currentChannel);
    let yh_channel = getChannelCode(currentChannel);
    let content_code = getRecommandContentCode(currentChannel);
    let rec_pos = getRecPosCode(currentChannel);
    param = {
      content_code,
      gender,
      yh_channel,
      rec_pos,
    };

    let newChannelData = Object.assign(currentChannelData, { recommend: Object.assign(recommend, { isLoading: true, }) });
    let newChannels = this.data.newChannels;
    newChannels[currentChannel] = newChannelData;

    this.setData({
      newChannels: newChannels,
    });

    homeService.getProductlist(param)
      .then(json => {
        console.log('==============商品数据===============');
        console.log(json);
        console.log('====================================');
        if (!json || !json.code || json.code != 200) {
          let currentChannel = this.data.selectedChannel;
          let currentChannelData = this.data.newChannels[currentChannel];
          let newChannelData = Object.assign(currentChannelData, { recommend: Object.assign(recommend, { isLoading: false, error: { code: json.code, message: json.message } }) });
          let newChannels = this.data.newChannels;
          newChannels[currentChannel] = newChannelData;

          this.setData({
            newChannels: newChannels,
          });
          return;
        }

        let data = json.data.product_list;
        data = parseProductListData(data);
        let currentChannel = this.data.selectedChannel;
        let currentChannelData = this.data.newChannels[currentChannel];
        let newChannelData = Object.assign(currentChannelData, { recommend: Object.assign(recommend, { isLoading: false, data }) });
        let newChannels = this.data.newChannels;
        newChannels[currentChannel] = newChannelData;

        this.setData({
          newChannels: newChannels,
        });
      })
      .catch(error => {
        let currentChannel = this.data.selectedChannel;
        let currentChannelData = this.data.newChannels[currentChannel];
        let newChannelData = Object.assign(currentChannelData, { recommend: Object.assign(recommend, { isLoading: false, error }) });
        let newChannels = this.data.newChannels;
        newChannels[currentChannel] = newChannelData;

        this.setData({
          newChannels: newChannels,
        });
      });
  },

  /**
   * 首页资源位dialog
   */
  showIndexDialog: function(){
    // var app = getApp();
    // let that = this;
    // let content_code = "0ddd512196d6eb4a571dc3281e0b2692";

    // let param = {
    //   content_code,
    // }
    // GET(API_HOST + '/operations/api/v5/resource/get', param).then(json => {
    //   // console.log(json);
    //   if (json && json.code && json.code == 200 && json.data[0] && json.data[0].data[0] && json.data[0].data[0].url){
    //     this.setStorage('pretimestamp', Date.parse(that.data.currentDate));
    //     that.setData({
    //       dialogSrc: formatImageUrl(json.data[0].data[0].src, 340 * app.globalData.systemInfo.pixelRatio, 340 * app.globalData.systemInfo.pixelRatio,2),
    //       dialogUrl: json.data[0].data[0].url,
    //     });
    //     that.setStorage('pretimestamp', Date.parse(that.data.currentDate));
    //     that.dialog.showDialog();
    //   }
    // }).catch(error => { });
  },
  
  //获取上次切换的频道 默认显示上次频道
  getLastChannel: function () {
    let lastChannel = getYHStorageSync(CHANNEL,'index');
    if (lastChannel && (lastChannel === 'boy' || lastChannel === 'girl')) {
      app.globalData.selectedChannel = lastChannel;

      if (lastChannel === 'boy') {
        app.globalData.cid = '1'
      }
      else if (lastChannel === 'girl') {
        app.globalData.cid = '2'
      }
      this.setData({
        selectedChannel: lastChannel
      })
    }
    // let C_ID = app.globalData.cid ? app.globalData.cid : '1';
    // let param = {
    //   C_ID,
    // }
    // logEvent('YB_MAIN_CHANNEL_C', param);
  },

  //是否展开频道选择框
  selectChannel:function(e){
    let _selectChannel = this.data.channelSelect;
    _selectChannel = !_selectChannel;
    this.setData({ channelSelect: _selectChannel})
  },

  hiddenChannel:function(){
    this.setData({
      channelSelect:false,
    })
  },

  //首页频道选择
  channelSelected: function (event) {

    // console.log(event)
    let selectedChannel = event.target.dataset.type;

    if (selectedChannel == this.data.selectedChannel) {
      return;
    }
  
    app.globalData.selectedChannel = selectedChannel;

    if (selectedChannel === 'boy') {
      app.globalData.cid = '1'
    }
    else if (selectedChannel === 'girl') {
      app.globalData.cid = '2'
    }
    // let C_ID = app.globalData.cid;
    // let param={
    //   C_ID,
    // }
    // logEvent('YB_MAIN_CHANNEL_C',param);
    this.setData({
      selectedChannel,
      channelSelect:false
    });

    this.fetchNewHomeData();

    tt.setStorage({
      key: CHANNEL,
      data: this.data.selectedChannel,
    })

    this.backToTop();
  },

  jumpToYohood:function(event){
    // jumpByUrl("https://ad.yoho.cn/html5/2018/yohood/index.html?share_id=5381&title=YOHOOD2018&openby:yohobuy={\"action\":\"go.h5\",\"params\":{\"title\":\"YOHOOD2018\",\"share_id\":5381,\"url\":\"https://ad.yoho.cn/html5/2018/yohood/index.html?share_id=5381&title=YOHOOD2018\"}}")
    // this.setData({
    //   channelSelect: false
    // });
  },

  //formId上报
  formSubmitFromSwitchGender: function (e) {
    // let formId = e.detail.formId;
    // postFormId(formId,"2");
  },

  //回到顶部
  backToTop: function () {
    tt.pageScrollTo({
      scrollTop: 0,
    })
  },

  jumpByRuleEvent(event) {
    console.log(event.detail);
    let url = event.detail.currentTarget.dataset.jump_rule;
    router.goUrl(url);
  },

   // 清除storage
   removeStorage: function(key) {
    try {
      tt.removeStorageSync(key);
    } catch (e) {
      // console.log(e);
    }
  },
  // 获取storage
  getStorage: function(key) {
    return getYHStorageSync(key,'index');
  },
  // 设置storage
  setStorage: function(key, value) {
    try {
      tt.setStorageSync(key, value);
    } catch (e) {
      // console.error(e);  
    }
  },

  onPullDownRefresh: function () {
    tt.stopPullDownRefresh();
    this.getLastChannel();
    this.fetchNewHomeData(true);   
  },

})