list.nvue 6.3 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1
<template>
2
	<view class="pages">
3
		<!-- #ifndef H5 -->
4
		<statusBar></statusBar>
5
		<!-- #endif -->
6

7
		<!-- 搜索功能 -->
8 9 10 11 12 13
		<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>

study夏羽's avatar
study夏羽 已提交
14 15 16
		<unicloud-db ref='udb' v-slot:default="{data,pagination,hasMore, loading, error, options}" @error="onqueryerror"
			:collection="colList" :page-size="10" @load="loadData">
			<!-- 基于 uni-list 的页面布局 field="user_id.nickname"-->
17
			<uni-list class="uni-list" :border="false" :style="{height:listHight}">
study夏羽's avatar
study夏羽 已提交
18 19 20 21

				<!-- 作用于app端nvue页面的下拉加载 -->
				<!-- #ifdef APP-NVUE -->
				<refreshBox @refresh="refresh" :loading="loading"></refreshBox>
22
				<!-- #endif -->
study夏羽's avatar
study夏羽 已提交
23

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

DCloud_JSON's avatar
DCloud_JSON 已提交
58 59 60 61
	</view>
</template>

<script>
study夏羽's avatar
update  
study夏羽 已提交
62
	let cdbRef;
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';
study夏羽's avatar
study夏羽 已提交
66 67
	const gps = new Gps(),
		db = uniCloud.database();
68

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

198 199 200 201 202 203 204
<style scoped>
	/* #ifndef APP-NVUE */
	view {
		display: flex;
		box-sizing: border-box;
		flex-direction: column;
	}
study夏羽's avatar
study夏羽 已提交
205

206
	/* #endif */
207
	.pages {
208
		background-color: #FFFFFF;
DCloud_JSON's avatar
DCloud_JSON 已提交
209
	}
210

DCloud_JSON's avatar
DCloud_JSON 已提交
211 212 213 214 215 216
	.avatar {
		width: 200rpx;
		height: 200rpx;
		margin-right: 10rpx;
	}

study夏羽's avatar
study夏羽 已提交
217 218
	.main {
		justify-content: space-between;
study夏羽's avatar
update  
study夏羽 已提交
219
		flex: 1;
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
220 221 222
	}

	.title {
study夏羽's avatar
update  
study夏羽 已提交
223
		font-size: 16px;
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
224 225 226 227 228 229 230 231 232
	}

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

	.author,
	.last_modify_date {
study夏羽's avatar
update  
study夏羽 已提交
233
		font-size: 14px;
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
234 235 236 237 238 239
		color: #999999;
	}

	.uni-search-box {
		background-color: #FFFFFF;
		position: sticky;
240
		height: 50px;
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
241
		top: 0;
DCloud_JSON's avatar
DCloud_JSON 已提交
242 243 244
		left: 0;
		/* #ifndef APP-PLUS */
		z-index: 9;
245 246 247
		/* #endif */
		/* #ifdef MP-WEIXIN */
		width: 580rpx;
DCloud_JSON's avatar
DCloud_JSON 已提交
248
		/* #endif */
249
	}
study夏羽's avatar
study夏羽 已提交
250

251 252 253 254 255 256 257 258 259
	.cover-search-bar {
		height: 50px;
		position: relative;
		top: -50px;
		margin-bottom: -50px;
		/* #ifndef APP-NVUE */
		z-index: 999;
		/* #endif */
	}
260
</style>