news-list.vue 8.1 KB
Newer Older
芊里 已提交
1 2 3 4 5 6 7 8
<template>
	<!--
	本页面模板教程:https://ext.dcloud.net.cn/plugin?id=2651
	uni-list 文档:https://ext.dcloud.net.cn/plugin?id=24
	uniCloud 文档:https://uniapp.dcloud.io/uniCloud/README
	uni-clientDB 组件文档:https://uniapp.dcloud.net.cn/uniCloud/uni-clientdb-component
	DB Schema 规范:https://uniapp.dcloud.net.cn/uniCloud/schema
	 -->
芊里 已提交
9 10
	<view style="overflow: hidden;">
		<view class="search-box">
芊里 已提交
11
			<!-- #ifdef APP-PLUS -->
芊里 已提交
12
			<status-bar class="status-bar"></status-bar>
芊里 已提交
13
			<!-- #endif -->
芊里 已提交
14
			<view class="search-container-bar">
芊里 已提交
15
				<uni-search-bar ref="searchBar" style="flex:1;" radius="100" v-model="searchText" @click.native="searchClick" cancelButton="none" disabled />
芊里 已提交
16
			</view>
芊里 已提交
17
		</view>
芊里 已提交
18
		<view class="list">
芊里 已提交
19 20 21
			<!-- #ifdef APP-PLUS -->
			<status-bar></status-bar>
			<!-- #endif -->
芊里 已提交
22 23 24 25 26 27
			<!-- 刷新页面后的顶部提示框 -->
			<!-- 当前弹出内容没有实际逻辑 ,可根据当前业务修改弹出提示 -->
			<view class="tips" :class="{ 'tips-ani': tipShow }">为您更新了10条内容</view>
			<!-- 页面分类标题 -->
			<uni-section style="margin:0;" v-if="searchText" :title="listTitle" type="line"></uni-section>
			<unicloud-db ref="udb" v-slot:default="{data, loading, error, options}" :options="formData"
芊里 已提交
28
				:collection="collection" :field="field" :where="where" @load="load" @error="isLoading = false">
芊里 已提交
29 30 31 32 33
				<text v-if="error" class="list-info">{{error.message}}</text>
				<!-- 基于 uni-list 的页面布局 -->
				<uni-list :class="{ 'uni-list--waterfall': options.waterfall }">
					<!-- 通过 uni-list--waterfall 类决定页面布局方向 -->
					<!-- to 属性携带参数跳转详情页面,当前只为参考 -->
34
					<uni-list-item :border="!options.waterfall" :to="'./detail?id='+item._id+'&title='+item.title" class="uni-list-item--waterfall" title="自定义列表"
芊里 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
						v-for="item in data" :key="item._id">
						<!-- 通过header插槽定义列表左侧图片 -->
						<template v-slot:header>
							<view class="uni-thumb shop-picture" :class="{ 'shop-picture-column': options.waterfall }">
								<image :src="item.avatar" mode="aspectFill"></image>
							</view>
						</template>
						<!-- 通过body插槽定义布局 -->
						<view slot="body" class="shop">
							<view>
								<view class="uni-title">
									<text class="uni-ellipsis-2">{{ item.title }}</text>
								</view>
							</view>
							<view>
								<view class="uni-note ellipsis">
									<text class="uni-ellipsis-1">{{ item.author[0].username }}</text>
									<text>{{ item.comment_count }}评论</text>
									<uni-dateformat :date="item.last_modify_date" format="yyyy-MM-dd" :threshold="[60000, 2592000000]"/>
								</view>
							</view>
						</view>
					</uni-list-item>
				</uni-list>
				<!-- 通过 loadMore 组件实现上拉加载效果,如需自定义显示内容,可参考:https://ext.dcloud.net.cn/plugin?id=29 -->
				<uni-load-more v-if="!error && (loading || options.status === 'noMore') " :status="options.status" />
芊里 已提交
61
				<uni-nodata v-if="data.length == 0" :isLoading="isLoading" @retry="refresh"></uni-nodata>
芊里 已提交
62 63 64 65 66
			</unicloud-db>
		</view>
	</view>
</template>

芊里 已提交
67 68
<script>
	import statusBar from '@/uni_modules/uni-nav-bar/components/uni-nav-bar/uni-status-bar.vue';
芊里 已提交
69
	export default {
芊里 已提交
70 71 72
		components:{
			statusBar
		},
芊里 已提交
73
		props:{
芊里 已提交
74
			currentText:{
芊里 已提交
75 76
				type:String,
				default:''
芊里 已提交
77 78 79 80
			},
			canSearch:{
				type:Boolean,
				default:false
芊里 已提交
81 82 83 84
			}
		},
		data() {
			return {
芊里 已提交
85
				searchText: this.currentText || '',
芊里 已提交
86 87 88 89 90 91 92 93 94
				formData: {
					waterfall: false, // 布局方向切换
					status: 'loading', // 加载状态
				},
				where: '',
				// 数据表名
				collection: 'opendb-news-articles,uni-id-users',
				// 查询字段,多个字段用 , 分割
				field: 'author{username, _id}, user_id,_id,avatar,title,excerpt,last_modify_date, comment_count, like_count',
芊里 已提交
95 96
				tipShow: false ,// 是否显示顶部提示框
				isLoading:true
芊里 已提交
97
			};
芊里 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
		},
		/**
		 * 作为页面出现时的生命周期
		 */
		onShow(options) {
			this.searchText = getApp().globalData.searchText;
		},
		/**
		 * 下拉刷新回调函数
		 */
		onPullDownRefresh() {
			this.refresh();
		},
		onReachBottom() {
			this.loadMore();
芊里 已提交
113 114 115
		},
		methods: {
			/**
116
			 * 切换列表布局方向
芊里 已提交
117 118 119 120 121 122 123
			 */
			select() {
				this.formData.waterfall = !this.formData.waterfall;
			},
			/**
			 * 下拉刷新回调函数
			 */
芊里 已提交
124
			refresh() {
芊里 已提交
125
				this.tipShow = true
芊里 已提交
126 127
				this.formData.status = 'more'
				this.isLoading = true
芊里 已提交
128 129 130
				this.$refs.udb.loadData({
					clear: true
				}, () => {
芊里 已提交
131 132
					this.tipShow = false
					this.isLoading = false
芊里 已提交
133 134 135 136 137 138
					uni.stopPullDownRefresh()
				})
			},
			/**
			 * 上拉加载回调函数
			 */
芊里 已提交
139
			loadMore() {
芊里 已提交
140 141
				this.$refs.udb.loadMore()
			},
芊里 已提交
142 143
			load(data, ended) {
				this.isLoading = false
芊里 已提交
144 145 146 147 148
				if (ended) {
					this.formData.status = 'noMore'
				}
			},
			searchClick() {
芊里 已提交
149 150 151 152 153
				uni.hideKeyboard();
				
				
				if(this.canSearch){
					uni.navigateTo({
芊里 已提交
154
						url: '/pages/list/search/search',
芊里 已提交
155 156 157 158 159
						animationType: 'fade-in'
					});
				}else {
					uni.navigateBack();
				}
芊里 已提交
160 161 162 163 164 165 166 167 168 169 170 171
			}
		},
		computed: {
			listTitle() {
				if (this.searchText) return '搜索结果';
				return '';
			}
		}
	};
</script>

<style lang="scss" scoped>
芊里 已提交
172
	@import '@/common/uni-ui.scss';
芊里 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248

	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #efeff4;
		min-height: 100%;
		height: auto;
	}

	.tips {
		color: #67c23a;
		font-size: 14px;
		line-height: 40px;
		text-align: center;
		background-color: #f0f9eb;
		height: 0;
		opacity: 0;
		transform: translateY(-100%);
		transition: all 0.3s;
	}

	.tips-ani {
		transform: translateY(0);
		height: 40px;
		opacity: 1;
	}

	.shop {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}

	.shop-picture {
		width: 110px;
		height: 110px;
	}

	.shop-picture-column {
		width: 100%;
		height: 170px;
		margin-bottom: 10px;
	}
	
	.ellipsis {
		display: flex;
		overflow: hidden;
	}

	.uni-ellipsis-1 {
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}

	.uni-ellipsis-2 {
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
	}
	.uni-note{
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		flex-wrap: nowrap;
	}

	// 默认加入 scoped ,所以外面加一层提升权重
	.list {
芊里 已提交
249
		margin-top: 52px;
芊里 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285

		.uni-list--waterfall {

			/* #ifndef H5 || APP-VUE */
			// 小程序 编译后会多一层标签,而其他平台没有,所以需要特殊处理一下
			/deep/ .uni-list {
				/* #endif */
				display: flex;
				flex-direction: row;
				flex-wrap: wrap;
				padding: 5px;
				box-sizing: border-box;

				/* #ifdef H5 || APP-VUE */
				// h5 和 app-vue 使用深度选择器,因为默认使用了 scoped ,所以样式会无法穿透
				/deep/
				/* #endif */
				.uni-list-item--waterfall {
					width: 50%;
					box-sizing: border-box;

					.uni-list-item__container {
						padding: 5px;
						flex-direction: column;
					}
				}

				/* #ifndef H5 || APP-VUE */
			}

			/* #endif */
		}
	}

	.search-icons {
		padding: 16rpx;
芊里 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298 299
	}
	
	.search-box{
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		justify-content: center;
		align-items: center;
		position: fixed;
		left: 0;
		right: 0;
		z-index: 10;
		background-color: #fff;
芊里 已提交
300 301 302 303 304 305 306 307
	}

	.search-container-bar {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: center;
芊里 已提交
308 309
		align-items: center;
		width: 750rpx;
芊里 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
	}

	/* #ifndef APP-NVUE */
	/deep/
	/* #endif */
	.uni-searchbar__box {
		border-width: 0;
	}

	/* #ifndef APP-NVUE */
	/deep/
	/* #endif */
	.uni-input-placeholder {
		font-size: 28rpx;
	}

	.list-info {
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
		flex: 1;
		text-align: center;
		font-size: 26rpx;
		color: #808080;
		margin-top: 20rpx;
	}
</style>