demo.vue
3.7 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
<template>
<view style="margin-top:128rpx">
<wiidz-waterfall ref="Waterfall" :rows="rows" v-on:getHandledRows="getHandledRows"></wiidz-waterfall>
</view>
</template>
<script>
export default {
components: {},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function() {
this.page = {
now: 1,
size: 10,
}
this.newsList = []
this.getPostList()
uni.stopPullDownRefresh()
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: async function() {
if (this.disabledGetMore) return
let lengthBefore = this.rows.length // 翻页
let page = this.page
page.now++
this.page = page
await this.getRows()
this.$refs.Waterfall.startRender()
let lengthNow = this.rows.length
if (lengthBefore == lengthNow) this.disabledGetMore = true
},
mounted() {
this.getRows()
},
onReady() {
},
data() {
return {
page: {
now: 1,
size: 10,
},
disabledGetMore: false,
rows: []
}
},
methods: {
// getrows : 获取数据
async getRows() {
// 加入分页后的数据
let rows = this.rows
let res = await this.simulateRemoteFunc(this.page.now, this.page.size) // 替换成具体的远程的方法
if (!res[0]) {
// 错误处理
console.log("出错啦")
this.disabledGetMore = true
return
}
let temprows = res[1];
rows = rows.concat(temprows); // 拼接老数据
this.rows = rows
},
getHandledRows(rows) {
this.rows = rows
},
// simulateRemoteFunc : 模拟方法
async simulateRemoteFunc(page_now, page_size) {
let that = this
await this.sleep(1) // 模拟等待
if (this.page.now >= 3) {
return [false, "到底了"]
}
let rows = []
for (let i = 1; i <= page_size; i++) {
let id = (page_now - 1) * page_size + i
rows.push({
id: id,
thumbnail: that.getRandomImg(),
title: that.getRandomTitle(id),
user: {
id: 1,
nickname: "test",
avatar: that.getRandomAvatar(),
},
})
}
return [true, rows]
},
getRandomImg() {
let imgURLs = [
"https://anteras-open.oss-cn-hangzhou.aliyuncs.com/imgs/1-1.png",
"https://anteras-open.oss-cn-hangzhou.aliyuncs.com/imgs/1-2.png",
"https://anteras-open.oss-cn-hangzhou.aliyuncs.com/imgs/2-1.png",
"https://anteras-open.oss-cn-hangzhou.aliyuncs.com/imgs/3-4.png",
"https://anteras-open.oss-cn-hangzhou.aliyuncs.com/imgs/4-3.png",
"https://anteras-open.oss-cn-hangzhou.aliyuncs.com/imgs/3-5.png",
"https://anteras-open.oss-cn-hangzhou.aliyuncs.com/imgs/5-3.png",
]
return imgURLs[Math.ceil(Math.random() * 6)]
},
getRandomAvatar() {
let imgURLs = [
"https://anteras-open.oss-cn-hangzhou.aliyuncs.com/imgs/avatar-1.png",
"https://anteras-open.oss-cn-hangzhou.aliyuncs.com/imgs/avatar-2.png",
"https://anteras-open.oss-cn-hangzhou.aliyuncs.com/imgs/avatar-3.png",
"https://anteras-open.oss-cn-hangzhou.aliyuncs.com/imgs/avatar-4.png",
"https://anteras-open.oss-cn-hangzhou.aliyuncs.com/imgs/avatar-5.png",
"https://anteras-open.oss-cn-hangzhou.aliyuncs.com/imgs/avatar-6.png",
"https://anteras-open.oss-cn-hangzhou.aliyuncs.com/imgs/avatar-7.png",
]
return imgURLs[Math.ceil(Math.random() * 7)]
},
getRandomTitle(index) {
let title = "【" + index + "】测试标题-"
let addLength = Math.ceil(Math.random() * 40)
for (let i = 0; i < addLength; i++) {
let ascii = Math.ceil(Math.random() * 122)
title += String.fromCharCode(ascii)
}
return title
},
sleep: function(time) {
return new Promise(function(resolve, reject) {
setTimeout(() => {
resolve();
}, time);
});
},
}
}
</script>
<style>
</style>