index.vue
6.83 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<template>
<div class="product-list" :class="{'no-header': noheader}">
<header-box :title="sortName" class="list-header"></header-box>
<filter-box :val="order" :filter="filterConfig" v-if="enableOrder"></filter-box>
<product-list :data="productList" :state="listState" class="list-items"
:report-page-name="pageName" :report-page-param="pageParam"></product-list>
<shopping-bag :cart-count="cartCount" v-if="isApp"></shopping-bag>
</div>
</template>
<script>
import $ from 'jquery';
import _ from 'lodash';
import yoho from 'yoho';
import Vue from 'vue';
import lazyload from 'vue-lazyload';
import infinitScroll from 'vue-infinite-scroll';
import qs from 'yoho-qs/parse';
import bus from 'common/vue-bus';
import tip from 'common/tip';
import HeaderBox from 'component/header.vue';
import ProductList from 'component/product/list.vue';
import ShoppingBag from 'component/product/shopping-bag.vue';
import FilterBox from 'component/product/filter/index.vue';
let locationQuery = qs(decodeURIComponent(location.search.replace(/^\?/, '')));
Vue.use(lazyload, { preLoad: 3 });
Vue.use(infinitScroll);
export default {
data: function() {
return {
noheader: false,
isApp: yoho.isApp,
isiOS: yoho.isiOS,
sortName: locationQuery.title || locationQuery.sort_name, // 优先使用 title
orderConfig: [],
filterConfig: null,
// query
url: '/product/list.json',
order: 's_t_desc',
filter: {},
page: 0, // 未搜索 page=0; 全部加载完 page = totalPage; 无数据: page !=0 && productList.length=0
totalPage: null,
// 产品列表
productList: [],
// state
inSearching: false, // 请求中
enableOrder: false,
cartCount: 0,
fixIosTop: false,
// for yas report
pageName: 'FP_BLK_Category_h5',
pageParam: locationQuery.sort || ''
};
},
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;
}
},
components: {
HeaderBox,
ProductList,
ShoppingBag,
FilterBox
},
methods: {
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(res => {
if (res.data) {
this.page = res.data.page;
this.totalPage = res.data.page_total;
// yas report param injection
_.each(res.data.product_list, item => {
item.from_page_name = this.pageName;
item.from_page_param = this.pageParam;
});
this.productList = this.productList.concat(res.data.product_list);
if (!this.filterConfig) {
this.filterConfig = res.data.filter;
}
}
})
.fail(() => { // eslint-disable-line
tip('网络出错~');
})
.always(() => {
this.inSearching = false;
// 完成后删除服务端渲染的元素
setTimeout(()=> {
$('#ssr').remove();
}, 500);
});
},
openFilter() {
this.$refs.filter.isVisible = true;
},
/**
* 清空数据(page=0) 重新搜索
*/
research: function() {
this.page = 0;
this.productList = [];
this.search();
},
refreshCart: function() {
$.get('/product/cart-count.json').then(result=> {
if (result.code === 200) {
this.cartCount = result.data.cart_goods_count;
}
});
}
},
watch: {
/* order 和 filter 改变 都会触发 重新搜索 */
order: function() {
this.research();
},
filter: function() {
this.research();
}
},
created: function() {
const self = this;
// this.noheader = yoho.isYohoBuy;
bus.$on('list.paging', function() {
self.search();
});
bus.$on('order.change', function({val}) {
self.order = val;
});
bus.$on('filter.change', function({val}) {
self.filter = val;
});
this.search()
.then(()=>{
if (self.productList.length) {
self.enableOrder = true;
}
});
// 读取购物车数量
if (this.isApp) {
this.refreshCart();
bus.$on('app.shoppingcart.refresh', this.refreshCart);
}
if(yoho.isiOS) {
this.fixIosTop = true;
}
}
};
</script>
<style>
@import "../../../scss/common/_header.css";
html,
body {
height: 100%;
background-color: #fff;
}
.list-header {
background-color: white;
}
.list-items {
background-color: #fff;
padding-top: 105px;
}
.product-list.no-header {
.top-filter {
top: 0;
}
}
</style>