<template>
	<view class="ndetail ">
		<image :src="nonews" v-if="datainfo.status==2" class="noimg"></image>
		<!-- 详情 -->
		<view class="ndbox mb12" v-else>
			<view class="plr15">
				<view class="tit">{{datainfo.partyTitle}}</view>
				<view class="time mb12">{{datainfo.publishTime}}</view>
				<view class="rich">
					<rich-text :nodes="partyContent" style="word-break: break-all;"></rich-text>
				</view>
			</view>
			<view class="ndbfox flexc">
			<!-- 	<view class="flexcc ndbflist">
					<image :src="nicona"></image>
					<view>32</view>
				</view> -->
				<view class="flexcc ndbflist">
					<image :src="eye" class="imgb"></image>
					<view>{{datainfo.viewCount}}</view>
				</view>
				<view class="flexcc ndbflist" @click="getScFn()">
					<image :src="starb" v-if="datainfo.isStars=='Y'"></image>
					<image :src="niconc" v-else></image>
					<view>{{datainfo.starsCount}}</view>
				</view>
			</view>
		</view>
	<loading></loading>	
	</view>
</template>

<script>
	import config from '@/config'
	const baseUrl = config.baseUrl
	import { checkPermi, checkRole } from "@/utils/permission"; // 权限判断函数
	import {partyNewsDet,getStarsDj} from "@/api/work/manage.js"
	export default{
		components:{},
		data(){
			return{
				nicona:require('@/manage/static/news/nicona.png'),
				niconb:require('@/manage/static/news/niconb.png'),
				niconc:require('@/manage/static/news/niconc.png'),
				nicond:require('@/manage/static/news/nicond.png'),
				nicone:require('@/manage/static/news/nicone.png'),
				head:require('@/manage/static/news/head.png'),
				starb:require('@/manage/static/news/starb.png'),
				eye:require('@/manage/static/news/eye.png'),
				nonews:require('@/manage/static/news/nonews.png'),
				datainfo:{
					
				},
				partyContent:'',
				text:'',
				id:"",
				userId:this.$store.state.user.userId,
				nickName:this.$store.state.user.nickName,
				avatar:this.$store.state.user.avatar,
				tenantId:this.$store.state.user.tenantId
			}
		},
		onLoad: function(e) {
			this.id=e.id;
			this.getDetailFn()
		},
		// 上拉触底加载更多触发事件
		onReachBottom() {
			if (this.reachflag) {
				this.pageNum++
				this.getDataFn()
			}
		},
		methods:{
			checkPermi, checkRole,
			getDetailFn(){
				partyNewsDet(this.id).then(res=>{
					if(res.code==200){
						this.datainfo=res.data;
						var data=JSON.parse(JSON.stringify(res.data))
						if(data.partyContent){
							this.partyContent=this.formatRichText(data.partyContent)
						}
					}
				})
			},
			getScFn(type){
				var params={
					userId:this.userId,
					nickName:this.nickName,
					avatar:this.avatar,
					targetType:'2',//目标类型（1：社区 2：党建 ）
					targetId:this.id,
					targetTitle:this.datainfo.partyTitle,
					tenantId:this.tenantId
				}
				getStarsDj(params).then(res=>{
					if(res.code==200){
						uni.$emit('partyNewsList')
						if(this.datainfo.isStars=='Y'){
							this.$toast("取消收藏成功");
							this.datainfo.isStars='N';
							this.datainfo.starsCount--;
						}else{
							this.$toast("收藏成功");
							this.datainfo.isStars='Y';
							this.datainfo.starsCount++
						}
					}
				})
			},
			//解析富文本方法
			formatRichText(html) {
					let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
						match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '').replace(/style=""/gi, '');
						// match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
						// match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
						return match;
					});
					newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
						match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
						return match;
					});
					newContent = newContent.replace(/<br[^>]*\/>/gi, '');
					newContent = newContent.replace(/\<img src="/gi,
						'<img style="max-width:100%;height:auto;display:block;margin:10rpx auto;" src="' +baseUrl);
					return newContent;
			},
		}
	}
</script>
<style>
	page{background: #F3F3F0;}
</style>
<style lang="scss" scoped>
.ndetail{
	min-height: 100vh;padding: 20rpx 18rpx 100rpx;
	.ndbox{background: #FFFFFF;border-radius: 20rpx;padding-top: 30rpx;
		.tit{font-weight: bold;font-size: 32rpx;color: #272727;margin-bottom: 14rpx;text-align: center;padding-top: 4rpx;}
		.time{font-weight: 500;font-size: 24rpx;color: #AAAAAA;text-align: center;}
		.rich{border-top: 2rpx solid #E5E5E5;padding: 28rpx 0;}	
		.ndbfox{
			border-top: 2rpx solid #E5E5E5;
			.ndbflist{width: 33.3%;min-height: 78rpx;padding: 16rpx 0;box-sizing: border-box;
				image{width: 26rpx;height: 26rpx;margin-right: 26rpx;}
				view{font-weight: 500;font-size: 26rpx;color: #666666;}
				.imgb{width: 24rpx;height: 18rpx;margin-right: 10rpx;}
			}
		}
	}
}
</style>