list.nvue 5.8 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1
<template>
2
	<view class="pages">
3
		<!-- #ifndef H5 -->
4
		<statusBar></statusBar>
5
		<!-- #endif -->
6
		<!-- 搜索功能 -->
DCloud_JSON's avatar
DCloud_JSON 已提交
7
		<uni-search-bar @click="searchClick" class="uni-search-box" v-model="keyword" ref="searchBar" radius="100"
8
				cancelButton="none" disabled :placeholder="inputPlaceholder" />
DCloud_JSON's avatar
DCloud_JSON 已提交
9 10
		<view class="cover-search-bar" @click="searchClick"></view>
		
11 12
		<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"
study夏羽's avatar
study夏羽 已提交
13 14
			field="avatar,title,last_modify_date,user_id.username" @load="loadData"
			>
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
	export default {
		components: {
			statusBar
57 58 59 60 61 62 63 64 65
		},
		computed:{
			inputPlaceholder(e){
				if(uni.getStorageSync('CURRENT_LANG') == "en"){
					return 'Please enter the search content'
				}else{
					return '请输入搜索内容'
				}
			}
66
		},
DCloud_JSON's avatar
DCloud_JSON 已提交
67 68
		data() {
			return {
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
69 70 71
				where: "",
				keyword: "",
				refreshState: 0,
study夏羽's avatar
study夏羽 已提交
72 73
				listHight: 0,
				dataList:[]
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
74
			}
75
		},
76
		watch: {
77 78
			keyword(keyword, oldValue) {
				let where = '"article_status" == 1 '
79
				if (keyword) {
80
					this.where = where + `/${keyword}/.test(title)`;
81
				} else {
82
					this.where = where;
83 84 85
				}
			}
		},
86
		async onReady() {
87 88 89 90 91
			// #ifdef APP-NVUE
			this.listHight = uni.getSystemInfoSync().windowHeight - 96 + 'px'
			// #endif
			// #ifndef APP-NVUE
			this.listHight = 'auto'
92
			// #endif
study夏羽's avatar
study夏羽 已提交
93 94
			cdbRef = this.$refs.udb
			
DCloud_JSON's avatar
DCloud_JSON 已提交
95
		},
study夏羽's avatar
study夏羽 已提交
96
		async onShow() {
DCloud_JSON's avatar
DCloud_JSON 已提交
97 98
			this.keyword = getApp().globalData.searchText
			getApp().globalData.searchText = ''
99 100 101 102 103
			//这里仅演示如何,在onShow生命周期获取设备位置,并在设备或者应用没有权限时自动引导。设置完毕自动重新获取。
			//你可以基于他做自己的业务,比如:根据距离由近到远排序列表数据等
			// uni.showLoading({
			// 	title:"获取定位中"
			// });
104
			//默认h5端不获取定位
study夏羽's avatar
study夏羽 已提交
105 106
			// let location = await gps.getLocation({geocode:true})
			// console.log(location);
107 108 109 110 111 112 113
			// if(location){
			// 	uni.showToast({
			// 		title: JSON.stringify(location),
			// 		icon: 'none'
			// 	});
			// }
			// uni.hideLoading()
DCloud_JSON's avatar
DCloud_JSON 已提交
114 115
		},
		methods: {
study夏羽's avatar
study夏羽 已提交
116 117 118 119 120
			loadData(e){
				console.log("e: ----",e[0]);
				this.dataList = e[0]
				console.log("this.dataList: ",this.dataList);
			},
121
			searchClick(e) { //点击搜索框
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
122 123 124
				uni.hideKeyboard();
				uni.navigateTo({
					url: '/pages/list/search/search',
125
					animationType: 'fade-in'
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
126
				});
127 128 129 130
			},
			retry() {
				this.refresh()
			},
131 132 133
			refresh() {
				console.log('----platform----');
				console.log(JSON.stringify(process.env.VUE_APP_PLATFORM));
134 135 136 137 138 139
				cdbRef.loadData({
					clear: true
				}, () => {
					uni.stopPullDownRefresh()
				})
				console.log('refresh');
DCloud_JSON's avatar
DCloud_JSON 已提交
140 141 142
			},
			onqueryerror(e){
				console.log(e);
143
			}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
144 145 146 147 148
		},
		onPullDownRefresh() {
			this.refresh()
		},
		onReachBottom() {
149 150 151
			cdbRef.loadMore({
				clear: true
			})
DCloud_JSON's avatar
DCloud_JSON 已提交
152 153
		}
	}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
154
</script>
DCloud_JSON's avatar
DCloud_JSON 已提交
155

156 157 158 159 160 161 162 163
<style scoped>
	/* #ifndef APP-NVUE */
	view {
		display: flex;
		box-sizing: border-box;
		flex-direction: column;
	}
	/* #endif */
164
	.pages {
165
		background-color: #FFFFFF;
DCloud_JSON's avatar
DCloud_JSON 已提交
166
	}
167

DCloud_JSON's avatar
DCloud_JSON 已提交
168 169 170 171 172 173
	.avatar {
		width: 200rpx;
		height: 200rpx;
		margin-right: 10rpx;
	}

DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
174 175 176 177 178 179
	.main {
		justify-content: space-between;
	}

	.title {
		width: 480rpx;
DCloud_JSON's avatar
DCloud_JSON 已提交
180
		font-size: 32rpx;
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194
	}

	.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 已提交
195 196 197 198 199 200 201 202
		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 已提交
203
		transition: height 0.3s;
DCloud_JSON's avatar
DCloud_JSON 已提交
204
	}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
205 206 207 208 209

	.uni-search-box {
		background-color: #FFFFFF;
		position: sticky;
		top: 0;
DCloud_JSON's avatar
DCloud_JSON 已提交
210 211 212
		left: 0;
		/* #ifndef APP-PLUS */
		z-index: 9;
213 214 215
		/* #endif */
		/* #ifdef MP-WEIXIN */
		width: 580rpx;
DCloud_JSON's avatar
DCloud_JSON 已提交
216
		/* #endif */
217 218
	}

DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
219
	.show-refresh-tip {
DCloud_JSON's avatar
DCloud_JSON 已提交
220 221 222
		transform: translateY(0);
		height: 40px;
		opacity: 1;
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
	}

	.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;
	}
242 243 244 245 246

	.uni-list {}

	.f1 {
		flex: 1;
DCloud_JSON's avatar
DCloud_JSON 已提交
247 248 249 250 251 252 253 254 255
	}
	.cover-search-bar{
		height: 50px;
		position: relative;
		top: -50px;
		margin-bottom: -50px;
		/* #ifndef APP-NVUE */
		z-index:999;
		/* #endif */
256
	}
257
</style>