productPool.js 2.85 KB
import wx from '../../utils/wx';
import Yas from '../../common/yas';
import listModel from '../../models/product/list';

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

Page({
  data: {
    order: '',
    gender: '',

    // 原始参数
    orgOps: {},

    page: 1,
    limit: 20,
    totalPage: 1,
    allLoaded: false,
    productList: [],
    totalZero: false,
    windowHeight
  },
  onLoad: function(options) {
    Object.keys(options).map(key => {
      options[key] = decodeURIComponent(options[key] || '');
    });

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

    this._productList();

    yas = new Yas();
    yas.pageOpenReport();
  },
  onPageScroll: function({scrollTop}) {
    if (scrollTop > windowHeight * 2 !== this.data.showBackTop) {
      this.setData({
        showBackTop: scrollTop > windowHeight * 2
      });
    }
  },
  backTop: function() {
    wx.pageScrollTo({
      scrollTop: 0
    });
  },
  onReachBottom: function() {
    this._productList();
  },

  // 筛选变更
  filterChange: function(e) {
    let {curSort, gender} = e.detail;

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

    this._resetPage();
    this.setData({ productList: [] });
    this._productList();
  },

  _resetPage: function() {
    this.data.page = 1;
    this.data.totalPage = 1;
    this.data.allLoaded = false;
  },

  // 商品列表
  _productList: function(params = {}) {
    if (this.data.allLoaded) {
      return;
    }

    this.setData({
      showLoading: true
    });

    Object.assign(params, this.data.orgOps, {
      page: this.data.page,
      limit: this.data.limit
    });

    if (this.data.addFilter) {
      params.order = this.data.order;
      params.gender = this.data.gender;
    }

    params.shop_id = app.getShopId();
    listModel.poolProductList(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.page += 1;
        this.data.totalPage = res.data && res.data.page_total || 0;


        this.setData({
          showLoading: false,
          allLoaded: this.data.page > this.data.totalPage || params.limit > list.length,
          productList: this.data.productList.concat(list),
          totalZero: this.data.productList.concat(list).length === 0
        });
      }
    }).catch(() => {});
  }
});