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

<script>
	export default {
		data() {
			return {
				muster: ''
			}
		},
		onLoad(o) {
			if (o.muster) {
				let muster = decodeURIComponent(o.muster)
				this.muster = JSON.parse(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.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"
									});
								}
							});
						}
					}
				})

				// uni.getSetting({ //获取用户的当前设置
				// 	success: (res) => {
				// 		if (res.authSetting['scope.writePhotosAlbum']) { //验证用户是否授权可以访问相册
				// 			this.saveImageToPhotosAlbum();
				// 		} else {
				// 			uni.authorize({ //如果没有授权，向用户发起请求
				// 				scope: 'scope.writePhotosAlbum',
				// 				success: () => {
				// 					this.saveImageToPhotosAlbum();
				// 				},
				// 				fail: () => {
				// 					uni.showToast({
				// 						title: "请打开保存相册权限，再点击保存相册分享",
				// 						icon: "none",
				// 						duration: 3000
				// 					});
				// 					setTimeout(() => {
				// 						uni.openSetting({ //调起客户端小程序设置界面,让用户开启访问相册
				// 							success: (res2) => {
				// 								this.saveImageToPhotosAlbum()
				// 								// console.log(res2.authSetting)
				// 							}
				// 						});
				// 					}, 3000);
				// 				}
				// 			})
				// 		}
				// 	}
				// })
			},
			saveImageToPhotosAlbum() {
				let base64 = this.muster.qrcode_url.replace(/^data:image\/\w+;base64,/, ""); //去掉data:image/png;base64,
				let filePath = wx.env.USER_DATA_PATH + '/ph_fit_qrcode.png';
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				uni.getFileSystemManager().writeFile({
					filePath: filePath, //创建一个临时文件名
					data: base64, //写入的文本或二进制数据
					encoding: 'base64', //写入当前文件的字符编码
					success: res => {
						uni.saveImageToPhotosAlbum({
							filePath: filePath,
							success: function(res2) {
								uni.hideLoading();
								uni.showToast({
									title: '保存成功',
									icon: "none",
									duration: 5000
								})
							},
							fail: function(err) {
								uni.hideLoading();
								// console.log(err.errMsg);
							}
						})
					},
					fail: err => {
						uni.hideLoading();
						//console.log(err)
					}
				})
			},
		}
	}
</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>
