index.vue 3.98 KB
<template>
    <div>
        <Order :config="orderConfig" :val="order" v-if="enableOrder">
        </Order>
        <List :data="productList" :state="listState"></List>
    </div>
</template>
<script>
    const Vue = require('vue');
    const $ = require('jquery');
    const lazyload = require('vue-lazyload');
    const infinitScroll = require('vue-infinite-scroll');

    const qs = require('yoho-qs');
    const bus = require('common/vue-bus');
    const tip = require('common/tip');

    const order = require('component/product/order.vue');
    const list = require('component/product/list.vue');

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

    require('common/vue-filter')(Vue);

    module.exports = {
        el: '#product-search',
        data: function() {
            return {
                orderConfig: [],

                // query
                url: '/product/search.json',
                order: '',
                query: decodeURIComponent(qs.query),
                page: 0, // 未搜索 page=0; 全部加载完 page = totalPage; 无数据: page !=0 && productList.length=0
                totalPage: null,

                // 产品列表
                productList: [],

                // state
                inSearching: false, // 请求中
                enableOrder: false
            };
        },
        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: {
            list,
            order
        },
        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, {
                    order: this.order, // 排序 信息
                    query: this.query,
                    page: nextPage
                })
                    .done(res => {
                        if (res.data) {
                            self.page = res.data.page;
                            self.totalPage = res.data.pageTotal;
                            self.$set('productList', self.productList.concat(res.data.productList));
                        }
                    })
                    .fail(() => {
                        tip('网络出错~');
                    })
                    .always(() => {
                        self.inSearching = false;
                    });
            },

            /**
             *  清空数据(page=0) 重新搜索
             */
            research: function() {
                this.page = 0;
                this.$set('productList', []);
                this.search();
            }
        },
        watch: {
            /* order 改变 都会触发 重新搜索 */
            order: function() {
                this.research();
            }
        },

        created: function() {
            const self = this;


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

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

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