list.nvue 4.8 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1
<template>
2
	<view class="pages">
3 4 5
		<!-- #ifndef H5 -->
		<statusBar></statusBar>
		<!-- #endif -->
6
		<!-- 搜索功能 -->
DCloud_JSON's avatar
DCloud_JSON 已提交
7 8 9 10 11
		<view @click.capture="searchClick">
			<uni-search-bar class="uni-search-box" v-model="keyword" ref="searchBar" radius="100"
				cancelButton="none" />
		</view>
		
12 13
		<unicloud-db ref='udb' v-slot:default="{data,pagination,hasMore, loading, error, options}" @error="onqueryerror"
			:where="where" collection="opendb-news-articles,uni-id-users" :page-size="10"
14
			field="avatar,title,last_modify_date,user_id.username">
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
15
			<!-- 基于 uni-list 的页面布局 -->
16 17
			<uni-list class="uni-list" :border="false" :bounce="true" :alwaysScrollableVertical="true"
				:style="{height:listHight}">
18
				<uni-list-item :to="'/pages/list/detail?id='+item._id+'&title='+item.title" v-for="(item,index) in data"
19
					:key="index">
20 21 22 23
					<!-- 通过header插槽定义列表左侧图片 -->
					<template v-slot:header>
						<image class="avatar" :src="item.avatar" mode="aspectFill"></image>
					</template>
DCloud_JSON's avatar
DCloud_JSON 已提交
24 25 26 27 28 29 30 31 32 33 34
					<!-- 通过body插槽定义布局 -->
					<template v-slot:body>
						<view class="main">
							<text class="title">{{item.title}}</text>
							<view class="info">
								<text class="author">{{item.user_id[0].username}}</text>
								<uni-dateformat class="last_modify_date" :date="item.last_modify_date" format="yyyy-MM-dd"
									:threshold="[60000, 2592000000]" />
							</view>
						</view>
					</template>
35
				</uni-list-item>
36
				<uni-list-item>
DCloud_JSON's avatar
DCloud_JSON 已提交
37
					<template v-slot:body>
38 39 40
						<uni-load-state @networkResume="refresh" :state="{data,pagination,hasMore, loading, error}">
						</uni-load-state>
					</template>
41
				</uni-list-item>
42
			</uni-list>
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
43
		</unicloud-db>
DCloud_JSON's avatar
DCloud_JSON 已提交
44 45 46 47
	</view>
</template>

<script>
48
	var cdbRef, currentWebview;
49 50 51 52 53
	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';
	const gps = new Gps()
	
54 55 56 57
	export default {
		components: {
			statusBar
		},
DCloud_JSON's avatar
DCloud_JSON 已提交
58 59
		data() {
			return {
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
60 61 62
				where: "",
				keyword: "",
				refreshState: 0,
DCloud_JSON's avatar
DCloud_JSON 已提交
63
				listHight: 0
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
64
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
65
		},
66 67 68 69 70 71 72 73 74
		watch: {
			keyword(keyword, oldValue) {
				if (keyword) {
					this.where = `/${keyword}/.test(title)`;
				} else {
					this.where = '';
				}
			}
		},
75
		async onReady() {
76 77 78 79 80
			// #ifdef APP-NVUE
			this.listHight = uni.getSystemInfoSync().windowHeight - 96 + 'px'
			// #endif
			// #ifndef APP-NVUE
			this.listHight = 'auto'
81
			// #endif
82
			cdbRef = this.$refs.udb
DCloud_JSON's avatar
DCloud_JSON 已提交
83
		},
84
		async onShow() {
DCloud_JSON's avatar
DCloud_JSON 已提交
85 86
			this.keyword = getApp().globalData.searchText
			getApp().globalData.searchText = ''
87 88 89 90 91 92 93 94 95 96 97 98 99 100
			//这里仅演示如何,在onShow生命周期获取设备位置,并在设备或者应用没有权限时自动引导。设置完毕自动重新获取。
			//你可以基于他做自己的业务,比如:根据距离由近到远排序列表数据等
			// uni.showLoading({
			// 	title:"获取定位中"
			// });
			let location = await gps.getLocation({geocode:true})
			console.log(location);
			// if(location){
			// 	uni.showToast({
			// 		title: JSON.stringify(location),
			// 		icon: 'none'
			// 	});
			// }
			// uni.hideLoading()
DCloud_JSON's avatar
DCloud_JSON 已提交
101 102
		},
		methods: {
103
			searchClick(e) { //点击搜索框
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
104 105 106
				uni.hideKeyboard();
				uni.navigateTo({
					url: '/pages/list/search/search',
107
					animationType: 'fade-in'
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
108
				});
109 110 111 112 113 114 115 116 117 118 119
			},
			retry() {
				this.refresh()
			},
			refresh() {
				cdbRef.loadData({
					clear: true
				}, () => {
					uni.stopPullDownRefresh()
				})
				console.log('refresh');
DCloud_JSON's avatar
DCloud_JSON 已提交
120 121 122
			},
			onqueryerror(e){
				console.log(e);
DCloud_JSON's avatar
DCloud_JSON 已提交
123
			}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
124 125 126 127 128
		},
		onPullDownRefresh() {
			this.refresh()
		},
		onReachBottom() {
129 130 131
			cdbRef.loadMore({
				clear: true
			})
DCloud_JSON's avatar
DCloud_JSON 已提交
132 133
		}
	}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
134
</script>
DCloud_JSON's avatar
DCloud_JSON 已提交
135

136
<style scoped>
137
	.pages {
138
		background-color: #FFFFFF;
DCloud_JSON's avatar
DCloud_JSON 已提交
139
	}
140

DCloud_JSON's avatar
DCloud_JSON 已提交
141 142 143 144 145 146
	.avatar {
		width: 200rpx;
		height: 200rpx;
		margin-right: 10rpx;
	}

DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
147 148 149 150 151 152
	.main {
		justify-content: space-between;
	}

	.title {
		width: 480rpx;
DCloud_JSON's avatar
DCloud_JSON 已提交
153
		font-size: 32rpx;
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167
	}

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

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

	.refresh-tip {
DCloud_JSON's avatar
DCloud_JSON 已提交
168 169 170 171 172 173 174 175
		color: #67c23a;
		font-size: 14px;
		line-height: 40px;
		text-align: center;
		background-color: #f0f9eb;
		height: 0;
		opacity: 0;
		transform: translateY(-100%);
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
176
		transition: height 0.3s;
DCloud_JSON's avatar
DCloud_JSON 已提交
177
	}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
178 179 180 181 182

	.uni-search-box {
		background-color: #FFFFFF;
		position: sticky;
		top: 0;
DCloud_JSON's avatar
DCloud_JSON 已提交
183 184 185 186
		left: 0;
		/* #ifndef APP-PLUS */
		z-index: 9;
		/* #endif */
187 188
	}

DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
189
	.show-refresh-tip {
DCloud_JSON's avatar
DCloud_JSON 已提交
190 191 192
		transform: translateY(0);
		height: 40px;
		opacity: 1;
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
	}

	.get-data-state {
		width: 750rpx;
		align-items: center;
	}

	.get-data-state-img {
		width: 500rpx;
	}

	.get-data-state-text {
		width: 32rpx;
		color: #999999;
		line-height: 50rpx;
		height: 50rpx;
		width: 750rpx;
		text-align: center;
	}
212 213 214 215 216

	.uni-list {}

	.f1 {
		flex: 1;
217
	}
218
</style>