<template>
	<view class="main">
		<view class="one">
			<view class="one_1">{{info.name||'暂无'}}</view>
			<view class="one_2">{{info.create_time||'暂无'}}</view>
			<view v-if="info.path" class="one_2" @tap="toView(info.path)">公告链接：{{info.path||'暂无'}}</view>
		</view>
		<view class="two" v-if="info.file.length>0">
			<swiperImg :imgsList='info.file'></swiperImg>
		</view>
		<view class="thr">
			<rich-text :nodes="formatRichText(info.content)"></rich-text>
		</view>
	</view>
</template>

<script>
	import swiperImg from '../../components/swiper/index.vue';
	export default {
		components: {
			swiperImg
		},
		data() {
			return {
				id: '',
				user: {},
				info: {
					file: []
				},
			}
		},
		onLoad: async function(e) {
			const that = this;
			that.$set(that, `id`, e && e.id || '');
			that.searchToken();
			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(`/notice/${that.id}`, 'GET', {})
					if (res.errcode == '0') {
						that.$set(that, `info`, res.data)
					} else {
						uni.showToast({
							title: res.errmsg,
						});
					}
				}
			},
			// 处理富文本
			formatRichText(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%
			},
			// 查看公告链接
			toView(url) {
				uni.navigateTo({
					url: `/pagesHome/weather/index?url=${url}`
				})
			},
		}
	}
</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;
			padding: 2vw;

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

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