list.nvue 6.1 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 15
		<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夏羽 已提交
16 17
			field="avatar,title,last_modify_date,user_id.username" @load="loadData"
			>
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
18
			<!-- 基于 uni-list 的页面布局 -->
19 20 21 22 23 24 25
			<uni-list class="uni-list" :border="false" :style="{height:listHight}">
				
				<!-- 作用于app端nvue页面的下拉加载 -->
				<!-- #ifdef APP-NVUE -->
				<refreshBox @refresh="refresh" :loading="loading"></refreshBox>
				<!-- #endif -->
				<!-- 列表渲染 -->
26
				<uni-list-item :to="'/pages/list/detail?id='+item._id+'&title='+item.title" v-for="(item,index) in data"
27
					:key="index">
28 29 30 31
					<!-- 通过header插槽定义列表左侧图片 -->
					<template v-slot:header>
						<image class="avatar" :src="item.avatar" mode="aspectFill"></image>
					</template>
32 33 34 35 36 37 38 39 40 41
					<!-- 通过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>
42
					</template>
43
				</uni-list-item>
44
				<!-- 加载状态:上拉加载更多,加载中,没有更多数据了,加载错误 -->
45
				<!-- #ifdef APP-PLUS -->
46
				<uni-list-item>
DCloud_JSON's avatar
DCloud_JSON 已提交
47
					<template v-slot:body>
48
				<!-- #endif -->
49 50
						<uni-load-state @networkResume="refresh" :state="{data,pagination,hasMore, loading, error}"
							@loadMore="loadMore">
51
						</uni-load-state>
52
				<!-- #ifdef APP-PLUS -->
53
					</template>
54
				</uni-list-item>
55
				<!-- #endif -->
56
			</uni-list>
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
57
		</unicloud-db>
DCloud_JSON's avatar
DCloud_JSON 已提交
58 59 60 61
	</view>
</template>

<script>
62
	var cdbRef, currentWebview;
63 64 65 66 67
	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()

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

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

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

DCloud_JSON's avatar
DCloud_JSON 已提交
202 203 204 205 206 207
	.avatar {
		width: 200rpx;
		height: 200rpx;
		margin-right: 10rpx;
	}

DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
208 209 210 211 212 213
	.main {
		justify-content: space-between;
	}

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

	.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;
231
		height: 50px;
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
232
		top: 0;
DCloud_JSON's avatar
DCloud_JSON 已提交
233 234 235
		left: 0;
		/* #ifndef APP-PLUS */
		z-index: 9;
236 237 238
		/* #endif */
		/* #ifdef MP-WEIXIN */
		width: 580rpx;
DCloud_JSON's avatar
DCloud_JSON 已提交
239
		/* #endif */
240
	}
241 242 243 244 245 246 247 248 249 250
	.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 已提交
251