activtyDetail.js 4.04 KB
import wx from '../../utils/wx';
import Yas from '../../common/yas';
import listModel from '../../models/product/list';
import { Actionsheet } from '../../vendors/zanui/index';

let app = getApp();
let { windowHeight } = app.getSystemInfo();

let yas;

Page(Object.assign({
  data: {
    query: '',
    order: '',
    gender: '',
    shopId: 0,
    searched: false,
    productList: [],
    floor: {},
    shopInfoHeight: 0,
    scrollTop: 0,
    fixedFilter: false,
    windowHeight,
    currentPage: 1,
    totalPage: 0,
    showLoading: false,
    showNoMore: false,
    isLoading: false,
    noResult: false
  },
  onLoad: function(options) {
    this.setData({
      coverUrl: options.cover_url
    });
    Object.keys(options).map(key => {
      options[key] = decodeURIComponent(options[key] || '');
    });

    wx.setNavigationBarTitle({
      title: options.title || ''
    });
    delete options.title;
    this.data.bsParams = options;

    this.productList({page: 1, limit: 20, bsParams: options});

    yas = new Yas(app);
    yas.pageOpenReport();
  },
  onReady: function() {
    wx.createSelectorQuery().select('.shop-info').boundingClientRect(rect => {
      this.setData({
        shopInfoHeight: rect.height
      });
    }).exec();
  },
  productList: function(params) {
    if (this.data.isLoading) {
      return;
    }

    this.data.isLoading = true;
    wx.showLoading({title: '加载中'});
    params.order = this.data.order;
    params.gender = this.data.gender;

    if (params.bsParams) {
      params = Object.assign(params, params.bsParams);
      delete params.bsParams;
    } else {
      params = Object.assign(this.data.bsParams, params);
    }

    params.shop_id = app.getShopId();
    listModel.activityDetailList(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.page_total,
          noResult: !this.data.productList.concat(list).length
        });

        if (this.data.fixedFilter && params.resetScroll) {
          wx.pageScrollTo({
            scrollTop: this.data.shopInfoHeight,
            duration: 10
          });
        }
      }
    }).catch(() => {
      this.data.isLoading = false;
    });
  },
  shareShop: function() {
    this.setData({
      'actionsheet.show': true
    });
  },
  sortChange: function(e) {
    let params;
    let {curSort, gender} = e.detail;

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

    params = {
      gender,
      order: curSort,
      page: 1,
      limit: 20
    };
    this.setData({
      productList: []
    });
    params.resetScroll = true;
    this.productList(params);
  },

  onPageScroll: function({scrollTop}) {
    const floatSign = scrollTop >= this.data.shopInfoHeight;

    if (floatSign !== this.data.fixedFilter) {
      this.setData({
        fixedFilter: floatSign
      });
    }

    const backSign = scrollTop > windowHeight * 2;

    if (backSign !== this.data.showBackTop) {
      this.setData({
        showBackTop: backSign
      });
    }
  },
  backTop: function() {
    wx.pageScrollTo({
      scrollTop: 0
    });
  },
  onReachBottom: function() {
    if (this.data.currentPage < this.data.totalPage) {
      this.setData({
        showLoading: true
      });
      this.productList({page: this.data.currentPage + 1, limit: 20, order: this.data.order});
    } else {
      this.setData({
        showNoMore: true
      });
    }
  }
}, Actionsheet));