list-news.uvue 5.4 KB
Newer Older
W
微调  
wanganxp 已提交
1 2 3 4 5 6 7 8
<template>
	<view style="width: 100%;height: 100%;">
		<view class="banner" @click="bannerClick(banner)">
			<image class="banner-img" :src="banner.cover"></image>
			<text class="banner-title">{{ banner.title }}</text>
		</view>
		<list-view class="uni-list-content" refresher-enabled=true @refresherrefresh="onRefresherrefresh"
			:refresher-triggered="refresherTriggered" scroll-y = true>
9
			<list-item v-for="(value, index) in listData" :key="index">
W
微调  
wanganxp 已提交
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>
22
			</list-item>
W
微调  
wanganxp 已提交
23 24 25 26
		</list-view>
	</view>
</template>

27
<script>
W
微调  
wanganxp 已提交
28 29
	type Banner = {
		cover: string | null,
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
30 31
		title: string | null,
		post_id: string | null
W
微调  
wanganxp 已提交
32 33 34 35 36 37 38 39 40
	}
	type Item = {
		author_name: string,
		cover: string,
		id: number,
		post_id: string,
		published_at: string,
		title: string
	}
41

W
微调  
wanganxp 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54
	export default {
		data() {
			return {
				refresherTriggered: false,
				banner: {} as Banner,
				listData: [] as Item[],
				last_id: '',
				pageVisible: false
			};
		},
		onLoad() {
			this.pageVisible = true;
			this.getBanner();
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
55
			this.getList();
W
微调  
wanganxp 已提交
56 57 58 59 60 61 62 63 64 65 66 67
		},
		onUnload() {
			this.pageVisible = false;
		},
		methods: {
			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,
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
68 69
					success: data => {
						if(this.pageVisible){
W
微调  
wanganxp 已提交
70 71
							this.refresherTriggered = false
							if (data.statusCode == 200) {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
72
								let result = data.data as UTSJSONObject;
W
微调  
wanganxp 已提交
73 74
								this.banner = {
									cover: result["cover"] as string,
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
75
									title: result["title"] as string,
W
微调  
wanganxp 已提交
76 77
									post_id: result["post_id"] as string
								} as Banner;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
78
							}
W
微调  
wanganxp 已提交
79 80 81 82 83 84 85 86
						}
					},
					fail: (e) => {
						console.log('fail', e);
					}
				});
			},
			getList() {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
87 88 89 90 91 92 93
				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;
				}
W
微调  
wanganxp 已提交
94 95 96 97

				uni.request({
					url: url,
					method:"GET",
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
98
					success: (data) => {
W
微调  
wanganxp 已提交
99
						if(this.pageVisible){
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
100
							if (data.statusCode == 200) {
101 102 103 104 105 106
								const result = data.data as UTSJSONObject[]
								if(result != null){
									let list = this.setTime(result);
									this.listData = list.concat(this.listData);
									this.last_id = listData[0].id + "";
								}
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
107 108
								this.refresherTriggered = false;
							}
W
微调  
wanganxp 已提交
109 110
						}
					},
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
111
					fail: (res) => {
W
微调  
wanganxp 已提交
112
						if(this.pageVisible){
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
113 114
							console.log('fail', res);
							this.refresherTriggered = false
W
微调  
wanganxp 已提交
115 116 117 118
						}
					}
				});
			},
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
119 120 121 122 123
			goDetail(e: Item) {
				const detail = e;
				const post_id = detail.post_id;
				const cover = detail.cover;
				const title = detail.title;
W
微调  
wanganxp 已提交
124 125 126
				uni.navigateTo({
					url: '/pages/template/list-news/detail/detail?post_id=' + post_id + "&cover=" + cover + "&title=" + title
				});
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
127 128 129 130 131 132 133
			},
			bannerClick(e:Banner){
				const detail = e;
				const post_id = detail.post_id;
				const cover = detail.cover;
				const title = detail.title;
				uni.navigateTo({
W
微调  
wanganxp 已提交
134
					url: '/pages/template/list-news/detail/detail?post_id=' + post_id + "&cover=" + cover + "&title=" + title
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
135
				});
W
微调  
wanganxp 已提交
136
			},
137
			setTime(items: UTSJSONObject[]): Item[] {
138
				let newItems = [] as Item[];
139 140
				for(const item in items){
					const e = item;
141 142 143 144 145 146 147 148
					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);
149
				}
W
微调  
wanganxp 已提交
150 151
				return newItems;
			},
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
152 153
			onRefresherrefresh() {
				if(this.pageVisible){
W
微调  
wanganxp 已提交
154 155
					this.refresherTriggered = true
					this.getBanner();
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
156
					this.getList();
157
				}
W
微调  
wanganxp 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
			}
		}
	};
</script>

<style>
	.banner {
		height: 360rpx;
		overflow: hidden;
		position: relative;
		background-color: #ccc;
	}

	.banner-img {
		width: 100%;
	}

	.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;
	}

	.uni-media-list {
		padding: 22rpx 30rpx;
		box-sizing: border-box;
		display: flex;
		width: 100%;
		flex-direction: row;
	}

	.uni-media-list-logo {
		width: 180rpx;
		height: 140rpx;
	}

taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
201 202
	.uni-media-list-body {
		flex: 1;
W
微调  
wanganxp 已提交
203 204
		padding-left: 15rpx;
		justify-content: space-around;
205
	}
W
微调  
wanganxp 已提交
206

207 208 209 210 211 212 213
	.uni-list-content{
		background-color: #FFFFFF;
		position: relative;
		display: flex;
		flex-direction: column;
		border-bottom: 1px solid #c8c7cc;
		flex: 1;
W
微调  
wanganxp 已提交
214 215 216 217
	}

	.uni-media-list-text-top {
		/* height: 74rpx; */
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
218 219
		font-size: 28rpx;
		lines:2;
W
微调  
wanganxp 已提交
220 221 222 223 224
		overflow: hidden;
	}

	.uni-media-list-text-bottom {
		display: flex;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
225
		flex-direction: row;
W
微调  
wanganxp 已提交
226
		justify-content: space-between;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
227
	}
W
微调  
wanganxp 已提交
228

taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
229 230 231
	.uni-media-list-text {
		color: #9D9D9F;
		font-size: 25rpx;
W
微调  
wanganxp 已提交
232 233
	}
</style>