list.uvue 5.6 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

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


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

<style>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
169 170 171
	.banner {
		height: 360rpx;
		overflow: hidden;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
172
		position: relative;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
173 174 175 176
		background-color: #ccc;
	}

	.banner-img {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
177 178 179
		width: 100%;
	}

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

taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
201 202 203
	.uni-media-list-logo {
		width: 180rpx;
		height: 140rpx;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
204 205
	}

taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
206 207
	.uni-media-list-body {
		flex: 1;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
208 209 210 211 212 213
		padding-left: 15rpx;
		justify-content: space-around;
	}

	.uni-media-list-text-top {
		/* height: 74rpx; */
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
214 215
		font-size: 28rpx;
		lines:2;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
216 217 218 219 220
		overflow: hidden;
	}

	.uni-media-list-text-bottom {
		display: flex;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
221
		flex-direction: row;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
222
		justify-content: space-between;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
223 224 225 226 227
	}
	
	.uni-media-list-text {
		color: #9D9D9F;
		font-size: 25rpx;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
228 229
	}
</style>