list.uvue 5.9 KB
Newer Older
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
1
<template>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
2
	<view style="width: 100%;height: 100%;">
3
		<view class="banner" @click="bannerClick(banner)">
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
4 5
			<image class="banner-img" :src="banner.cover"></image>
			<text class="banner-title">{{ banner.title }}</text>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
6
		</view>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
7 8
		<list class="uni-list" refresher-enabled=true @refresherrefresh="onRefresherrefresh"
			:refresher-triggered="refresherTriggered">
Y
yurj26 已提交
9
			<cell v-for="(value, index) in listData" :key="index">
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
10 11 12 13 14 15 16 17 18 19 20 21
				<view class="uni-list-cell" hover-class="uni-list-cell-hover" @click="goDetail(value)">
					<view class="uni-media-list">
						<image class="uni-media-list-logo" :src="value.cover"></image>
						<view class="uni-media-list-body">
							<text class="uni-media-list-text-top">{{ value.title }}</text>
							<view class="uni-media-list-text-bottom">
								<text class="uni-media-list-text">{{ value.author_name }}</text>
								<text class="uni-media-list-text">{{ value.published_at }}</text>
							</view>
						</view>
					</view>
				</view>
Y
yurj26 已提交
22 23
			</cell>
			<cell class="footer"></cell>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
24 25
			
		</list>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
26 27
	</view>
</template>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
28 29

<script>
Y
yurj26 已提交
30
	import JSONObject from 'com.alibaba.fastjson.JSONObject';
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
31 32 33
	import JSONArray from 'com.alibaba.fastjson.JSONArray';
	type Banner = {
		cover: string | null,
Y
yurj26 已提交
34 35
		title: string | null,
		post_id: string | null
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
36
	}
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
37
	type Item = {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
38 39 40 41 42 43
		author_name: string,
		cover: string,
		id: number,
		post_id: string,
		published_at: string,
		title: string
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
44
	}
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
45 46


taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
47 48 49 50
	export default {
		data() {
			return {
				refresherTriggered: false,
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
51 52 53 54 55 56 57 58 59 60
				banner: {} as Banner,
				listData: [] as Item[],
				last_id: '',
				contentText: {
					contentdown: '上拉加载更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
				pageVisible: false
			};
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
61 62
		},
		onLoad() {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
63 64
			this.pageVisible = true;
			this.getBanner();
Y
yurj26 已提交
65
			this.getList();
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
66 67 68
		},
		onUnload() {
			this.pageVisible = false;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
69 70
		},
		methods: {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
71 72 73 74 75 76 77
			getBanner() {
				let data = {
					column: 'id,post_id,title,author_name,cover,published_at' //需要的字段名
				};
				uni.request({
					url: 'https://unidemo.dcloud.net.cn/api/banner/36kr',
					data: data,
Y
yurj26 已提交
78 79
					success: data => {
						if(this.pageVisible){
80 81
							this.refresherTriggered = false
							if (data.statusCode == 200) {
Y
yurj26 已提交
82
								let result = data.data as UTSJSONObject;
83 84
								this.banner = {
									cover: result["cover"] as string,
Y
yurj26 已提交
85
									title: result["title"] as string,
86 87
									post_id: result["post_id"] as string
								} as Banner;
Y
yurj26 已提交
88
							}
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
89 90 91 92 93 94 95 96
						}
					},
					fail: (e) => {
						console.log('fail', e);
					}
				});
			},
			getList() {
Y
yurj26 已提交
97 98 99 100 101 102 103 104
				let url = "https://unidemo.dcloud.net.cn/api/news?column=id,post_id,title,author_name,cover,published_at";
				if (this.last_id != "") {
					const minId = (this.last_id).toInt();
					const time = new Date().getTime() + '';
					const pageSize = 10;
					url = url + "&minId=" + minId + "&time=" + time + "&pageSize=" + pageSize;
				}
				
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
105
				uni.request({
106 107
					url: url,
					method:"GET",
Y
yurj26 已提交
108
					success: (data) => {
109
						if(this.pageVisible){
Y
yurj26 已提交
110 111 112 113 114 115 116 117
							if (data.statusCode == 200) {
								const result = data.data as UTSJSONObject
								let list = this.setTime(result);
								this.listData = list.concat(this.listData);
								console.log("after ",this.listData.size);
								this.last_id = listData[0].id + "";
								this.refresherTriggered = false;
							}
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
118 119
						}
					},
Y
yurj26 已提交
120
					fail: (res) => {
121
						if(this.pageVisible){
Y
yurj26 已提交
122 123
							console.log('fail', res);
							this.refresherTriggered = false
124
						}
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
125 126 127
					}
				});
			},
Y
yurj26 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141
			goDetail(e: Item) {
				const detail = e;
				const post_id = detail.post_id;
				const cover = detail.cover;
				const title = detail.title;
				uni.navigateTo({
					url: 'pages/component/list/detail/detail?post_id=' + post_id + "&cover=" + cover + "&title=" + title
				});
			},
			bannerClick(e:Banner){
				const detail = e;
				const post_id = detail.post_id;
				const cover = detail.cover;
				const title = detail.title;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
142 143 144 145 146 147
				uni.navigateTo({
					url: 'pages/component/list/detail/detail?post_id=' + post_id + "&cover=" + cover + "&title=" + title
				});
			},
			setTime(items: UTSJSONObject): Item[] {
				let newItems = [] as Item[];
Y
yurj26 已提交
148
				if (items.isJSONArray()) {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
149
					const array = items.toJSONObject() as JSONArray;
150
					for (let i = array.size - 1; i >= 0; i--) {
Y
yurj26 已提交
151
						const e = array.get(i) as JSONObject;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
152 153 154 155 156 157 158 159 160 161 162 163
						newItems.push({
								author_name: e["author_name"] as string,
								cover: e["cover"] as string,
								id: e["id"] as number,
								post_id: e["post_id"] as string,
								published_at: e["published_at"] as string,
								title: e["title"] as string
							} as Item);
					}
				}
				return newItems;
			},
Y
yurj26 已提交
164 165
			onRefresherrefresh() {
				if(this.pageVisible){
166 167
					this.refresherTriggered = true
					this.getBanner();
Y
yurj26 已提交
168
					this.getList();
169
				}
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
170 171
			}
		}
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
172
	};
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
173 174 175
</script>

<style>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
176 177 178
	.banner {
		height: 360rpx;
		overflow: hidden;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
179
		position: relative;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
180 181 182 183
		background-color: #ccc;
	}

	.banner-img {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
184 185 186
		width: 100%;
	}

taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
187 188 189 190 191 192 193 194 195 196 197
	.banner-title {
		max-height: 84rpx;
		overflow: hidden;
		position: absolute;
		left: 30rpx;
		bottom: 30rpx;
		width: 90%;
		font-size: 32rpx;
		font-weight: 400;
		line-height: 42rpx;
		color: white;
Y
yurj26 已提交
198
		/* z-index: 11; */
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
199 200 201 202 203
	}

	.uni-media-list {
		padding: 22rpx 30rpx;
		box-sizing: border-box;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
204 205
		display: flex;
		width: 100%;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
206
		flex-direction: row;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
207 208
	}

taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
209 210 211
	.uni-media-list-logo {
		width: 180rpx;
		height: 140rpx;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
212 213
	}

Y
yurj26 已提交
214 215
	.uni-media-list-body {
		flex: 1;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
216 217 218 219 220 221
		padding-left: 15rpx;
		justify-content: space-around;
	}

	.uni-media-list-text-top {
		/* height: 74rpx; */
Y
yurj26 已提交
222 223
		font-size: 28rpx;
		lines:2;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
224 225 226 227 228
		overflow: hidden;
	}

	.uni-media-list-text-bottom {
		display: flex;
Y
yurj26 已提交
229
		flex-direction: row;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
230
		justify-content: space-between;
Y
yurj26 已提交
231 232 233 234 235
	}
	
	.uni-media-list-text {
		color: #9D9D9F;
		font-size: 25rpx;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
236 237
	}
</style>