index.js 5.07 KB
import wx from '../../utils/wx';
import config from '../../common/config';
import Yas from '../../common/yas';
import indexModel from '../../models/index/index';

// 获取应用实例
let app = getApp();
let yas;
let router = global.router;
let {windowHeight} = app.getSystemInfo();

Page({
  data: {
    gender: '',
    order: '',
    productList: [],
    floatFilter: false,
    showLoading: false,
    showNoMore: false,

    isLoading: false,
    currentPage: 1,
    totalPage: 0,
    pullRefresh: false,
    showBackTop: false,
    showCopyright: false,
    resource: config.resourceContentCode
  },
  onLoad: function() {
    this.productList({ page: 1, limit: 20});
    yas = new Yas(app);
  },
  onReady: function() {
    setTimeout(() => {
      let query = wx.createSelectorQuery();

      query.select('.recommend-product .filter').boundingClientRect();
      query.select('.container .top-search').boundingClientRect();
      query.selectViewport().scrollOffset();
      query.exec(res => {
        const [filter, search, scroll] = res;

        this.maxScrollTop = filter.top + scroll.scrollTop - search.bottom + search.top;
        this.resetListScrollPos = this.maxScrollTop;

        this.onPageScroll({scrollTop: scroll.scrollTop});
      });
    }, 1000);
  },
  onShow: function() {
    yas.pageOpenReport();
    yas.report('YB_MAIN_TAB_C', {TAB_ID: 1});
  },
  backTop: function() {
    wx.pageScrollTo({
      scrollTop: 0,
      floatFilter: false
    });
  },
  onPullDownRefresh: function() {
    this.setData({
      pullRefresh: true
    });
    this.data.productList = [];
    this.productList({ page: 1, limit: 20});
  },
  onReachBottom: function() {
    if (this.data.currentPage < this.data.totalPage) {
      this.setData({
        showLoading: true
      });
      this.productList({page: this.data.currentPage + 1, limit: 20});
    } else {
      this.setData({
        showNoMore: true
      });
    }
  },
  onPageScroll: function(e) {
    const { scrollTop } = e;
    const floatFilter = scrollTop > this.maxScrollTop;
    const show = scrollTop > windowHeight * 2;

    let data;

    if (floatFilter !== this.data.floatFilter) {
      data = data || {};
      data.floatFilter = floatFilter;
    }

    if (show !== this.data.showBackTop) {
      data = data || {};
      data.showBackTop = show;
    }

    data && this.setData(data);
  },
  sortChange: function(e) {
    let params;
    let {curSort, gender} = e.detail;

    this.data.order = curSort;
    this.data.gender = gender;

    params = {
      gender,
      order: curSort,
      page: 1,
      limit: 20
    };
    this.data.productList = [];

    params.setScrollPos = true;
    this.productList(params);
  },
  productList: function(params) {
    const setScrollPos = params.setScrollPos;

    if (this.data.isLoading) {
      return;
    }

    this.data.isLoading = true;
    wx.showLoading({title: '加载中'});
    delete params.setScrollPos;
    indexModel.productList(params).then(res => {
      if (res.code === 200) {
        const keyAdapter = {
          skn: 'product_skn',
          salesPrice: 'sales_price',
          marketPrice: 'market_price',
          productName: 'product_name',
          defaultImages: 'default_images'
        };
        let list = [];

        (res.data.product_list || []).forEach(product => {
          let item = {};

          Object.keys(keyAdapter).forEach(key => {
            item[key] = product[keyAdapter[key]];
          });
          list.push(item);
        });

        this.data.isLoading = false;
        wx.hideLoading();
        this.setData({
          showLoading: false,
          productList: this.data.productList.concat(list),
          currentPage: params.page,
          totalPage: res.data && res.data.page_total || 0
        });

        if (!this.data.showCopyright) {
          this.setData({
            showCopyright: true
          });
        }

        // 浮动筛选时滚动位置
        if (this.data.floatFilter && setScrollPos) {
          wx.pageScrollTo({
            scrollTop: this.resetListScrollPos,
            duration: 10
          });
        }

        if (this.data.pullRefresh) {
          this.setData({
            pullRefresh: false
          });
          wx.stopPullDownRefresh();
        }
      }
    }).catch(() => {
      wx.hideLoading();
      this.data.isLoading = false;
      if (this.data.pullRefresh) {
        this.setData({
          pullRefresh: false
        });
        wx.stopPullDownRefresh();
      }
    });
  },
  toSearch: function() {
    yas.report('YB_SEARCH_C', {SEARCH_POS: 1});
    router.go('productSearch');
  },
  resourceClickReport: function(e) {
    yas.report('YB_MAIN_EVENT', e.detail);
  },
  productClick: function(params) {
    const gender = this.data.gender;

    yas.report('YB_MAIN_EVENT', {
      F_ID: '201804',
      F_NAME: 'recommendGoods',
      F_URL: `/pages/product/detail/detail/fromPage=home&productSkn=${params.detail.productSkn}`,
      F_INDEX: '2',
      I_INDEX: params.detail.idx + '',
      TAB_ID: this.data.order,
            GENDER: gender ? (gender === '1,3' ? '1' : '2') : '0' // eslint-disable-line
    });
  }
});