index.js 3.47 KB
import wx from '../../utils/wx';
import resource from '../../common/resource';
import indexModel from '../../models/index/index';

//获取应用实例
let app = getApp();

Page({
    data: {
        resource,
        productList: [],
        showFilterHolder: false,
        filterInitialTop: 0,
        topSearchHeight: 0
    },
    onLoad: function () {
        this.productList();

        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,
                });
            })
        }, 1000)
    },

    onShow: function () {


    },
    onShareAppMessage: function (res) {
        // let param = {
        //     FROM: res.from,
        //     SHARE_RESUIL: 0,
        //     TITLE: 'Yoho!Buy有货-潮流购物逛不停',
        //     PATH: '/pages/index/index'
        // }
        //
        // // 用户点击右上角分享
        // return {
        //     title: param.TITLE, // 分享标题
        //     desc: '精选1400+全球潮流品牌,明星潮牌和新锐原创品牌每日上新,100%正品行货。', // 分享描述
        //     path: param.PATH, // 分享路径
        //     success: function (res) {
        //         param.SHARE_RESUIL = 1
        //         logEvent(YB_SHARE_RESULT_L, param);
        //     },
        //     fail: function (res) {
        //         param.SHARE_RESUIL = 2
        //         logEvent(YB_SHARE_RESULT_L, param);
        //     }
        // }
    },
    onPullDownRefresh: function () {
        // wx.stopPullDownRefresh();
        // this.getLastChannel();
        // this.fetchNewHomeData(true);
    },
    //回到顶部
    backToTop: function () {
        wx.pageScrollTo({
            scrollTop: 0
        })
    },
    onPageScroll: function () {
        let query = wx.createSelectorQuery();

        query.selectViewport().scrollOffset();
        query.exec(res => {
            this.setData({
                showFilterHolder: res[0].scrollTop > this.data.filterInitialTop - this.data.topSearchHeight 
            });
        });
    },
    sortChange: function (e) {
        let params;
        let {curSort, gender} = e.detail;
    
        params = {
            gender,
            order: curSort,
        };
        this.productList(params)
    },
    productList: function (params) {
        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.setData({
                    productList: list
                });
            }
            
            
        });
    },
    toSearch: function () {
        wx.navigateTo({
            url: '../search/search'
        });
    }
});