brand-box.vue 5.39 KB
<template>
    <brand-top-bar v-bind:share-data="shareData"></brand-top-bar>
    <brand-shop-top v-bind:shop-info="shopInfo"></brand-shop-top>
    <goods-list v-bind:data="productList"></goods-list>
    <drawer v-ref:drawer>
        <filter :config.once="filterConfig"></filter>
    </drawer>
</template>

<script>
    const $ = require('yoho-jquery');
    const qs = require('yoho-qs');
    const bus = require('common/vue-bus');
    const tip = require('common/tip');
    const brandTopBar = require('channel/brand-top-bar.vue'); // 顶部栏,包括返回、收藏店铺、分享,打开筛选页面
    const brandShopTop = require('channel/brand-shop-top.vue'); // 店铺头部信息
    const goodsList = require('product/list.vue');
    const drawer = require('product/drawer.vue');
    const filter = require('product/filter.vue');

    require('common/vue-filter');

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

                sortConfig: global.sortConfig,
                filterConfig: global.filterConfig,

                // query
                url: '/get-brand-shop-goods',
                sort: '',
                filter: {},
                page: 0, // page= 0 未搜索, 1 并且productList.length =0 没有数据, page = page_total 全部加载完
                totalPage: 3,

                // 产品列表
                productList: [],

                // state:
                inSearching: false
            };
        },
        watch: {
            domain: function() {
                this.getShopInfo();
            },

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

            /* 获取店铺简介相关数据 */
            getShopInfo() {
                $.get({
                    url: '/get-shop-info',
                    data: { domain: this.domain }
                }).done(result => {
                    if (result) {
                        this.shopInfo = result;
                        this.shopInfo.showBrandInfo = true;
                        this.shareData = {
                            title: result.brandName,
                            link: '/brand-share?domain=' + this.domain,
                            img: result.brandBg
                        };
                    } else {
                        this.shopInfo.showBrandInfo = false;
                    }
                }).fail(() => {
                    tip('网络错误');
                });
            },
            getProductList() {
                let data = {};

                $.ajax({
                    url: this.url,
                    data: data
                }).done(result => {
                    this.productList = result.data.productList;
                }).fail(() => {
                    tip('网络错误');
                });
            },
            search: function() {
                const self = this;

                if (this.inSearching) {
                    return;
                }
                if (this.page && this.page + 1 > this.totalPage) {
                    return;
                }

                this.inSearching = true;
                this.page++;
                $.post(this.url, {
                    sort: this.sort,
                    filter: this.filter,
                    page: this.page
                }).done(result => {
                    this.productList = result.data.productList;
                }).fail(error => {
                    self.page--;
                    console.log(error);
                }).always(() => {
                    self.inSearching = false;
                });
            },

            openFilterSub: function() {
                console.log('TODO: open filter sub');
            },

            /**
             *
             */
            research: function() {
                this.page = 0;
                this.$set('productList', []);
                this.search();
            }
        },
        components: {
            brandTopBar,
            brandShopTop,
            goodsList,
            drawer,
            filter
        },
        created() {
            this.getShopInfo();

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

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


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

            /**
             *  筛选组件 打开二级晒寻,通过bridge 打开APP view
             *  1. 打开view
             *  2. 监听 router.back ,重新设置 筛选值
             */
            bus.$on('filter.sub.show', function({val}) {
                this.openFilterSub(val);
            });
        }
    };
</script>