shop-box.vue 6.37 KB
<template>
    <top-bar v-bind:share-data="shareData"></top-bar>
    <shop-top v-bind:shop-info="shopInfo"></shop-top>
    <div v-bind:class='{"shop-goods-top": !shopInfo.isBlkShop}'>
        <goods-list v-bind:data="productList" :empty="empty"></goods-list>
    </div>
    <filter :config="filterConfig" v-ref:filter></filter>
</template>
<style>
    #shop {
        .empty-tip {
            margin-top: 40px;
        }
    }
</style>
<script>
    const $ = require('jquery');
    const qs = require('yoho-qs/parse');
    const bus = require('common/vue-bus');
    const tip = require('common/tip');

    const topBar = require('product/shop/top-bar.vue'); // 顶部栏,包括返回、收藏店铺、分享,打开筛选页面
    const shopTop = require('product/shop/shop-top.vue'); // 店铺头部信息
    const goodsList = require('component/product/list.vue');
    const filter = require('component/product/filter.vue');

    const $shop = $('#shop');

    const shareSubTitle = '我在BLK发现了一个不错的品牌,赶快来看看吧!';
    let locationQuery = qs(decodeURIComponent(location.search.replace(/^\?/, '')));

    module.exports = {
        data() {
            return {
                shareData: {}, // 分享相关数据
                shopInfo: {}, // 店铺介绍相关数据

                sortConfig: {},
                filterConfig: {},

                // query
                url: '/product/shop/goods.json',
                filter: {
                    domain: $shop.data('domain')
                },
                page: 0, // page= 0 未搜索, 1 并且productList.length =0 没有数据, page = page_total 全部加载完
                totalPage: null,

                // 产品列表
                productList: [],

                // state:
                inSearching: false
            };
        },
        computed: {
            empty: function() {
                return this.page !== 0 && !this.productList.length;
            }
        },
        watch: {

            /* order 和 filter 改变 都会触发 重新搜索 (想象成清空所有分页) */
            order: function() {
                this.research();
            },
            filter: function() {
                this.research();
            }
        },
        methods: {

            /* 获取店铺简介相关数据 */
            getShopInfo() {
                $.get({
                    url: '/product/shop/info.json',
                    data: {
                        domain: $shop.data('domain'),
                        shopId: $shop.data('shopid') // 店铺 ID
                    }
                }).done(result => {
                    if (result) {
                        this.shopInfo = result;
                        this.shopInfo.showBrandInfo = true;
                        let shareUrl = locationQuery.id ?
                            location.origin + '/product/shop/favorite/share?id=' + locationQuery.id :
                            location.origin + '/product/shop/' + $shop.data('domain') + '/share';

                        this.shareData = {
                            title: result.shopName,
                            des: shareSubTitle,
                            url: shareUrl,
                            img: result.shopLogo ? result.shopLogo : '',
                            isBlkShop: result.isBlkShop,
                            domain: locationQuery.domain,
                            brandName: result.brandName,
                            brandId: result.brandId, // 不是分享的参数,收藏店铺使用
                            shopId: result.shopId, // 不是分享的参数,收藏店铺使用
                            isFav: result.isFav // 不是分享的参数,收藏店铺使用
                        };
                    } else {
                        this.shopInfo.showBrandInfo = false;
                    }
                }).fail(() => {
                    tip('网络出错~');
                });
            },
            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;
                $.get(this.url, Object.assign({
                    page: nextPage
                }, this.filter, locationQuery)).done(result => {
                    if (result.code === 200) {
                        self.page = result.data.page;
                        self.totalPage = result.data.pageTotal;
                        self.$set('productList', self.productList.concat(result.data.productList));
                        if ($.isEmptyObject(self.filterConfig)) {
                            self.$set('filterConfig', result.data.filter);
                        }
                    }
                }).fail(() => {
                    tip('网络出错~');
                }).always(() => {
                    self.inSearching = false;
                });
            },

            research: function() {
                this.page = 0;
                this.$set('productList', []);
                this.search();
            }
        },
        components: {
            topBar,
            shopTop,
            goodsList,
            filter
        },
        created() {
            const self = this;

            this.getShopInfo();
            this.search();
            bus.$on('list.paging', () => {
                this.search();
            });

            bus.$on('sort.change', ({
                val
            }) => {
                this.sort = val;
            });


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

                $.each(val, (type, item) => {
                    if (item.id) {
                        filterTemp[type] = item.id;
                    }
                });

                Object.assign(filterTemp, { domain: $shop.data('domain') });
                self.$set('filter', filterTemp);
                self.$refs.filter.isVisible = false;
            });
        }
    };

</script>