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>

14
		<unicloud-db ref='udb' v-slot:default="{data,pagination,hasMore, loading, error, options}" @error="onqueryerror"
DCloud_JSON's avatar
-...  
DCloud_JSON 已提交
15
			:collection="colList" :page-size="10"
study夏羽's avatar
study夏羽 已提交
16
			>
DCloud_JSON's avatar
-...  
DCloud_JSON 已提交
17
			<!-- 基于 uni-list 的页面布局 field="user_id.username"-->
18 19 20 21 22 23 24
			<uni-list class="uni-list" :border="false" :style="{height:listHight}">
				
				<!-- 作用于app端nvue页面的下拉加载 -->
				<!-- #ifdef APP-NVUE -->
				<refreshBox @refresh="refresh" :loading="loading"></refreshBox>
				<!-- #endif -->
				<!-- 列表渲染 -->
25
				<uni-list-item :to="'/pages/list/detail?id='+item._id+'&title='+item.title" v-for="(item,index) in data"
26
					: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">
DCloud_JSON's avatar
-...  
DCloud_JSON 已提交
36
								<text class="author">{{JSON.stringify(item.user_id[0]?.username)}}</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>
42
				</uni-list-item>
43
				<!-- 加载状态:上拉加载更多,加载中,没有更多数据了,加载错误 -->
44
				<!-- #ifdef APP-PLUS -->
45
				<uni-list-item>
DCloud_JSON's avatar
DCloud_JSON 已提交
46
					<template v-slot:body>
47
				<!-- #endif -->
48 49
						<uni-load-state @networkResume="refresh" :state="{data,pagination,hasMore, loading, error}"
							@loadMore="loadMore">
50
						</uni-load-state>
51
				<!-- #ifdef APP-PLUS -->
52
					</template>
53
				</uni-list-item>
54
				<!-- #endif -->
55
			</uni-list>
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
56
		</unicloud-db>
DCloud_JSON's avatar
DCloud_JSON 已提交
57 58 59 60
	</view>
</template>

<script>
61
	var cdbRef, currentWebview;
62 63 64
	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 已提交
65
	const gps = new Gps(),db = uniCloud.database();
66

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

165
					console.log('end');
166 167
				})
				console.log('refresh');
168
			},
169 170 171
			loadMore() {
				cdbRef.loadMore()
			},
172
			onqueryerror(e) {
173
				console.error(e);
174 175 176 177 178 179 180
			},
			onpullingdown(e) {
				console.log(e);
				this.showRefresh = true
				if(e.pullingDistance>100){
					this.refresh()
				}
181
			}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
182
		},
183
		// #ifndef APP-NVUE
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
184 185 186 187
		onPullDownRefresh() {
			this.refresh()
		},
		onReachBottom() {
188
			this.loadMore()
DCloud_JSON's avatar
DCloud_JSON 已提交
189
		}
190
		// #endif
DCloud_JSON's avatar
DCloud_JSON 已提交
191
	}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
192
</script>
DCloud_JSON's avatar
DCloud_JSON 已提交
193

194 195 196 197 198 199 200
<style scoped>
	/* #ifndef APP-NVUE */
	view {
		display: flex;
		box-sizing: border-box;
		flex-direction: column;
	}
201
	/* #endif */
202
	.pages {
203
		background-color: #FFFFFF;
DCloud_JSON's avatar
DCloud_JSON 已提交
204
	}
205

DCloud_JSON's avatar
DCloud_JSON 已提交
206 207 208 209 210 211
	.avatar {
		width: 200rpx;
		height: 200rpx;
		margin-right: 10rpx;
	}

DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
212 213 214 215 216 217
	.main {
		justify-content: space-between;
	}

	.title {
		width: 480rpx;
DCloud_JSON's avatar
DCloud_JSON 已提交
218
		font-size: 32rpx;
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
	}

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

	.author,
	.last_modify_date {
		font-size: 28rpx;
		color: #999999;
	}

	.uni-search-box {
		background-color: #FFFFFF;
		position: sticky;
235
		height: 50px;
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
236
		top: 0;
DCloud_JSON's avatar
DCloud_JSON 已提交
237 238 239
		left: 0;
		/* #ifndef APP-PLUS */
		z-index: 9;
240 241 242
		/* #endif */
		/* #ifdef MP-WEIXIN */
		width: 580rpx;
DCloud_JSON's avatar
DCloud_JSON 已提交
243
		/* #endif */
244
	}
245 246 247 248 249 250 251 252 253 254
	.cover-search-bar {
		height: 50px;
		position: relative;
		top: -50px;
		margin-bottom: -50px;
		/* #ifndef APP-NVUE */
		z-index: 999;
		/* #endif */
	}
</style>
DCloud_JSON's avatar
DCloud_JSON 已提交
255