index.js 5.31 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 = new Yas();
let router = global.router;
let {windowHeight} = app.getSystemInfo();

Page({
    data: {
        productList: [],
        floatFilter: false,
        filterInitialTop: 0,
        topSearchHeight: 0,
        resetListScrollPos: 0,
        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});

        setTimeout(() => {
            let query = wx.createSelectorQuery();

            query.select('.recommend-product .filter').boundingClientRect();
            query.select('.container .top-search').boundingClientRect();
            query.exec(res => {
                this.setData({
                    filterInitialTop: res[0].top,
                    topSearchHeight: res[1].bottom - res[1].top,
                    resetListScrollPos: res[0].top - (res[1].bottom - res[1].top)  
                });
            })
        }, 2000);

        yas.pageOpenReport();
    },
    onShow: function () {
        yas.report('YB_MAIN_TAB_C', {TAB_ID:1})
    },
    backTop: function() {
        wx.pageScrollTo({
            scrollTop: 0,
            floatFilter: false
        });
    },
    onPullDownRefresh: function () {
        this.setData({
            pullRefresh: true
        });
        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 query = wx.createSelectorQuery();

        query.selectViewport().scrollOffset();
        query.exec(res => {
            this.setData({
                floatFilter: res[0].scrollTop > this.data.filterInitialTop - this.data.topSearchHeight 
            });
        });

        const show = scrollTop > windowHeight * 2;
        if (show !== this.data.showBackTop) {
            this.setData({
                showBackTop: show
            });
        }
    },
    sortChange: function (e) {
        let params;
        let {curSort, gender} = e.detail;
    
        params = {
            gender,
            order: curSort,
            page: 1,
            limit: 20
        };
        this.data.productList = [];
        
        params.setScrollPos = true;
        this.productList(params);
    },
    productList: function (params) {
        if (this.data.isLoading) return;
        
        this.data.isLoading = true;
        wx.showLoading({title: '加载中'});
        indexModel.productList(params).then(res => { 
            if (res.code === 200) {
                const keyAdapter = {
                    skn: 'product_skn',
                    salePriceStr: 'sales_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
                });
                
                if (!this.data.showCopyright) {
                    this.setData({
                        showCopyright: true
                    });
                }
                
                // 浮动筛选时滚动位置
                if (this.data.floatFilter && params.setScrollPos) {
                    wx.pageScrollTo({
                        scrollTop: this.data.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);
    }
});