search.vue
4.48 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
<template>
<div>
<filter-sub :data="brand" :value="3" type="brand"></filter-sub>
<Sort :config="sortConfig" :val="sort">
</Sort>
<List :data="productList"></List>
<Drawer v-ref:drawer>
<Filter :config="filterConfig"></Filter>
</Drawer>
</div>
</template>
<script>
const Vue = require('yoho-vue');
const lazyload = require('yoho-vue-lazyload');
const infinitScroll = require('yoho-vue-infinite-scroll');
const bus = require('common/vue-bus');
const tip = require('common/tip');
const sort = require('product/sort.vue');
const list = require('product/list.vue');
const drawer = require('product/drawer.vue');
const filter = require('product/filter.vue');
const filterSub = require('product/filter/filter-detail.vue');
Vue.use(lazyload);
Vue.use(infinitScroll);
require('common/vue-filter');
module.exports = {
el: '#product-list',
data: function() {
return {
sortConfig: global.sortConfig,
filterConfig: global.filterConfig,
// query
url: 'search',
sort: null,
filter: {},
page: 0, // 未搜索 page=0; 全部加载完 page = totalPage; 无数据: page !=0 && productList.length=0
totalPage: null,
// 产品列表
productList: [],
// state
inSearching: false, // 请求中
brand: require('product/filter/brand-group-mock')
};
},
components: {
list,
sort,
filter,
filterSub,
drawer
},
methods: {
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;
console.log(nextPage);
$.post(this.url, {
sort: this.sort,
filter: this.filter,
page: nextPage
})
.done(res => {
self.page = res.data.page;
self.totalPage = res.data.pageTotal;
self.$set('productList', self.productList.concat(res.data.productList));
})
.fail(error => {
tip('网络出错~');
})
.always(() => {
self.inSearching = false;
});
},
openFilterSub: function() {
console.log('TODO: open filter sub');
},
/**
* 清空数据(page=0) 重新搜索
*/
research: function() {
this.page = 0;
this.$set('productList', []);
this.search();
}
},
watch: {
/* sort 和 filter 改变 都会触发 重新搜索 */
sort: function() {
this.research();
},
filter: function() {
this.research();
}
},
created: function() {
const self = this;
bus.$on('list.paging', function() {
self.search();
});
bus.$on('sort.change', function({
val
}) {
console.log(val);
self.sort = val;
});
/**
* 筛选组件 筛选值变更,触发 filter.change事件
* 1. 重新搜索
* 2. 关闭 drawer 组件
*/
bus.$on('filter.change', function({
val
}) {
console.log(val);
self.filter = val;
self.$refs.drawer.on = false;
});
/**
* 筛选组件 打开二级晒寻,通过bridge 打开APP view
* 1. 打开view
* 2. 监听 router.back ,重新设置 筛选值
*/
bus.$on('filter.sub.show', function({
val
}) {
self.openFilterSub(val);
});
this.search();
}
};
</script>
<style>
</style>