<template>
	<view class="main">
		<view class="one">
			<view class="one_1">{{info.name||'暂无'}}</view>
			<view class="one_2">{{info.fixed_time||'暂无'}}</view>
		</view>
		<view class="two">
			<view class="two_1">类型：{{info.zhType||'暂无'}}</view>
			<view class="two_2">来源：{{info.source||'暂无'}}</view>
			<view class="two_3">作者：{{info.author||'暂无'}}</view>
		</view>
		<view class="thr" v-if="info.file.length>0">
			<swiperImg :imgsList='info.file'></swiperImg>
		</view>
		<view class="four">
			<rich-text :nodes="formatRichText(info.brief)"></rich-text>
		</view>
	</view>
</template>

<script>
	import swiperImg from '../../components/swiper/index.vue';
	export default {
		components: {
			swiperImg
		},
		data() {
			return {
				id: '',
				user: {},
				info: {
					file: []
				},
				// 字典表
				typeList: []
			}
		},
		onLoad: async function(e) {
			const that = this;
			that.$set(that, `id`, e && e.id || '');
			that.searchToken();
			await that.searchOther();
			that.search();
		},
		methods: {
			searchToken() {
				const that = this;
				try {
					const res = uni.getStorageSync('token');
					if (res) {
						that.$set(that, `user`, res);
					}
				} catch (e) {
					uni.showToast({
						title: err.errmsg,
						icon: 'error',
						duration: 2000
					});
				}
			},
			async search() {
				const that = this;
				if (that.id) {
					const res = await that.$api(`/news/${that.id}`, 'GET', {})
					if (res.errcode == '0') {
						const type = that.typeList.find(i => i.value == res.data.type)
						if (type) res.data.zhType = type.label
						that.$set(that, `info`, res.data)
					} else {
						uni.showToast({
							title: res.errmsg,
						});
					}
				}
			},
			// 处理富文本
			formatRichText(html) {
				if (html) {
					// 富文本内容格式化
					return html && html.replace(/<img[^>]*>/gi, function(match, capture) {
						// 查找所有的 img 元素
						return match.replace(/style=".*"/gi, '').replace(/style='.*'/gi,
							'')
						// 删除找到的所有 img 元素中的 style 属性
					}).replace(/\<img/gi, '<img style="width:100%;"') // 对 img 元素增加 style 属性，并设置宽度为 100%
				}
			},
			// 查询其他信息
			async searchOther() {
				const that = this;
				let res;
				// 查询类型
				res = await that.$api(`/dictData`, 'GET', {
					type: 'news_type',
					is_use: '0',
				})
				if (res.errcode == '0') that.$set(that, `typeList`, res.data);
			},
		}
	}
</script>

<style lang="scss" scoped>
	.main {

		.one {
			padding: 4vw 2vw;
			text-align: center;

			.one_1 {
				padding: 1vw 0;
				font-weight: bold;
				font-size: var(--font16Size);
			}

			.one_2 {
				color: var(--f85Color);
				font-size: var(--font12Size);
			}

		}

		.two {
			text-align: center;

			.button {
				margin: 1vw 0 0 0;
				background-color: var(--f3CColor);
				color: var(--mainColor);
				font-size: var(--font14Size);
			}
		}

		.thr {
			text-align: center;
			padding: 2vw;

			.image {
				width: 50vw;
				height: 50vw;
			}
		}

		.four {
			padding: 2vw;
		}
	}
</style>