Skip to content

Commit 993b57b

Browse files
committed
perf: 列表到详情优化上拉体验
1 parent c0bb34b commit 993b57b

1 file changed

Lines changed: 148 additions & 134 deletions

File tree

‎pages/template/list2detail-list/list2detail-list.vue‎

Lines changed: 148 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -2,173 +2,187 @@
22
<view>
33
<view class="banner" @click="goDetail(banner)">
44
<image class="banner-img" :src="banner.cover"></image>
5-
<view class="banner-title">{{banner.title}}</view>
5+
<view class="banner-title">{{ banner.title }}</view>
66
</view>
77
<view class="uni-list">
8-
<view class="uni-list-cell" hover-class="uni-list-cell-hover" v-for="(value,key) in listData" :key="key" @click="goDetail(value)">
8+
<view class="uni-list-cell" hover-class="uni-list-cell-hover" v-for="(value, key) in listData" :key="key" @click="goDetail(value)">
99
<view class="uni-media-list">
1010
<image class="uni-media-list-logo" :src="value.cover"></image>
1111
<view class="uni-media-list-body">
12-
<view class="uni-media-list-text-top">{{value.title}}</view>
12+
<view class="uni-media-list-text-top">{{ value.title }}</view>
1313
<view class="uni-media-list-text-bottom">
14-
<text>{{value.author_name}}</text>
15-
<text>{{value.published_at}}</text>
14+
<text>{{ value.author_name }}</text>
15+
<text>{{ value.published_at }}</text>
1616
</view>
1717
</view>
1818
</view>
1919
</view>
2020
</view>
21+
<uni-load-more :status="status" :content-text="contentText" />
2122
</view>
2223
</template>
2324

2425
<script>
25-
var dateUtils = require('../../../common/util.js').dateUtils;
26+
import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
27+
var dateUtils = require('../../../common/util.js').dateUtils;
2628
27-
export default {
28-
data() {
29-
return {
30-
banner: {},
31-
listData: [],
32-
last_id: "",
33-
reload: false
29+
export default {
30+
components: {
31+
uniLoadMore
32+
},
33+
data() {
34+
return {
35+
banner: {},
36+
listData: [],
37+
last_id: '',
38+
reload: false,
39+
status: 'more',
40+
contentText: {
41+
contentdown: '上拉加载更多',
42+
contentrefresh: '加载中',
43+
contentnomore: '没有更多'
3444
}
35-
},
36-
onLoad() {
37-
this.getBanner();
38-
this.getList();
39-
},
40-
onPullDownRefresh() {
41-
this.reload = true;
42-
this.last_id = "";
43-
this.getBanner();
44-
this.getList();
45-
},
46-
onReachBottom() {
47-
this.getList();
48-
},
49-
methods: {
50-
getBanner() {
51-
let data = {
52-
column: "id,post_id,title,author_name,cover,published_at" //需要的字段名
53-
};
54-
uni.request({
55-
url: 'https://unidemo.dcloud.net.cn/api/banner/36kr',
56-
data: data,
57-
success: (data) => {
58-
uni.stopPullDownRefresh();
59-
if (data.statusCode == 200) {
60-
this.banner = data.data;
61-
}
62-
},
63-
fail: (data, code) => {
64-
console.log('fail' + JSON.stringify(data));
45+
};
46+
},
47+
onLoad() {
48+
this.getBanner();
49+
this.getList();
50+
},
51+
onPullDownRefresh() {
52+
this.reload = true;
53+
this.last_id = '';
54+
this.getBanner();
55+
this.getList();
56+
},
57+
onReachBottom() {
58+
this.status = 'more';
59+
this.getList();
60+
},
61+
methods: {
62+
getBanner() {
63+
let data = {
64+
column: 'id,post_id,title,author_name,cover,published_at' //需要的字段名
65+
};
66+
uni.request({
67+
url: 'https://unidemo.dcloud.net.cn/api/banner/36kr',
68+
data: data,
69+
success: data => {
70+
uni.stopPullDownRefresh();
71+
if (data.statusCode == 200) {
72+
this.banner = data.data;
6573
}
66-
})
67-
},
68-
getList() {
69-
var data = {
70-
column: "id,post_id,title,author_name,cover,published_at" //需要的字段名
71-
};
72-
if (this.last_id) { //说明已有数据,目前处于上拉加载
73-
data.minId = this.last_id;
74-
data.time = new Date().getTime() + "";
75-
data.pageSize = 10;
74+
},
75+
fail: (data, code) => {
76+
console.log('fail' + JSON.stringify(data));
7677
}
77-
uni.request({
78-
url: 'https://unidemo.dcloud.net.cn/api/news',
79-
data: data,
80-
success: (data) => {
81-
if (data.statusCode == 200) {
82-
let list = this.setTime(data.data);
83-
this.listData = this.reload ? list : this.listData.concat(list);
84-
this.last_id = list[list.length - 1].id;
85-
this.reload = false;
86-
}
87-
},
88-
fail: (data, code) => {
89-
console.log('fail' + JSON.stringify(data));
78+
});
79+
},
80+
getList() {
81+
var data = {
82+
column: 'id,post_id,title,author_name,cover,published_at' //需要的字段名
83+
};
84+
if (this.last_id) {
85+
//说明已有数据,目前处于上拉加载
86+
this.status = 'loading';
87+
data.minId = this.last_id;
88+
data.time = new Date().getTime() + '';
89+
data.pageSize = 10;
90+
}
91+
uni.request({
92+
url: 'https://unidemo.dcloud.net.cn/api/news',
93+
data: data,
94+
success: data => {
95+
if (data.statusCode == 200) {
96+
let list = this.setTime(data.data);
97+
this.listData = this.reload ? list : this.listData.concat(list);
98+
this.last_id = list[list.length - 1].id;
99+
this.reload = false;
90100
}
91-
})
92-
},
93-
goDetail: function (e) {
94-
// if (!/前|刚刚/.test(e.published_at)) {
95-
// e.published_at = dateUtils.format(e.published_at);
96-
// }
97-
let detail = {
101+
},
102+
fail: (data, code) => {
103+
console.log('fail' + JSON.stringify(data));
104+
}
105+
});
106+
},
107+
goDetail: function(e) {
108+
// if (!/前|刚刚/.test(e.published_at)) {
109+
// e.published_at = dateUtils.format(e.published_at);
110+
// }
111+
let detail = {
112+
author_name: e.author_name,
113+
cover: e.cover,
114+
id: e.id,
115+
post_id: e.post_id,
116+
published_at: e.published_at,
117+
title: e.title
118+
};
119+
uni.navigateTo({
120+
url: '../list2detail-detail/list2detail-detail?detailDate=' + encodeURIComponent(JSON.stringify(detail))
121+
});
122+
},
123+
setTime: function(items) {
124+
var newItems = [];
125+
items.forEach(e => {
126+
newItems.push({
98127
author_name: e.author_name,
99128
cover: e.cover,
100129
id: e.id,
101130
post_id: e.post_id,
102-
published_at: e.published_at,
131+
published_at: dateUtils.format(e.published_at),
103132
title: e.title
104-
}
105-
uni.navigateTo({
106-
url: "../list2detail-detail/list2detail-detail?detailDate=" + encodeURIComponent(JSON.stringify(detail))
107-
})
108-
},
109-
setTime: function (items) {
110-
var newItems = [];
111-
items.forEach((e) => {
112-
newItems.push({
113-
author_name: e.author_name,
114-
cover: e.cover,
115-
id: e.id,
116-
post_id: e.post_id,
117-
published_at: dateUtils.format(e.published_at),
118-
title: e.title
119-
});
120133
});
121-
return newItems;
122-
}
123-
},
134+
});
135+
return newItems;
136+
}
124137
}
138+
};
125139
</script>
126140

127141
<style>
128-
.banner {
129-
height: 360upx;
130-
overflow: hidden;
131-
position: relative;
132-
background-color: #ccc;
133-
}
142+
.banner {
143+
height: 360upx;
144+
overflow: hidden;
145+
position: relative;
146+
background-color: #ccc;
147+
}
134148
135-
.banner-img {
136-
width: 100%;
137-
}
149+
.banner-img {
150+
width: 100%;
151+
}
138152
139-
.banner-title {
140-
max-height: 84upx;
141-
overflow: hidden;
142-
position: absolute;
143-
left: 30upx;
144-
bottom: 30upx;
145-
width: 90%;
146-
font-size: 32upx;
147-
font-weight: 400;
148-
line-height: 42upx;
149-
color: white;
150-
z-index: 11;
151-
}
153+
.banner-title {
154+
max-height: 84upx;
155+
overflow: hidden;
156+
position: absolute;
157+
left: 30upx;
158+
bottom: 30upx;
159+
width: 90%;
160+
font-size: 32upx;
161+
font-weight: 400;
162+
line-height: 42upx;
163+
color: white;
164+
z-index: 11;
165+
}
152166
153-
.uni-media-list-logo {
154-
width: 180upx;
155-
height: 140upx;
156-
}
167+
.uni-media-list-logo {
168+
width: 180upx;
169+
height: 140upx;
170+
}
157171
158-
.uni-media-list-body {
159-
height: auto;
160-
justify-content: space-around;
161-
}
172+
.uni-media-list-body {
173+
height: auto;
174+
justify-content: space-around;
175+
}
162176
163-
.uni-media-list-text-top {
164-
height: 74upx;
165-
font-size: 28upx;
166-
overflow: hidden;
167-
}
177+
.uni-media-list-text-top {
178+
height: 74upx;
179+
font-size: 28upx;
180+
overflow: hidden;
181+
}
168182
169-
.uni-media-list-text-bottom {
170-
display: flex;
171-
flex-direction: row;
172-
justify-content: space-between;
173-
}
174-
</style>
183+
.uni-media-list-text-bottom {
184+
display: flex;
185+
flex-direction: row;
186+
justify-content: space-between;
187+
}
188+
</style>

0 commit comments

Comments
 (0)