list.nvue 6.0 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<template>
	<view class="pages">
		<!-- #ifndef H5 -->
		<statusBar></statusBar>
		<!-- #endif -->

		<!-- 搜索功能 -->
		<view class="uni-search-box">
			<uni-search-bar v-model="keyword" ref="searchBar" radius="100" cancelButton="none" disabled
				:placeholder="inputPlaceholder" />
			<view class="cover-search-bar" @click="searchClick"></view>
		</view>

		<unicloud-db ref='udb' v-slot:default="{data,pagination,hasMore, loading, error, options}" @error="onqueryerror"
DCloud_JSON's avatar
-...  
DCloud_JSON 已提交
15 16
			:collection="colList" :page-size="10"
			>
17
			<!-- 基于 uni-list 的页面布局 field="user_id.nickname"-->
DCloud_JSON's avatar
DCloud_JSON 已提交
18
			<uni-list class="uni-list" :border="false" :style="{height:listHight}">
雪洛's avatar
雪洛 已提交
19 20 21
				
				<!-- 作用于app端nvue页面的下拉加载 -->
				<!-- #ifdef APP-NVUE -->
22
				<refreshBox @refresh="refresh" :loading="loading"></refreshBox>
DCloud_JSON's avatar
DCloud_JSON 已提交
23
				<!-- #endif -->
雪洛's avatar
雪洛 已提交
24

DCloud_JSON's avatar
DCloud_JSON 已提交
25
				<!-- 列表渲染 -->
26
				<uni-list-item :to="'/pages/list/detail?id='+item._id+'&title='+item.title" v-for="(item,index) in data" :key="index">
DCloud_JSON's avatar
DCloud_JSON 已提交
27 28 29 30 31 32 33 34 35
					<!-- 通过header插槽定义列表左侧图片 -->
					<template v-slot:header>
						<image class="avatar" :src="item.avatar" mode="aspectFill"></image>
					</template>
					<!-- 通过body插槽定义布局 -->
					<template v-slot:body>
						<view class="main">
							<text class="title">{{item.title}}</text>
							<view class="info">
36
								<text class="author">{{item.user_id[0]?item.user_id[0].nickname:''}}</text>
DCloud_JSON's avatar
DCloud_JSON 已提交
37 38 39 40 41
								<uni-dateformat class="last_modify_date" :date="item.last_modify_date"
									format="yyyy-MM-dd" :threshold="[60000, 2592000000]" />
							</view>
						</view>
					</template>
雪洛's avatar
雪洛 已提交
42
				</uni-list-item>
43 44 45 46 47
				<!-- 加载状态:上拉加载更多,加载中,没有更多数据了,加载错误 -->
				<!-- #ifdef APP-PLUS -->
				<uni-list-item>
					<template v-slot:body>
				<!-- #endif -->
DCloud_JSON's avatar
DCloud_JSON 已提交
48 49
						<uni-load-state @networkResume="refresh" :state="{data,pagination,hasMore, loading, error}"
							@loadMore="loadMore">
50 51 52 53 54
						</uni-load-state>
				<!-- #ifdef APP-PLUS -->
					</template>
				</uni-list-item>
				<!-- #endif -->
DCloud_JSON's avatar
DCloud_JSON 已提交
55
			</uni-list>
56 57
		</unicloud-db>
		
DCloud_JSON's avatar
DCloud_JSON 已提交
58 59 60 61
	</view>
</template>

<script>
62
	let cdbRef;
DCloud_JSON's avatar
DCloud_JSON 已提交
63 64 65
	import statusBar from "@/uni_modules/uni-nav-bar/components/uni-nav-bar/uni-status-bar";

	import Gps from '@/uni_modules/json-gps/js_sdk/gps.js';
DCloud_JSON's avatar
-...  
DCloud_JSON 已提交
66
	const gps = new Gps(),db = uniCloud.database();
DCloud_JSON's avatar
DCloud_JSON 已提交
67 68 69 70 71 72 73 74 75 76 77 78

	export default {
		components: {
			statusBar
		},
		computed: {
			inputPlaceholder(e) {
				if (uni.getStorageSync('CURRENT_LANG') == "en") {
					return 'Please enter the search content'
				} else {
					return '请输入搜索内容'
				}
DCloud_JSON's avatar
-...  
DCloud_JSON 已提交
79 80 81 82
			},
			colList(){
				return [
					db.collection('opendb-news-articles').where(this.where).field('avatar,title,last_modify_date,user_id').getTemp(),
83
					db.collection('uni-id-users').field('_id,nickname').getTemp()
DCloud_JSON's avatar
-...  
DCloud_JSON 已提交
84
				]
DCloud_JSON's avatar
DCloud_JSON 已提交
85 86 87
			}
		},
		data() {
DCloud_JSON's avatar
-...  
DCloud_JSON 已提交
88 89
			return {
				where: '"article_status" == 1',
DCloud_JSON's avatar
DCloud_JSON 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
				keyword: "",
				showRefresh: false,
				listHight: 0
			}
		},
		watch: {
			keyword(keyword, oldValue) {
				let where = '"article_status" == 1 '
				if (keyword) {
					this.where = where + `&& /${keyword}/.test(title)`;
				} else {
					this.where = where;
				}
			}
		},
105
		async onReady() {
DCloud_JSON's avatar
DCloud_JSON 已提交
106 107
			// #ifdef APP-NVUE
			/* 可用窗口高度 - 搜索框高 - 状态栏高 */
108
			this.listHight = uni.getSystemInfoSync().windowHeight - uni.getSystemInfoSync().statusBarHeight - 50 + 'px';
DCloud_JSON's avatar
DCloud_JSON 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
			// #endif
			// #ifndef APP-NVUE
			this.listHight = 'auto'
			// #endif
			cdbRef = this.$refs.udb
		},
		async onShow() {
			this.keyword = getApp().globalData.searchText
			getApp().globalData.searchText = ''
			//这里仅演示如何,在onShow生命周期获取设备位置,并在设备或者应用没有权限时自动引导。设置完毕自动重新获取。
			//你可以基于他做自己的业务,比如:根据距离由近到远排序列表数据等
			// uni.showLoading({
			// 	title:"获取定位中"
			// });
			//默认h5端不获取定位
			// #ifndef H5
			let location = await gps.getLocation({
				geocode: true
			})
128
			// console.log(location);
DCloud_JSON's avatar
DCloud_JSON 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
			// #endif
			// if(location){
			// 	uni.showToast({
			// 		title: JSON.stringify(location),
			// 		icon: 'none'
			// 	});
			// }
			// uni.hideLoading()
		},
		methods: {
			searchClick(e) { //点击搜索框
				uni.hideKeyboard();
				uni.navigateTo({
					url: '/pages/list/search/search',
					animationType: 'fade-in'
				});
			},
			retry() {
				this.refresh()
			},
			refresh() {
				cdbRef.loadData({
					clear: true
				}, () => {
雪洛's avatar
雪洛 已提交
153 154 155 156
					uni.stopPullDownRefresh()
					// #ifdef APP-NVUE
					this.showRefresh = false
					// #endif
DCloud_JSON's avatar
DCloud_JSON 已提交
157 158 159 160 161 162 163 164 165 166 167
					console.log('end');
				})
				console.log('refresh');
			},
			loadMore() {
				cdbRef.loadMore()
			},
			onqueryerror(e) {
				console.error(e);
			},
			onpullingdown(e) {
雪洛's avatar
雪洛 已提交
168 169 170 171
				console.log(e);
				this.showRefresh = true
				if(e.pullingDistance>100){
					this.refresh()
DCloud_JSON's avatar
DCloud_JSON 已提交
172 173 174
				}
			}
		},
雪洛's avatar
雪洛 已提交
175 176 177 178 179 180 181
		// #ifndef APP-NVUE
		onPullDownRefresh() {
			this.refresh()
		},
		onReachBottom() {
			this.loadMore()
		}
DCloud_JSON's avatar
DCloud_JSON 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
		// #endif
	}
</script>

<style scoped>
	/* #ifndef APP-NVUE */
	view {
		display: flex;
		box-sizing: border-box;
		flex-direction: column;
	}
	/* #endif */
	.pages {
		background-color: #FFFFFF;
	}

	.avatar {
		width: 200rpx;
		height: 200rpx;
		margin-right: 10rpx;
	}

204 205 206
	.main {
		justify-content: space-between;
		flex: 1;
DCloud_JSON's avatar
DCloud_JSON 已提交
207 208 209
	}

	.title {
210
		font-size: 16px;
DCloud_JSON's avatar
DCloud_JSON 已提交
211 212 213 214 215 216 217 218 219
	}

	.info {
		flex-direction: row;
		justify-content: space-between;
	}

	.author,
	.last_modify_date {
220
		font-size: 14px;
DCloud_JSON's avatar
DCloud_JSON 已提交
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
		color: #999999;
	}

	.uni-search-box {
		background-color: #FFFFFF;
		position: sticky;
		height: 50px;
		top: 0;
		left: 0;
		/* #ifndef APP-PLUS */
		z-index: 9;
		/* #endif */
		/* #ifdef MP-WEIXIN */
		width: 580rpx;
		/* #endif */
	}
	.cover-search-bar {
		height: 50px;
		position: relative;
		top: -50px;
		margin-bottom: -50px;
		/* #ifndef APP-NVUE */
		z-index: 999;
		/* #endif */
	}
雪洛's avatar
雪洛 已提交
246
</style>