list.uvue 5.3 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>
7
		<list-view class="uni-list" refresher-enabled=true @refresherrefresh="onRefresherrefresh"
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
8
			:refresher-triggered="refresherTriggered">
9
			<list-item 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>
22 23
			</list-item>
		</list-view>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
24 25
	</view>
</template>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
26 27 28 29

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

taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
42 43 44 45
	export default {
		data() {
			return {
				refresherTriggered: false,
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
46 47 48 49 50
				banner: {} as Banner,
				listData: [] as Item[],
				last_id: '',
				pageVisible: false
			};
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
51 52
		},
		onLoad() {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
53 54
			this.pageVisible = true;
			this.getBanner();
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
55
			this.getList();
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
56 57 58
		},
		onUnload() {
			this.pageVisible = false;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
59 60
		},
		methods: {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
61 62 63 64 65 66 67
			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){
70 71
							this.refresherTriggered = false
							if (data.statusCode == 200) {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
72
								let result = data.data as UTSJSONObject;
73 74
								this.banner = {
									cover: result["cover"] as string,
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
75
									title: result["title"] as string,
76 77
									post_id: result["post_id"] as string
								} as Banner;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
78
							}
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
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 94
				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 已提交
95
				uni.request({
96 97
					url: url,
					method:"GET",
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
98
					success: (data) => {
99
						if(this.pageVisible){
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
100 101 102 103 104 105 106
							if (data.statusCode == 200) {
								const result = data.data as UTSJSONObject
								let list = this.setTime(result);
								this.listData = list.concat(this.listData);
								this.last_id = listData[0].id + "";
								this.refresherTriggered = false;
							}
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
107 108
						}
					},
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
109
					fail: (res) => {
110
						if(this.pageVisible){
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
111 112
							console.log('fail', res);
							this.refresherTriggered = false
113
						}
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
114 115 116
					}
				});
			},
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
117 118 119 120 121
			goDetail(e: Item) {
				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 已提交
122
				uni.navigateTo({
123
					url: '/pages/component/list/detail/detail?post_id=' + post_id + "&cover=" + cover + "&title=" + title
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
124
				});
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
125 126 127 128 129 130 131
			},
			bannerClick(e:Banner){
				const detail = e;
				const post_id = detail.post_id;
				const cover = detail.cover;
				const title = detail.title;
				uni.navigateTo({
132
					url: '/pages/component/list/detail/detail?post_id=' + post_id + "&cover=" + cover + "&title=" + title
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
133
				});
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
134
			},
135 136 137 138 139 140 141 142 143 144 145 146 147
			setTime(items: UTSJSONObject): Item[] {
				let newItems = [] as Item[];
				items.forEach((res)=>{
					const e = res as UTSJSONObject;
					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);
				});
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
148 149
				return newItems;
			},
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
150 151
			onRefresherrefresh() {
				if(this.pageVisible){
152 153
					this.refresherTriggered = true
					this.getBanner();
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
154
					this.getList();
155
				}
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
156 157
			}
		}
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
158
	};
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
159 160 161
</script>

<style>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
162 163 164
	.banner {
		height: 360rpx;
		overflow: hidden;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
165
		position: relative;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
166 167 168 169
		background-color: #ccc;
	}

	.banner-img {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
170 171 172
		width: 100%;
	}

taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	.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;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
189 190
		display: flex;
		width: 100%;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
191
		flex-direction: row;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
192 193
	}

taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
194 195 196
	.uni-media-list-logo {
		width: 180rpx;
		height: 140rpx;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
197 198
	}

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

	.uni-media-list-text-top {
		/* height: 74rpx; */
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
207 208
		font-size: 28rpx;
		lines:2;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
209 210 211 212 213
		overflow: hidden;
	}

	.uni-media-list-text-bottom {
		display: flex;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
214
		flex-direction: row;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
215
		justify-content: space-between;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
216 217 218 219 220
	}
	
	.uni-media-list-text {
		color: #9D9D9F;
		font-size: 25rpx;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
221 222
	}
</style>