<template>
	<view class="box">

		<view class="maBoximg">
			
			<image :src="qrImg" mode="" class="QR"></image>
		</view>
		<image src="../../static/img/saveBtn.png" mode="" class="saveBtn" @click="saveEwm"></image>

	</view>
</template>

<script>
	
	export default {
		
		data() {
			return {
				base64:'',
				qrImg: '',
				openSettingBtnHidden: true,//是否授权
			}
		},
		onLoad() {
			 
			this.getQR();
			
		},
		methods: {

			getQR() {
				uni.showLoading({
					title: '加载中'
				})
				let uidStr = uni.getStorageSync("logodata").uid.replace(/\-/g, "");

				let url = 'worldKeepCar/worldDistribution/createMiniAppQrCode',
					params = {
						path: 'pages/homePage/homePage',
						scene: uidStr,
					}


				this.$httpBinary(url, params, 'GET').then(res => {
					uni.hideLoading();

                    this.base64=uni.arrayBufferToBase64(res)
					this.qrImg = 'data:image/png;base64,' + uni.arrayBufferToBase64(res)
					
				})
			},
			
			
					//微信小程序保存到相册
					        saveEwm:function(e){
								let that = this;
					            //获取相册授权
					               uni.getSetting({
					                 success(res) {
					                   if (!res.authSetting['scope.writePhotosAlbum']) {
					                     uni.authorize({
					                       scope: 'scope.writePhotosAlbum',
					                       success() {
					                         //这里是用户同意授权后的回调
					                         
											 that.saveImage()
					                       },
					                       fail() {//这里是用户拒绝授权后的回调
					                           that.openSettingBtnHidden=false
					                       }
					                     })
					                   } else {//用户已经授权过了
					                     
										 that.saveImage()
					                   }
					                 }
					               })
					        },
							
							saveImage(){
								let base64=this.base64;
								let fileName = new Date().getTime();
								 let filePath=wx.env.USER_DATA_PATH + `/hym_pay${fileName}_qrcode.png`;
								 uni.getFileSystemManager().writeFile({
								                 filePath:filePath ,  //创建一个临时文件名
								                 data: base64,    //写入的文本或二进制数据
								                 encoding: 'base64',  //写入当前文件的字符编码
								                 success: res => {
								                     uni.saveImageToPhotosAlbum({
								                         filePath: filePath,
								                         success: function(res2) {
								                             uni.showToast({
								                                 title: '保存成功',
								                                 icon:"none",
								                                 duration:5000
								                             })
								                         },
								                         fail: function(err) {
								                             // console.log(err.errMsg);
								                         }
								                     })
								                 },
								                 fail: err => {
								                     //console.log(err)
								                 }
								             })
							},
							
							
							 
							
							

			
		}
	}
</script>

<style>
	.box {
		min-height: 100vh;
		width: 100%;
		background-image: url('http://123.56.219.146:10086/keepCar/yqyl.png');
		background-size: 100% 100%;

		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;

	}

	.maBoximg {
		width: 377rpx;
		height: 377rpx;

		border-radius: 19rpx;
		background-color: #FFFFFF;
		padding: 20rpx;

	}
	.QR{
		width: 100%;
		height: 100%;
	}

	.saveBtn {
		width: 411rpx;
		height: 115rpx;
		margin-top: 60rpx;
	}
</style>
