list.uvue 5.8 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 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
		<list class="uni-list" refresher-enabled=true @refresherrefresh="onRefresherrefresh"
			:refresher-triggered="refresherTriggered">
			<cell v-for="(value, index) in listData" :key="index">
				<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>
			</cell>
			<cell class="footer"></cell>
			
		</list>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
26 27
	</view>
</template>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
28 29 30 31 32 33

<script>
	import JSONObject from 'com.alibaba.fastjson.JSONObject';
	import JSONArray from 'com.alibaba.fastjson.JSONArray';
	type Banner = {
		cover: string | null,
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();
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,
78 79 80 81 82 83 84 85 86 87 88
					success: data => {
						if(this.pageVisible){
							this.refresherTriggered = false
							if (data.statusCode == 200) {
								let result = data.data as UTSJSONObject;
								this.banner = {
									cover: result["cover"] as string,
									title: result["title"] as string,
									post_id: result["post_id"] as string
								} as Banner;
							}
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
89 90 91 92 93 94 95 96
						}
					},
					fail: (e) => {
						console.log('fail', e);
					}
				});
			},
			getList() {
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 108 109 110 111 112 113 114 115 116 117
					url: url,
					method:"GET",
					success: (data) => {
						if(this.pageVisible){
							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
						}
					},
120 121 122 123 124
					fail: (res) => {
						if(this.pageVisible){
							console.log('fail', res);
							this.refresherTriggered = false
						}
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
125 126 127 128 129 130 131 132 133 134 135
					}
				});
			},
			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
				});
136 137 138 139 140 141 142 143 144
			},
			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 已提交
145 146 147 148 149
			},
			setTime(items: UTSJSONObject): Item[] {
				let newItems = [] as Item[];
				if (items.isJSONArray()) {
					const array = items.toJSONObject() as JSONArray;
150
					for (let i = array.size - 1; i >= 0; i--) {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164
						const e = array.get(i) as JSONObject;
						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;
			},
			onRefresherrefresh() {
165 166 167 168 169
				if(this.pageVisible){
					this.refresherTriggered = true
					this.getBanner();
					this.getList();
				}
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 198 199 200 201 202 203
	.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;
		z-index: 11;
	}

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

taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
	.uni-media-list-body {
		flex: 1;
		padding-left: 15rpx;
		justify-content: space-around;
	}

	.uni-media-list-text-top {
		/* height: 74rpx; */
		font-size: 28rpx;
		lines:2;
		overflow: hidden;
	}

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