shop-box.vue
5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<template>
<shop-top v-bind:shop-info="shopInfo"></shop-top>
<div v-bind:class='{"shop-goods-top": !shopInfo.isBlkShop}'>
<goods-list v-bind:data="productList"></goods-list>
</div>
<filter :config="filterConfig" v-ref:filter></filter>
<top-bar v-bind:share-data="shareData"></top-bar>
</template>
<style>
.shop-goods-top {
margin-top: 140px;
}
</style>
<script>
const $ = require('yoho-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 shareSubTitle = '我在BLK发现了一个不错的品牌,赶快来看看吧!';
let locationQuery = qs(decodeURIComponent(location.search.replace(/^\?/, '')));
require('common/vue-filter');
module.exports = {
data() {
return {
shareData: {}, // 分享相关数据
shopInfo: {}, // 店铺介绍相关数据
sortConfig: {},
filterConfig: {},
// query
url: '/product/shop/goods.json',
sort: '',
filter: {},
page: 0, // page= 0 未搜索, 1 并且productList.length =0 没有数据, page = page_total 全部加载完
totalPage: 3,
// 产品列表
productList: [],
// state:
inSearching: false
};
},
watch: {
/* sort 和 filter 改变 都会触发 重新搜索 (想象成清空所有分页) */
sort: function() {
this.research();
},
filter: function() {
this.research();
}
},
methods: {
/* 获取店铺简介相关数据 */
getShopInfo() {
$.get({
url: '/product/shop/info.json',
data: {
domain: locationQuery.domain
}
}).done(result => {
if (result) {
this.shopInfo = result;
this.shopInfo.showBrandInfo = true;
this.shareData = {
title: result.shopName,
des: shareSubTitle,
url: '/product/shop/share?domain=' + locationQuery.domain,
img: result.shopBg,
isBlkShop: result.isBlkShop,
domain: locationQuery.domain,
brandName: result.brandName,
shopId: result.shopId, // 不是分享的参数,收藏店铺使用
isFav: result.isFav // 不是分享的参数,收藏店铺使用
};
} else {
this.shopInfo.showBrandInfo = false;
}
}).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, Object.assign({
order: this.sort,
page: this.page
}, this.filter, locationQuery)).done(result => {
this.productList = result.data.productList;
this.filterConfig = result.data.filter;
}).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: {
topBar,
shopTop,
goodsList,
filter
},
created() {
const self = this;
this.getShopInfo();
this.search();
bus.$on('list.paging', () => {
this.search();
});
bus.$on('sort.change', ({
val
}) => {
console.log(val);
this.sort = val;
});
/**
* 筛选组件 筛选值变更,触发 filter.change事件
* 1. 重新搜索
* 2. 关闭 filter 组件
*/
bus.$on('filter.change', function({val}) {
let filter = {};
$.each(val, (type, item) => {
if (item.id) {
filter[type] = item.id;
}
});
self.$set('filter', filter);
self.$refs.filter.isVisible = false;
});
}
};
</script>