<template>
	<view  :class="[AppTheme]">
		<view class="goCode1">
			<view class="code1">
				<image :src="qrcode.img2" alt="" class="img" />
			</view>	 
		</view>
		<view class="goCode">
			<view class="code">
				<image :src="qrcode.img1" alt="" class="img" />
			</view>
			<view class="vueDAlertWrapperBtn bg-primary" @click.stop="saveImage()">
				保存店铺名片
			</view>
		</view>	
	</view>
</template>

<script>
	// #ifdef H5
	import FileSaver from 'file-saver'
	import wxchar from '@/js/wxchar.js'
	// #endif
	import mineApi from '@/api/home/index.js';
	export default {
		data() {
			return {
				primary: this.$theme.primary,
				settingFile: getApp().globalData.siteinfo,
				qrcode: '',
				farm_id: ''
			}
		},

		onLoad(options) {
			this.farm_id = options.id
			this.qrcodes()
		},
		methods: {
			qrcodes() {
				mineApi.codeqorce({
					farm_id: this.farm_id
				}).then(res => {
					if (res.status == 1) {
						this.qrcode = res.data;
						 
					}
				});
			},
//保存图片到相册
			saveImage() {
				//判断用户授权
				var that = this
				uni.getSetting({
					success(res) {
						console.log('获取用户权限', res.authSetting)
						if (Object.keys(res.authSetting).length > 0) {
							//判断是否有相册权限
							if (res.authSetting['scope.writePhotosAlbum'] == undefined) {
								that.authorize();
							} else {
								if (!res.authSetting['scope.writePhotosAlbum']) {
									that.authorize();
								}
							}
						} else {
							return
						}
					}
				})
				that.saveImg(that.qrcode.img1);
			},
			saveImg(url) {
				console.log(url,'urlurl')
				const that = this;
				uni.downloadFile({
					url: url,
					success: res => {
						console.log(res,'ooooo')
						if (res.statusCode === 200) {
							uni.saveImageToPhotosAlbum({
								filePath: res.tempFilePath,
								success: function() {
									uni.showToast({
										title: '保存下载成功',
										icon: 'none',
										position: 'top'
									})
								},
								fail: function() {
									uni.showToast({
										title: '保存失败，请稍后重试',
										icon: 'none',
										position: 'top'
									})
								}
							});
						} else {
							uni.showToast({
								title: '下载失败',
								icon: 'none',
								position: 'top'
							})
						}
					}
					 
				});
			},
			authorize() {
				let scope = 'scope.writePhotosAlbum'
				return new Promise((resolve, reject) => {
					wx.getSetting({
						success: res => {
							if (!res.authSetting[scope]) {
								wx.authorize({
									scope,
									success: resolve,
									fail: () => wx.showModal({
										content: R.string
											.prompt_authorize_photos, // 请允许小程序使用相册权限
										success: () => wx.openSetting({
											success: res => {
												if (res.authSetting[
														scope]) {
													resolve(res
														.authSetting)
												} else {
													wx.showModal({
														content: R
															.string
															.prompt_authorize_cancel
													}) // 未允许使用该权限
													reject(res.authSetting)
												}
											}
										})
									})
								})
							} else {
								resolve(res.authSetting)
							}
						}
					})
				})
			},

		}
	}
</script>

<style lang="scss">
page{
		width:100%;
		height: 100%;
	}
	.code1 {
		width: 100%;
		height: 100%;
	}
	
	.code1 .img {
		position: fixed; 
		width: 100%;
		height: 100%;
		 
	}
	
	.goCode{
		 display: flex;
		 justify-content: space-evenly;
	
		.code {
			width: 100%;
			height: 100%;
		}
		
		.code .img {
			position: fixed;
			margin-left: 5%;
			margin-top: 3vh;
			width: 90%;
			height: 72%;
			border-radius: 40rpx;
		 
		}
		.vueDAlertWrapperBtn {
			  display: flex;
			 justify-content: space-evenly;
			  margin-top: 80vh;
			 align-items: center;
			 position: absolute;
			 // background-color: blueviolet;
			 color: #fff;
			 padding: 25rpx;
			 border-radius: 40rpx;
			 width: 60%;
			 
		}
	}
	
	 
</style>
