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>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
7 8
		<list class="uni-list" refresher-enabled=true @refresherrefresh="onRefresherrefresh"
			:refresher-triggered="refresherTriggered">
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
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>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
22
			</cell>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
23
		</list>
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 122
			goDetail(e: Item) {
				console.log("eeee",e);
				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 已提交
123 124 125
				uni.navigateTo({
					url: 'pages/component/list/detail/detail?post_id=' + post_id + "&cover=" + cover + "&title=" + title
				});
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
126 127 128 129 130 131 132 133 134
			},
			bannerClick(e:Banner){
				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
				});
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
135
			},
136 137 138 139 140 141 142 143 144 145 146 147 148
			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 已提交
149 150
				return newItems;
			},
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
151 152
			onRefresherrefresh() {
				if(this.pageVisible){
153 154
					this.refresherTriggered = true
					this.getBanner();
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
155
					this.getList();
156
				}
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
157 158
			}
		}
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
159
	};
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
160 161 162
</script>

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

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

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

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

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

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

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