<template>
	<view class="pages">
		<view class="" style="font-size: 36rpx;font-weight: 700;" v-if="muster.muster">
			专属森林向导提醒您
		</view>
		<view class="content" v-if="muster.muster">
			<u-parse :content="muster.muster"></u-parse>
		</view>
		<view class="" style="font-size: 36rpx;font-weight: 700;" v-if="muster.qrcode_url">
			您的专属森林向导
		</view>
		<view class="image" v-if="muster.qrcode_url">
			<image :src="muster.qrcode_url" mode="aspectFill"></image>
		</view>
		<view class="submit" @click="savePoster()" v-if="muster.qrcode_url">
			保存二维码到相册
		</view>
		<view v-if="!muster.muster" class="more">暂无</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				muster: ''
			}
		},
		onLoad(o) {
			console.log(o)
			if (o.muster) {
				let muster = decodeURIComponent(o.muster)
				console.log(muster)
				this.muster = JSON.parse(muster)
				console.log(this.muster)
			}
		},
		methods: {
			init() {
				let list = uni.getStorageSync("data")
				// console.log(list[5])
				this.list = list[5].value.phone
			},
			toPhone(item) {
				uni.makePhoneCall({
					phoneNumber: item //仅为示例
				});
			},
			savePoster() {
				

					uni.showLoading({
						title: '正在保存图片...'
					});
					//获取用户的当前设置。获取相册权限
					uni.getSetting({
						success: (res) => {
							//如果没有相册权限
							if (!res.authSetting["scope.writePhotosAlbum"]) {
								//向用户发起授权请求
								uni.authorize({
									scope: "scope.writePhotosAlbum",
									success: () => {
										//授权成功保存图片到系统相册
										uni.downloadFile({
											url: this.muster.qrcode_url,
											success: (res) => {
												console.log(res)
												if (res.statusCode === 200) {
													uni.saveImageToPhotosAlbum({
														filePath: res.tempFilePath,
														success: function() {
															uni.showToast({
																title: "保存成功",
																icon: "none"
															});
														},
														fail: function() {
															uni.showToast({
																title: "保存失败",
																icon: "none"
															});
														}
													});
												}
											}
										})
									},
									//授权失败
									fail: () => {
										uni.hideLoading();
										uni.showModal({
											title: "您已拒绝获取相册权限",
											content: "是否进入权限管理，调整授权？",
											success: (res) => {
												if (res.confirm) {
							//调起客户端小程序设置界面，返回用户设置的操作结果。（重新让用户授权）
													uni.openSetting({
														success: (res) => {
														console.log(res.authSetting);
														},
													});
												} else if (res.cancel) {
													return uni.showToast({
														title: "已取消！",
													});
												}
											},
										});
									},
								});
							} else {
								//如果已有相册权限，直接保存图片到系统相册
								uni.downloadFile({
									url: this.muster.qrcode_url,
									success: (res) => {
										console.log(res)
										if (res.statusCode === 200) {
											uni.saveImageToPhotosAlbum({
												filePath: res.tempFilePath,
												success: function() {
													uni.showToast({
														title: "保存成功",
														icon: "none"
													});
												},
												fail: function() {
													uni.showToast({
														title: "保存失败",
														icon: "none"
													});
												}
											});
										}
									}
								})
							}
						},
						fail: (res) => {},
					});
				
				
			},
			
		}
	}
</script>

<style lang="less">
	page {
		background-color: #F4F4F4;
		font-size: 28rpx;
	}

	.pages {
		margin: 40rpx 30rpx;

		.content {
			margin: 24rpx 0 94rpx;
		}

		.image {
			width: 200rpx;
			height: 200rpx;
			// background-color: pink;
			margin: 40rpx auto 73rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.submit {
			margin: 0 auto;
			box-sizing: border-box;
			text-align: center;
			color: #FCFCFC;
			width: 654rpx;
			height: 92rpx;
			line-height: 92rpx;
			background: #1E9F6A;
			border-radius: 8rpx 16rpx 8rpx 16rpx;
		}
	}
</style>
