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

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

Page({
  data: {
    order: '',
    gender: '',
    searched: false,
    productList: [],
    showLoading: false,
    showNoMore: false,
    currentPage: 0,
    totalPage: 1,
    urlParams: {},
    showBackTop: false,
    noResult: false,
    windowHeight
  },
  onLoad: function(options) {
    Object.keys(options).map(key => {
      options[key] = decodeURIComponent(options[key] || '');
    });

    wx.setNavigationBarTitle({
      title: options.title || ''
    });
    delete options.title;

    options.shop_id = app.getShopId();

    this.setData({
      urlParams: options
    });
    this.productList(options);

    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(this.data.urlParams);
  },
  setBrandTitle: function(brandId) {
    listModel.brandIntro(brandId).then(res => {
      if (res.code === 200) {
        wx.setNavigationBarTitle({
          title: res.data.brand_name
        });
      }
    })
      .catch(() => {
        wx.setNavigationBarTitle({
          title: '商品列表'
        });
      });
  },
  productList: function(params = {}) {
    params.page = this.data.currentPage + 1;

    if (params.page > this.data.totalPage) {
      return;
    }

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

    this.data.isLoading = true;
    wx.showLoading({title: '加载中'});
    params.order = this.data.order;
    params.gender = this.data.gender;
    params.limit = 20;
    listModel.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.page_total,
          showNoMore: params.page === res.data.page_total,
          noResult: !this.data.productList.concat(list).length
        });

        if (params.resetScroll) {
          wx.pageScrollTo({
            scrollTop: 0,
            duration: 10
          });
        }
      }
    });
  },
  sortChange: function(e) {
    let {curSort, gender} = e.detail;

    this.data.gender = gender;
    this.data.order = curSort;
    this.data.currentPage = 0;
    this.data.totalPage = 1;

    this.data.productList = [];
    this.productList(Object.assign({resetScroll: true}, this.data.urlParams));
  }
});