<template>
	<view class="container">
		<view class="main">
			<view class="qrcode">
				<image class="qrcode_pic" :src="imagePath" mode="widthFix" />
			</view>
			<view class="desc">扫码体验小程序</view>
			<view class="share_btn">
				<view class="save" @click="headerQrcodeClick">保存二维码</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		picUrl,
		request
	} from '../../../utils';
	import {
		apiArr
	} from '../../../api/user';
	import permision from "../../../js_sdk/wa-permission/permission.js"
	export default {
		data() {
			return {
				imagePath: 'https://static.hshuishang.com/test_qrcode.jpg',
			}
		},
		methods: {
			async init() {
				const res = await request(apiArr.createQrcode, 'POST', {
					page: '/page/index/index',
					scene: `device=${wx.getStorageSync('device_id')}`
				});
				let img = picUrl + res.data;
				console.log('img', img)
				this.imagePath = img;
			},

			headerQrcodeClick() {
				console.log('2');
				const that = this;
				uni.downloadFile({
					url: that.imagePath, // 网络图片地址
					success: async (res) => {
						console.log('11312312313', res);
						if (res.statusCode === 200) {
							const tempFilePath = res.tempFilePath; // 获取临时文件路径
							const systemInfo = uni.getSystemInfoSync();
							console.log('获取当前设备信息', systemInfo); //TODO: 代验证 ios 安卓是否可保存
							if (systemInfo.uniPlatform === 'mp-weixin') {
								// 2. 检查用户授权
								uni.getSetting({
									success(settingRes) {
										console.log('查询授权状态')
										if (!settingRes.authSetting['scope.writePhotosAlbum']) {
											// 未授权，请求授权
											uni.authorize({
												scope: 'scope.writePhotosAlbum',
												success() {
													// 授权成功，保存图片
													that.saveImageToAlbum(tempFilePath);
												},
												fail(err) {
													console.log('131111', err)
													// 授权失败，提示用户手动打开授权
													uni.showModal({
														title: '提示',
														content: '需要授权保存图片到相册，请手动打开授权设置',
														success(modalRes) {
															if (modalRes.confirm) {
																uni.openSetting(); // 打开授权设置页面
															}
														}
													});
												}
											});
										} else {
											// 已授权，保存图片
											that.saveImageToAlbum(tempFilePath);
										}
									},
									fail(err) {
										console.log('检查用户授权错误', err);
									}
								});
								return;
							}
							if (systemInfo.osName === 'ios') {
								const result = await permision.judgeIosPermission("photoLibrary");
								if (result == 1) {
										// strStatus = "已获得授权"
										that.saveImageToAlbum(tempFilePath);
								} else if (result == 0) {
										// strStatus = "未获得授权"
										permision.gotoAppPermissionSetting()
								} else {
										// strStatus = "被永久拒绝权限";
										permision.gotoAppPermissionSetting()
								}
							}
							if (systemInfo.osName === 'android') {
								const res = await permision.requestAndroidPermission('android.permission.WRITE_EXTERNAL_STORAGE');
								if (result == 1) {
										// strStatus = "已获得授权"
										that.saveImageToAlbum(tempFilePath);
								} else if (result == 0) {
										// strStatus = "未获得授权"
										permision.gotoAppPermissionSetting()
								} else {
										// strStatus = "被永久拒绝权限"
										permision.gotoAppPermissionSetting()
								}
							}

						}
					},
					fail() {
						uni.showToast({
							title: '图片下载失败',
							icon: 'none'
						});
					}
				});
			},

			saveImageToAlbum(tempFilePath) {
				uni.saveImageToPhotosAlbum({
					filePath: tempFilePath,
					success() {
						uni.showToast({
							title: '保存成功',
							icon: 'success'
						});
					},
					fail() {
						uni.showToast({
							title: '保存失败',
							icon: 'none'
						});
					}
				});
			},


		},
		onLoad() {
			// this.init();
		}
	}
</script>

<style>
	@import url("./index.css");
</style>