index.vue 5.11 KB
<template>
    <div>
        <cheader :title="sortName" class="list-header">
            <i class="icon icon-filter" slot="right" @click="openFilter"></i>
        </cheader>
        <order :config="orderConfig" :val="order" v-if="enableOrder"></order>
        <List :data="productList" :empty="empty"></List>
        <Filter :config="filterConfig" v-ref:filter></Filter>
    </div>
</template>
<script>
    const $ = require('jquery');
    const Vue = require('vue');
    const lazyload = require('vue-lazyload');
    const infinitScroll = require('vue-infinite-scroll');
    const qs = require('yoho-qs/parse');
    const bus = require('common/vue-bus');
    const tip = require('common/tip');
    const cheader = require('component/header.vue');
    const order = require('component/product/order.vue');
    const list = require('component/product/list.vue');
    const filter = require('component/product/filter.vue');

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

    Vue.use(lazyload);
    Vue.use(infinitScroll);
    require('common/vue-filter');

    module.exports = {
        el: '#product-list',
        data: function() {
            return {
                sortName: locationQuery.title || locationQuery.sort_name, // 优先使用 title
                orderConfig: [],
                filterConfig: null,

                // query
                url: '/product/list.json',
                order: '',
                filter: {},
                page: 0, // 未搜索 page=0; 全部加载完 page = totalPage; 无数据: page !=0 && productList.length=0
                totalPage: null,

                // 产品列表
                productList: [],

                // state
                inSearching: false, // 请求中
                enableOrder: false
            };
        },
        computed: {
            empty: function() {
                return this.page !== 0 && !this.productList.length;
            }
        },
        components: {
            cheader,
            list,
            order,
            filter
        },
        methods: {
            search: function() {
                const self = this;
                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,
                    page: nextPage
                }, this.filter, locationQuery))
                    .done(res => {
                        if (res.code === 200) {
                            self.page = res.data.page;
                            self.totalPage = res.data.pageTotal;
                            self.$set('productList', self.productList.concat(res.data.productList));

                            if (!self.filterConfig) {
                                self.$set('filterConfig', res.data.filter);
                            }
                        }
                    })
                    .fail(error => {
                        tip('网络出错~');
                    })
                    .always(() => {
                        self.inSearching = false;
                    });
            },

            openFilter() {
                this.$refs.filter.isVisible = true;
            },

            /**
             *  清空数据(page=0) 重新搜索
             */
            research: function() {
                this.page = 0;
                this.$set('productList', []);
                this.search();
            }
        },
        watch: {
            /* order 和 filter 改变 都会触发 重新搜索 */
            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}) {
                console.log(val);
                self.order = val;
            });

            /**
             * 筛选组件 筛选值变更,触发 filter.change事件
             *  1. 重新搜索
             *  2. 关闭 drawer 组件
             */
            bus.$on('filter.change', function({val}) {
                console.log(val);
                let filter = {};

                $.each(val, (type, item) => {
                    if (item.id) {
                        filter[type] = item.id;
                    }
                });
                self.$set('filter', filter);
                self.$refs.filter.isVisible = false;
            });

            this.search()
                .then(()=>{
                    if (self.productList.length) {
                        self.enableOrder = true;
                    }
                });
        }
    };

</script>
<style>
    .list-header {
        background-color: white;
    }
</style>