index.vue 6.25 KB
<template>
    <div>
        <filter-box :val="order" :filter="filterConfig" v-if="enableOrder" :search-page="true"></filter-box>
        <product-list :data="productList" :state="listState" class="list-items"
                      @click-product="clickProduct" :report-page-name="pageName"
                      :report-page-param="query"></product-list>
    </div>
</template>
<script>
    import Vue from 'vue';
    import _ from 'lodash';
    import $ from 'jquery';
    import lazyload from 'vue-lazyload';
    import infinitScroll from 'vue-infinite-scroll';

    import qs from 'yoho-qs';
    import bus from 'common/vue-bus';
    import tip from 'common/tip';

    import ProductList from 'component/product/list.vue';
    import qsParse from 'yoho-qs/parse';
    import FilterBox from 'component/product/filter/index.vue';

    let locationQuery = qsParse(decodeURIComponent(location.search.replace(/^\?/, '')));

    Vue.use(lazyload, { preLoad: 3 });
    Vue.use(infinitScroll);


    export default {
        data: function() {
            return {
                url: '/product/search.json',
                order: 's_t_desc',
                orderType: 0,
                query: qs.query ? decodeURIComponent(qs.query) : '',
                page: 0, // 未搜索 page=0; 全部加载完 page = totalPage; 无数据: page !=0 && productList.length=0
                totalPage: null,
                productList: [],
                inSearching: false,
                enableOrder: false,
                filterConfig: null,
                filter: {},

                // for yas report
                pageName: 'FP_BLK_Search_h5',
            };
        },
        computed: {
            listState: function() {
                let state; // 0: 全部加载完  1: 正在加载

                if (!this.page) {
                    return;
                }

                if (!this.productList.length) {
                    return -1;
                } else if (this.page === this.totalPage) {
                    return 0;
                } else if (this.inSearching) {
                    return 1;
                }

                return state;
            }
        },
        components: {
            ProductList,
            FilterBox
        },
        methods: {
            clickProduct(item, index) {
                if (this.$isYohoBuy) {
                    this.$yas.event('YB_GOODS_LIST_DT', {
                        TYPE_ID: 13,
                        ENT_ID: this.query,
                        I_INDEX: index + 1,
                        PRD_ID: item.product_id,
                        SORT_TYPE: this.orderType
                    });
                }
            },
            search: function() {
                const nextPage = this.page + 1;

                if (this.inSearching) {
                    return;
                }

                // page = 0, 始终执行搜索
                if (this.page && nextPage > this.totalPage) {
                    return;
                }

                this.inSearching = true;

                return $.get(this.url, Object.assign({
                    order: this.order, // 排序 信息
                    query: this.query,
                    page: nextPage
                }, locationQuery, this.filter))
                    .done(res => {
                        if (res.data) {
                            this.page = res.data.page;
                            this.totalPage = res.data.page_total;

                            // yas report param injection
                            _.each(res.data.product_list, item => {
                                item.from_page_name = this.pageName;
                                item.from_page_param = this.query;
                            });

                            this.productList = this.productList.concat(res.data.product_list);
                            if (!this.filterConfig) {
                                this.filterConfig = res.data.filter;
                            }
                            if (this.$isYohoBuy) {
                                setTimeout(() => {
                                    this.$yas.eventData('YB_GOODS_LIST', {
                                        TYPE_ID: 13,
                                        ENT_ID: this.query,
                                        TOTAL: this.productList.length,
                                        PRD_LIST: this.productList.map(product => {
                                            return product.product_id;
                                        }).join(','),
                                        SORT_TYPE: this.orderType,
                                    });
                                }, 500);
                            }
                        }
                    })
                    .fail(() => {
                        tip('网络出错~');
                    })
                    .always(() => {
                        this.inSearching = false;
                    });
            },

            /**
             *  清空数据(page=0) 重新搜索
             */
            research: function() {
                this.page = 0;
                this.productList = [];
                this.search();
            }
        },
        watch: {
            order: function() {
                this.research();
            },
            filter: function() {
                this.research();
            }
        },

        created: function() {
            const self = this;

            bus.$on('list.paging', function() {
                self.search();
            });

            bus.$on('order.change', function({val, type}) {
                self.order = val;
                self.orderType = type;
            });

            bus.$on('filter.change', function({val}) {
                self.filter = val;
            });

            this.search()
                .then(()=>{
                    if (self.productList.length) {
                        self.enableOrder = true;
                    }
                });
        }
    };
</script>
<style>
    html,
    body {
        height: 100%;
        background-color: #fff;
    }
    .top-filter {
        border-top: none!important;
    }
    .list-items {
        background-color: #fff;
        padding-top: 105px;
    }
</style>