detail.vue 8.3 KB
Newer Older
L
linju 已提交
1 2 3 4 5 6 7 8
<template>
	<!--
	 本页面模板教程:https://ext.dcloud.net.cn/plugin?id=2717
	 uni-list 文档:https://ext.dcloud.net.cn/plugin?id=24
	 uniCloud 文档:https://uniapp.dcloud.io/uniCloud/README
	 unicloud-db 组件文档:https://uniapp.dcloud.net.cn/uniCloud/unicloud-db-component
	 DB Schema 规范:https://uniapp.dcloud.net.cn/uniCloud/schema
	 -->
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
9 10 11 12
	<view class="article">
		<!-- #ifdef APP-PLUS -->
		<uni-nav-bar :statusBar="true" :border="false"></uni-nav-bar>
		<!-- #endif -->
芊里 已提交
13
		<view class="article-title">{{ title }}</view>
L
linju 已提交
14 15
		<unicloud-db v-slot:default="{data, loading, error, options}" :options="formData" :collection="collection"
			:field="field" :getone="true" :where="where" :manual="true" ref="detail"
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
16
			foreignKey="opendb-news-articles.user_id" @load="loadData">
L
linju 已提交
17 18 19 20 21
			<template v-if="!loading && data">
				<uni-list :border="false">
					<uni-list-item thumbSize="lg" :thumb="data.image">
						<!-- 通过body插槽定义作者信息内容 -->
						<view slot="body" class="header-content">
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
22
							<view class="uni-title">{{data.user_id && data.user_id[0].username}}</view>
L
linju 已提交
23
						</view>
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
24 25 26 27
						<view slot="footer" class="footer">
							<view class="uni-note">更新于
								<uni-dateformat :date="data.last_modify_date" format="yyyy-MM-dd hh:mm"
									:threshold="[60000, 2592000000]" />
芊里 已提交
28 29
							</view>
							<!-- <button @click="followClick" class="footer-button">关注</button> -->
L
linju 已提交
30 31 32 33 34 35 36 37 38 39 40 41
						</view>
					</uni-list-item>
				</uni-list>
				<view class="banner">
					<!-- 文章开头,缩略图 -->
					<image class="banner-img" :src="data.avatar" mode="widthFix"></image>
					<!-- 文章摘要 -->
					<view class="banner-title">
						<text class="uni-ellipsis">{{data.excerpt}}</text>
					</view>
				</view>
				<view class="article-content">
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
42
					<rich-text :nodes="data.content"></rich-text>
L
linju 已提交
43 44 45 46 47 48 49 50
				</view>
			</template>
		</unicloud-db>
	</view>
</template>

<script>
	import uniShare from 'uni_modules/uni-share/js_sdk/uni-share.js';
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
51 52 53 54 55 56

	const db = uniCloud.database();
	const newsFavoriteTable = db.collection('opendb-news-favorite')
	import {
		mapGetters
	} from 'vuex';
L
linju 已提交
57 58
	export default {
		data() {
DCloud_JSON's avatar
DCloud_JSON 已提交
59
			return {
L
linju 已提交
60 61
				// 当前显示 _id
				id: "",
芊里 已提交
62
				title: 'title',
L
linju 已提交
63 64 65
				// 数据表名
				collection: 'opendb-news-articles,uni-id-users',
				// 查询字段,多个字段用 , 分割
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
66
				field: 'user_id{username, _id},_id,avatar,excerpt,last_modify_date, comment_count, like_count,title,content',
L
linju 已提交
67 68 69 70 71 72 73 74 75 76
				formData: {
					noData: '<p style="text-align:center;color:#666">详情加载中...</p>'
				},
			}
		},
		computed: {
			//拼接where条件
			//查询条件 ,更多详见 :https://uniapp.dcloud.net.cn/uniCloud/unicloud-db?id=jsquery
			where() {
				return `_id =="${this.id}"`
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
77 78 79 80
			},
			...mapGetters({
				'userInfo': 'user/info',
				'hasLogin': 'user/hasLogin'
DCloud_JSON's avatar
DCloud_JSON 已提交
81
			}),
82
			baseappConfig() {
DCloud_JSON's avatar
DCloud_JSON 已提交
83 84
				return getApp().globalData.config
			}
L
linju 已提交
85
		},
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
86
		onLoad(event) {
L
linju 已提交
87 88 89 90 91 92 93 94 95 96
			//获取真实新闻id,通常 id 来自上一个页面
			if (event.id) {
				this.id = event.id
			}
			//若上一页传递了标题过来,则设置导航栏标题
			if (event.title) {
				this.title = event.title
				uni.setNavigationBarTitle({
					title: event.title
				})
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
97
			}
L
linju 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
		},
		onNavigationBarButtonTap(event) {
			if (event.type == 'share') {
				this.shareClick();
			}
		},
		onReady() {
			// 开始加载数据,修改 where 条件后才开始去加载 clinetDB 的数据 ,需要等组件渲染完毕后才开始执行 loadData,所以不能再 onLoad 中执行
			if (this.id) { // ID 不为空,则发起查询
				this.$refs.detail.loadData()
			} else {
				uni.showToast({
					icon: 'none',
					title: '出错了,新闻ID为空'
				})
			}
		},
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
		methods: {
			setFavorite() {
				if (!this.has) return
				newsFavoriteTable.where({
						article_id: this.id,
						user_id: this.userInfo._id
					})
					.get()
					.then(res => {
						let value = {
							article_id: this.id,
							article_title: this.title,
							user_id: this.userInfo._id,
							update_date: Date.now()
						}
						if (res.result.data.length == 0) {
							return newsFavoriteTable.add(value)
						} else {
							return newsFavoriteTable.where({
									article_id: this.id,
									user_id: this.userInfo._id
								})
								.update(value)
						}
					})
					.then(res => {
						// console.log(res);
					})
					.catch(err => {
						console.log(err);
					})
芊里 已提交
146
			},
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
147
			loadData(data) {
L
linju 已提交
148 149 150 151 152
				//如果上一页未传递标题过来(如搜索直达详情),则从新闻详情中读取标题
				if (this.title == '' && data[0].title) {
					this.title = data[0].title
					uni.setNavigationBarTitle({
						title: data[0].title
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
153 154 155
					});

				}
芊里 已提交
156
				this.setFavorite();
L
linju 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
			},
			/**
			 * followClick
			 * 点击关注
			 */
			followClick() {
				uni.showToast({
					title: '点击关注',
					icon: 'none'
				});
			},
			/**
			 * 分享该文章
			 */
			shareClick() {
				let {
					_id,
					title,
					excerpt,
					avatar
				} = this.$refs.detail.dataList
				uniShare({
					content: { //公共的分享类型(type)、链接(herf)、标题(title)、summary(描述)、imageUrl(缩略图)
						type: 0,
						href: baseappConfig.h5.url + `/#/pages/list/detail?id=${_id}&title=${title}`,
						title: this.title,
						summary: excerpt,
						imageUrl: avatar
					},
					menus: [{
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
187
							"img": "/static/app-plus/sharemenu/wechatfriend.png",
L
linju 已提交
188 189 190 191 192 193 194
							"text": "微信好友",
							"share": {
								"provider": "weixin",
								"scene": "WXSceneSession"
							}
						},
						{
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
195
							"img": "/static/app-plus/sharemenu/wechatmoments.png",
L
linju 已提交
196 197 198 199 200 201 202
							"text": "微信朋友圈",
							"share": {
								"provider": "weixin",
								"scene": "WXSenceTimeline"
							}
						},
						{
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
203
							"img": "/static/app-plus/sharemenu/mp_weixin.png",
L
linju 已提交
204 205 206 207 208 209
							"text": "微信小程序",
							"share": {
								provider: "weixin",
								scene: "WXSceneSession",
								type: 5,
								miniProgram: {
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
210 211 212 213 214
									id: baseappConfig.mp.weixin.id,
									path: `/pages/list/detail?id=${_id}&title=${title}`,
									webUrl: baseappConfig.h5.url +
										`/#/pages/list/detail?id=${_id}&title=${title}`,
									type: 0
L
linju 已提交
215 216 217 218
								},
							}
						},
						{
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
219
							"img": "/static/app-plus/sharemenu/weibo.png",
L
linju 已提交
220 221 222 223 224 225
							"text": "微博",
							"share": {
								"provider": "sinaweibo"
							}
						},
						{
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
226
							"img": "/static/app-plus/sharemenu/qq.png",
L
linju 已提交
227 228 229 230 231 232
							"text": "QQ",
							"share": {
								"provider": "qq"
							}
						},
						{
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
233
							"img": "/static/app-plus/sharemenu/copyurl.png",
L
linju 已提交
234 235 236 237
							"text": "复制",
							"share": "copyurl"
						},
						{
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
238
							"img": "/static/app-plus/sharemenu/more.png",
L
linju 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251
							"text": "更多",
							"share": "shareSystem"
						}
					],
					cancelText: "取消分享",
				}, e => { //callback
					console.log(e);
				})
			},
		}
	}
</script>

DCloud_JSON's avatar
DCloud_JSON 已提交
252
<style scoped>
L
linju 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
	.header-content {
		flex: 1;
		display: flex;
		flex-direction: column;
		font-size: 14px;
	}

	/* 标题 */
	.uni-title {
		display: flex;
		margin-bottom: 5px;
		font-size: 14px;
		font-weight: bold;
		color: #3b4144;
	}

	/* 描述 额外文本 */
	.uni-note {
		color: #999;
		font-size: 12px;

		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
	}

	.footer {
		display: flex;
		align-items: center;
	}

	.footer-button {
		display: flex;
		align-items: center;
		font-size: 12px;
		height: 30px;
		color: #fff;
		background-color: #ff5a5f;
	}

	.banner {
		position: relative;
		margin: 0 15px;
		height: 180px;
		overflow: hidden;
	}

	.banner-img {
		position: absolute;
		width: 100%;
	}

	.banner-title {
		display: flex;
		align-items: center;
		position: absolute;
		padding: 0 15px;
		width: 100%;
		bottom: 0;
		height: 30px;
		font-size: 14px;
		color: #fff;
		background: rgba(0, 0, 0, 0.4);
		overflow: hidden;
		box-sizing: border-box;
	}

	.uni-ellipsis {
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}

	.article-title {
		padding: 20px 15px;
		padding-bottom: 0;
	}

	.article-content {
		padding: 15px;
		font-size: 15px;
		overflow: hidden;
	}
338
</style>