detail.vue 9.6 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
	<view class="article">
10
		<!-- #ifdef APP-PLUS -->
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
11 12
		<uni-nav-bar :statusBar="true" :border="false"></uni-nav-bar>
		<!-- #endif -->
芊里 已提交
13
		<view class="article-title">{{ title }}</view>
14 15
		<unicloud-db v-slot:default="{data, loading, error, options}" :options="formData" :collection="colList"
			:getone="true" :manual="true" ref="detail"
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
16
			foreignKey="opendb-news-articles.user_id" @load="loadData">
L
linju 已提交
17 18 19
			<template v-if="!loading && data">
				<uni-list :border="false">
					<uni-list-item thumbSize="lg" :thumb="data.image">
20 21
						<!-- 通过body插槽定义作者信息内容 -->
						<template v-slot:body>
study夏羽's avatar
study夏羽 已提交
22 23
							<view class="header-content">
								<view class="uni-title">{{data.user_id && data.user_id[0] && data.user_id[0].nickname || '未知'}}</view>
24 25 26 27 28 29 30 31 32 33
							</view>
						</template>
						<template v-slot:footer>
							<view class="footer">
								<view class="uni-note">更新于
									<uni-dateformat :date="data.last_modify_date" format="yyyy-MM-dd hh:mm"
										:threshold="[60000, 2592000000]" />
								</view>
							</view>
						</template>
L
linju 已提交
34
					</uni-list-item>
35
				</uni-list>
L
linju 已提交
36 37 38 39 40 41 42 43
				<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>
44
				<view class="article-content">
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
45
					<rich-text :nodes="data.content"></rich-text>
L
linju 已提交
46 47 48 49 50 51 52
				</view>
			</template>
		</unicloud-db>
	</view>
</template>

<script>
53
	// #ifdef APP-PLUS
study夏羽's avatar
study夏羽 已提交
54 55
	import UniShare from '@/uni_modules/uni-share/js_sdk/uni-share.js';
	import uniNavBar from '@/uni_modules/uni-nav-bar/components/uni-nav-bar/uni-nav-bar.vue';
56 57
	const uniShare = new UniShare()
	// #endif
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
58
	const db = uniCloud.database();
59
	const readNewsLog = db.collection('read-news-log')
60
	export default {
61
		// #ifdef APP-PLUS
study夏羽's avatar
study夏羽 已提交
62 63 64
		components:{
			"uni-nav-bar":uniNavBar
		},
65
		onBackPress({from}) {
66 67 68 69 70 71 72
			if(from == 'backbutton'){
				if(uniShare.isShow){
					this.$nextTick(function(){
						console.log(uniShare);
						uniShare.hide()
					})
				}
73 74
				return uniShare.isShow;
			}
75 76
		},
		// #endif
L
linju 已提交
77
		data() {
DCloud_JSON's avatar
DCloud_JSON 已提交
78
			return {
L
linju 已提交
79 80
				// 当前显示 _id
				id: "",
芊里 已提交
81
				title: 'title',
L
linju 已提交
82 83
				// 数据表名
				// 查询字段,多个字段用 , 分割
84
				// field: 'user_id.nickname,user_id._id,avatar,excerpt,last_modify_date,comment_count,like_count,title,content',
L
linju 已提交
85 86
				formData: {
					noData: '<p style="text-align:center;color:#666">详情加载中...</p>'
87
				}
L
linju 已提交
88 89 90
			}
		},
		computed: {
91
			uniStarterConfig() {
DCloud_JSON's avatar
DCloud_JSON 已提交
92
				return getApp().globalData.config
93 94 95 96
			},
			where(){
				//拼接where条件 查询条件 ,更多详见 :https://uniapp.dcloud.net.cn/uniCloud/unicloud-db?id=jsquery
				return `_id =="${this.id}"`
97 98 99 100 101 102 103
			},
      colList(){
      	return [
      		db.collection('opendb-news-articles').where(this.where).field('user_id,_id,avatar,excerpt,last_modify_date,comment_count,like_count,title,content').getTemp(),
          db.collection('uni-id-users').field('_id,nickname').getTemp()
      	]
      }
L
linju 已提交
104
		},
105
		onLoad(event) {
L
linju 已提交
106
			//获取真实新闻id,通常 id 来自上一个页面
107 108
			if (event.id) {
				this.id = event.id
L
linju 已提交
109 110 111 112 113 114 115
			}
			//若上一页传递了标题过来,则设置导航栏标题
			if (event.title) {
				this.title = event.title
				uni.setNavigationBarTitle({
					title: event.title
				})
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
116
			}
L
linju 已提交
117 118 119
		},
		onReady() {
			// 开始加载数据,修改 where 条件后才开始去加载 clinetDB 的数据 ,需要等组件渲染完毕后才开始执行 loadData,所以不能再 onLoad 中执行
120 121
			if (this.id) { // ID 不为空,则发起查询
				this.$refs.detail.loadData()
L
linju 已提交
122 123 124
			} else {
				uni.showToast({
					icon: 'none',
study夏羽's avatar
update  
study夏羽 已提交
125
					title: this.$t('listDetail.newsErr')
L
linju 已提交
126
				})
127
			}
128 129 130 131 132
		},
		onNavigationBarButtonTap(event) {
			if (event.type == 'share') {
				this.shareClick();
			}
L
linju 已提交
133
		},
134 135 136 137
		methods: {
			$log(...args){
				console.log('args',...args,this.id)
			},
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
			setReadNewsLog(){
				let item = {
					"article_id":this.id,
					"last_time":Date.now()
				},
				readNewsLog = uni.getStorageSync('readNewsLog')||[],
				index = -1;
				readNewsLog.forEach(({article_id},i)=>{
					if(article_id == item.article_id){
						index = i
					}
				})
				if(index === -1){
					readNewsLog.push(item)
				}else{
					readNewsLog.splice(index,1,item)
				}
				uni.setStorageSync('readNewsLog',readNewsLog)
				console.log(readNewsLog);
			},
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
158
			setFavorite() {
159
				if ( uniCloud.getCurrentUserInfo().tokenExpired < Date.now() ){
DCloud_JSON's avatar
DCloud_JSON 已提交
160
					return console.log('未登录用户');
161 162 163
				}
				let article_id = this.id,
					last_time = Date.now();
164
					console.log({article_id,last_time});
165 166 167 168 169 170 171
					readNewsLog.where(`"article_id" == "${article_id}" && "user_id"==$env.uid`)
						.update({last_time})
						.then(({result:{updated}}) => {
							console.log('updated',updated);
							if (!updated) {
								readNewsLog.add({article_id}).then(e=>{
									console.log(e);
172 173
								}).catch(err => {
									console.log(err);
174 175
								})
							}
176 177
						}).catch(err => {
							console.log(err);
178
						})
芊里 已提交
179
			},
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
180
			loadData(data) {
L
linju 已提交
181 182 183 184 185
				//如果上一页未传递标题过来(如搜索直达详情),则从新闻详情中读取标题
				if (this.title == '' && data[0].title) {
					this.title = data[0].title
					uni.setNavigationBarTitle({
						title: data[0].title
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
186 187 188
					});

				}
189
				this.setReadNewsLog();
L
linju 已提交
190 191 192 193 194 195 196
			},
			/**
			 * followClick
			 * 点击关注
			 */
			followClick() {
				uni.showToast({
study夏羽's avatar
update  
study夏羽 已提交
197
					title:this.$t('listDetail.follow'),
L
linju 已提交
198 199 200 201 202 203
					icon: 'none'
				});
			},
			/**
			 * 分享该文章
			 */
204
			// #ifdef APP-PLUS
205 206 207 208 209 210
			shareClick() {
				let {
					_id,
					title,
					excerpt,
					avatar
211
				} = this.$refs.detail.dataList
212 213 214 215 216
				console.log( JSON.stringify({
					_id,
					title,
					excerpt,
					avatar
217
				}) );
218 219 220 221 222 223 224 225 226
				uniShare.show({
					content: { //公共的分享类型(type)、链接(herf)、标题(title)、summary(描述)、imageUrl(缩略图)
						type: 0,
						href: this.uniStarterConfig.h5.url + `/#/pages/list/detail?id=${_id}&title=${title}`,
						title: this.title,
						summary: excerpt,
						imageUrl: avatar + '?x-oss-process=image/resize,m_fill,h_100,w_100' //压缩图片解决,在ios端分享图过大导致的图片失效问题
					},
					menus: [{
Anne_LXM's avatar
Anne_LXM 已提交
227
							"img": "/static/app/sharemenu/wechatfriend.png",
study夏羽's avatar
update  
study夏羽 已提交
228
							"text": this.$t('common.wechatFriends'),
229 230 231 232 233 234
							"share": {
								"provider": "weixin",
								"scene": "WXSceneSession"
							}
						},
						{
Anne_LXM's avatar
Anne_LXM 已提交
235
							"img": "/static/app/sharemenu/wechatmoments.png",
study夏羽's avatar
update  
study夏羽 已提交
236
							"text": this.$t('common.wechatBbs'),
237 238
							"share": {
								"provider": "weixin",
239
								"scene": "WXSceneTimeline"
240 241 242
							}
						},
						{
Anne_LXM's avatar
Anne_LXM 已提交
243
							"img": "/static/app/sharemenu/mp_weixin.png",
study夏羽's avatar
update  
study夏羽 已提交
244
							"text": this.$t('common.wechatApplet'),
245 246 247 248 249 250 251 252 253 254 255 256 257 258
							"share": {
								provider: "weixin",
								scene: "WXSceneSession",
								type: 5,
								miniProgram: {
									id: this.uniStarterConfig.mp.weixin.id,
									path: `/pages/list/detail?id=${_id}&title=${title}`,
									webUrl: this.uniStarterConfig.h5.url +
										`/#/pages/list/detail?id=${_id}&title=${title}`,
									type: 0
								},
							}
						},
						{
Anne_LXM's avatar
Anne_LXM 已提交
259
							"img": "/static/app/sharemenu/weibo.png",
study夏羽's avatar
update  
study夏羽 已提交
260
							"text": this.$t('common.weibo'),
261 262 263 264 265
							"share": {
								"provider": "sinaweibo"
							}
						},
						{
Anne_LXM's avatar
Anne_LXM 已提交
266
							"img": "/static/app/sharemenu/qq.png",
267 268 269 270 271 272
							"text": "QQ",
							"share": {
								"provider": "qq"
							}
						},
						{
Anne_LXM's avatar
Anne_LXM 已提交
273
							"img": "/static/app/sharemenu/copyurl.png",
study夏羽's avatar
update  
study夏羽 已提交
274
							"text": this.$t('common.copy'),
275 276 277
							"share": "copyurl"
						},
						{
Anne_LXM's avatar
Anne_LXM 已提交
278
							"img": "/static/app/sharemenu/more.png",
study夏羽's avatar
update  
study夏羽 已提交
279
							"text": this.$t('common.more'),
280 281 282
							"share": "shareSystem"
						}
					],
study夏羽's avatar
update  
study夏羽 已提交
283
					cancelText: this.$t('common.cancelShare'),
284
				}, e => { //callback
285
					console.log(e);
286 287 288
				})
			}
			// #endif
L
linju 已提交
289 290 291 292
		}
	}
</script>

DCloud_JSON's avatar
DCloud_JSON 已提交
293
<style scoped>
L
linju 已提交
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 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
	.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;
	}
379
</style>