index.vue 6.48 KB
<template>
    <div>
        <cheader :title="sortName" class="list-header">
            <i v-if="isiOS" class="icon icon-filter" slot="right" @touchstart="openFilter"></i>
            <i v-else class="icon icon-filter" slot="right" @click="openFilter"></i>
        </cheader>
        <list-filter :val="order" v-if="enableOrder"></list-filter>
        <List :data="productList" :state="listState" class="list-items"></List>
        <Filter :config="filterConfig" v-ref:filter></Filter>
        <shopping-bag :cart-count="cartCount" v-if="isApp"></shopping-bag>
    </div>
</template>
<script>
    const $ = require('jquery');
    const yoho = require('yoho');
    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 listFilter = require('component/product/list-filter.vue');
    const list = require('component/product/list.vue');
    const filter = require('component/product/filter.vue');
    const shoppingBag = require('component/product/shopping-bag.vue');

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

    Vue.use(lazyload, { preLoad: 3 });
    Vue.use(infinitScroll);
    require('common/vue-filter')(Vue);

    module.exports = {
        el: '#product-list',
        data: function() {
            return {
                isApp: yoho.isApp,
                isiOS: yoho.isiOS,
                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,
                cartCount: 0
            };
        },
        computed: {
            empty: function() {
                return this.page !== 0 && !this.productList.length;
            },
            listState: function() {
                let state = 1; // 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: {
            cheader,
            list,
            listFilter,
            filter,
            shoppingBag
        },
        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
                }, locationQuery, this.filter))
                    .done(res => {
                        if (res.data) {
                            self.page = res.data.page;
                            self.totalPage = res.data.page_total;
                            self.$set('productList', self.productList.concat(res.data.product_list));

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

                        // 完成后删除服务端渲染的元素
                        setTimeout(()=> {
                            $('#ssr').remove();
                        }, 500);
                    });
            },

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

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

            refreshCart: function() {
                $.get('/product/cart-count.json').then(result=> {
                    if (result.code === 200) {
                        this.cartCount = result.data.cart_goods_count;
                    }
                });
            }
        },
        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}) {
                self.order = val;
            });

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

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

            // 读取购物车数量
            if (this.isApp) {
                this.refreshCart();
                bus.$on('app.shoppingcart.refresh', this.refreshCart);
            }

        }
    };

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

    .list-items {
        padding-top: 30px;
    }
</style>