shop-box.vue 7.43 KB
<template>
    <div>
        <top-bar :share-data="shareData" :show-top-bar="showTopBar"></top-bar>
        <div :class='{"shop-goods-top": true}' class="list-items">
            <goods-list :data="productList" :state="listState"></goods-list>
        </div>
        <filter-box :val="order" :filter="filterConfig" v-if="enableOrder" ref="filter"
                    :fix-ios-top="fixIosTop"></filter-box>
    </div>
</template>
<style>
    #shop {
        .empty-tip {
            margin-top: 40px;
        }

        .list-items {
            padding-top: 105px;
            background-color: #fff;
        }
    }
</style>
<script>
    import $ from 'jquery';
    import yoho from 'yoho';
    import qs from 'yoho-qs/parse';
    import bus from 'common/vue-bus';
    import tip from 'common/tip';

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

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

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

    export default {
        data() {
            return {
                shareData: {}, // 分享相关数据
                shopInfo: {}, // 店铺介绍相关数据
                sortConfig: {},
                filterConfig: null,
                showTopBar: true,

                // 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: [],
                inSearching: false,
                enableOrder: false,
                order: '',
                fixIosTop: false
            };
        },
        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;
            }
        },
        watch: {

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

            /* 获取店铺简介相关数据 */
            getShopInfo() {
                $.get({
                    // async: false,
                    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;

                        // 处理富文本
                        this.shopInfo.shopIntro && (this.shopInfo.shopIntro = this.shopInfo.shopIntro.replace(/<\/?[^>]*>/g, ''));

                        let shareUrl = locationQuery.id ?
                            location.origin + '/product/shop/favorite/share?id=' + locationQuery.id :
                            location.origin + '/product/shop/' + $shop.data('domain') + '/share';

                        this.shareData = {
                            des: shareSubTitle,
                            url: shareUrl,
                            img: result.shopLogo ? result.shopLogo.split('?')[0] +
                                '?imageMogr2/thumbnail/300x300/format/jpg/quality/90' : '',
                            isBlkShop: result.isBlkShop,
                            domain: locationQuery.domain,
                            brandName: result.brandName,
                            brandId: result.brandId, // 不是分享的参数,收藏店铺使用
                            shopId: result.shopId, // 不是分享的参数,收藏店铺使用
                            isFav: result.isFav // 不是分享的参数,收藏店铺使用
                        };
                    } else {
                        this.shopInfo.showBrandInfo = false;
                    }

                    this.showTopBar = true;
                }).fail(() => {
                    tip('网络出错~');
                });
            },
            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,
                    page: nextPage
                }, locationQuery, this.filter)).done(result => {
                    if (result.code === 200) {
                        this.page = result.data.page;
                        this.totalPage = result.data.pageTotal;
                        this.productList = this.productList.concat(result.data.productList);
                        if (!this.filterConfig) {
                            this.filterConfig = result.data.filter;
                        }
                    }
                }).fail(() => {
                    tip('网络出错~');
                }).always(() => {
                    this.inSearching = false;
                });
            },

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

            this.getShopInfo();
            this.search()
                .then(()=>{
                    if (self.productList.length) {
                        self.enableOrder = true;
                    }
                });
            bus.$on('list.paging', () => {
                this.search();
            });

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


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

            if(yoho.isiOS) {
                this.fixIosTop = true;
            }
        }
    };

</script>