<template>
	<view v-if="detaile">
		<image mode="widthFix" style="width: 100%;margin-bottom: 40rpx;" v-if="detaile.fieldValues&&detaile.fieldValues.pic" :src="detaile.fieldValues.pic"></image>
		<block v-else>
			
		</block>
		<view class="content">
			<view class="viewMore">
				<mp-html :content="detaile.text" :lazy-load="true" @imgtap="choose"></mp-html>
			</view>
		</view>
		
		<serviceContact></serviceContact>
	</view>
</template>

<script>
const app = getApp();
const req = require('../../utils/request.js');
const api = require('../../utils/api.js');
const util = require('../../utils/util.js');
import mpHtml from "../../components/mp-html/components/mp-html/mp-html";
import serviceContact from "../../components/service-contact/index.vue";
export default {
	components: {
		mpHtml,
		serviceContact
	},
	data() {
		return {
			detaile: '',
			type: 2, //详情类型：1 普通内容 2 自定义页面内容
			config: {},
		};
	},

	onLoad: async function(options) {
		uni.showShareMenu({
			withShareTicket: true,
			menus: ['shareAppMessage', 'shareTimeline']
		});
		this.id = options.id;
		await this.getDetail();
		// await this.monitor();
	},
	onShow(){
		this.config =  JSON.parse(req.getStorage('configRes'))
		this.getBrowse();
	},

	onShareAppMessage() {
		return {
			title: this.detaile.title,
			imageUrl: this.detaile.pic,
			path: '/topic/content/content?id=' + this.id +'&isShare=' + true
		};
	},
	onShareTimeline() {
		return {
			title: this.detaile.title,
			imageUrl: this.detaile.pic,
			path: '/topic/content/content?id=' + this.id +'&isShareTimeline=' + true
		};
	},
	methods: {
		monitor() {
			let that = this;
			let system = uni.getSystemInfoSync();
			let query = uni.createSelectorQuery();
			query.select('.viewMore').boundingClientRect(data=>{
				let height = data.height;
				if(height > system.screenHeight) {
					this.viewMore = true
				}
			}).exec();
		},
		
		choose: function () {
		  let freshen = false;
		  this.$emit('freshen', {
		    detail: freshen
		  });
		},
		toIndex(){
			uni.switchTab({
				url: '/pages/index/index'
			})
		},
		getDateTimeStamp(dateStr){
		 return util.getDateDiff(Date.parse(dateStr.replace(/-/gi,"/")));
		},
		getDetail() {
			let that = this;
			let apiUrl = '/api/content/detail';
			return new Promise((resolve,reject)=>{
				req.getRequest(apiUrl,{ id: that.id },async res => {
					if(res.productCategory){
						res.productCategory = res.productCategory.split(',')
					}
					that.detaile = res;
					let arr = [];
					if(res.productCategory && res.productCategory.length > 0){
						for (var i = 0; i < res.productCategory.length; i++) {
							await that.getProductList(res.productCategory[i], i).then(res=>{
								arr = arr.concat(res);
								that.productList = arr;
								console.log('相关产品=='+JSON.stringify(arr),arr.length)
							});
						}
					}
					resolve();
				},true);
			})
		},
		getBrowse(){
			var dataP = {};
			dataP.type =
				4 //、产品 2、活动 3、未知 4、内容 5、课程 6、老师 7、素材 8、题目 9、资料领取 10、招聘职位 12、用户须知 13、素材 15、医院科室 16、海报 20、医生 21 新闻 23名片
			dataP.behavior = 4; //1、关注 2、收藏 3、点赞 4、浏览 5、确认 6、分享
			dataP.bindId = this.id;
			req.postRequestLoding('/api/v3/behavior/save', dataP, data => {
			});
		},
		
	},
	filters: {
		/**
		 * 处理富文本里的图片宽度自适应
		 * 1.去掉img标签里的style、width、height属性
		 * 2.img标签添加style属性：max-width:100%;height:auto
		 * 3.修改所有style里的width属性为max-width:100%
		 * 4.去掉<br/>标签
		 * @param html
		 * @returns {void|string|*}
		 */
		formatRichText(html) {
			//控制小程序中图片大小
			let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
				match = match.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(/<br[^>]*\/>/gi, '<p style="margin: 10px 0;"></p>');
			newContent = newContent.replace(/<br[^>]*\>/gi, '<p style="margin: 10px 0;"></p>');
			newContent = newContent.replace(/font-size:[^;]+;?/g,'');
			newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:inline-block;margin:12rpx auto;"');
			return newContent;
		}
	}
};
</script>
<style>
@import './alContent.css';
</style>
