index.js
5.31 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
import wx from '../../utils/wx';
import config from '../../common/config';
import Yas from '../../common/yas';
import indexModel from '../../models/index/index';
//获取应用实例
let app = getApp();
let yas = new Yas();
let router = global.router;
let {windowHeight} = app.getSystemInfo();
Page({
data: {
productList: [],
floatFilter: false,
filterInitialTop: 0,
topSearchHeight: 0,
resetListScrollPos: 0,
showLoading: false,
showNoMore: false,
isLoading: false,
currentPage: 1,
totalPage: 0,
pullRefresh: false,
showBackTop: false,
showCopyright: false,
resource: config.resourceContentCode
},
onLoad: function () {
this.productList({ page: 1, limit: 20});
setTimeout(() => {
let query = wx.createSelectorQuery();
query.select('.recommend-product .filter').boundingClientRect();
query.select('.container .top-search').boundingClientRect();
query.exec(res => {
this.setData({
filterInitialTop: res[0].top,
topSearchHeight: res[1].bottom - res[1].top,
resetListScrollPos: res[0].top - (res[1].bottom - res[1].top)
});
})
}, 2000);
yas.pageOpenReport();
},
onShow: function () {
yas.report('YB_MAIN_TAB_C', {TAB_ID:1})
},
backTop: function() {
wx.pageScrollTo({
scrollTop: 0,
floatFilter: false
});
},
onPullDownRefresh: function () {
this.setData({
pullRefresh: true
});
this.productList({ page: 1, limit: 20});
},
onReachBottom: function () {
if (this.data.currentPage < this.data.totalPage) {
this.setData({
showLoading: true
});
this.productList({page: this.data.currentPage + 1, limit: 20})
} else {
this.setData({
showNoMore: true
});
}
},
onPageScroll: function (e) {
const { scrollTop } = e;
const query = wx.createSelectorQuery();
query.selectViewport().scrollOffset();
query.exec(res => {
this.setData({
floatFilter: res[0].scrollTop > this.data.filterInitialTop - this.data.topSearchHeight
});
});
const show = scrollTop > windowHeight * 2;
if (show !== this.data.showBackTop) {
this.setData({
showBackTop: show
});
}
},
sortChange: function (e) {
let params;
let {curSort, gender} = e.detail;
params = {
gender,
order: curSort,
page: 1,
limit: 20
};
this.data.productList = [];
params.setScrollPos = true;
this.productList(params);
},
productList: function (params) {
if (this.data.isLoading) return;
this.data.isLoading = true;
wx.showLoading({title: '加载中'});
indexModel.productList(params).then(res => {
if (res.code === 200) {
const keyAdapter = {
skn: 'product_skn',
salePriceStr: 'sales_price',
productName: 'product_name',
defaultImages: 'default_images'
};
let list = [];
(res.data.product_list || []).forEach(product => {
let item = {};
Object.keys(keyAdapter).forEach(key => {
item[key] = product[keyAdapter[key]]
});
list.push(item);
});
this.data.isLoading = false;
wx.hideLoading();
this.setData({
showLoading: false,
productList: this.data.productList.concat(list),
currentPage: params.page,
totalPage: res.data.page_total
});
if (!this.data.showCopyright) {
this.setData({
showCopyright: true
});
}
// 浮动筛选时滚动位置
if (this.data.floatFilter && params.setScrollPos) {
wx.pageScrollTo({
scrollTop: this.data.resetListScrollPos,
duration: 10
});
}
if (this.data.pullRefresh) {
this.setData({
pullRefresh: false
});
wx.stopPullDownRefresh();
}
}
}).catch(() => {
wx.hideLoading();
this.data.isLoading = false;
if (this.data.pullRefresh) {
this.setData({
pullRefresh: false
});
wx.stopPullDownRefresh();
}
});
},
toSearch: function () {
yas.report('YB_SEARCH_C', {SEARCH_POS: 1});
router.go('productSearch');
},
resourceClickReport: function (e) {
yas.report('YB_MAIN_EVENT', e.detail);
}
});