list.uvue 5.4 KB
Newer Older
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
1
<template>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
2 3 4 5
	<view style="width: 100%;height: 100%;">
		<view class="banner">
			<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 34 35

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


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

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

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

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

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

taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
	.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 已提交
224 225
	}
</style>