<template>
	<view class="container">
		<view class="x-c" style="height: 50vh;" v-if="loading">
			<u-loading-icon :show="loading" text="加载中" vertical></u-loading-icon>
		</view>
		<template v-if="imgurl">
			<image class="bg" id="bg" :src="imgurl" mode="widthFix" show-menu-by-longpress="true" @longpress="longpressImg"></image>
		</template>
		<u-empty mode="data" v-if="!imgurl && !loading" icon="http://cdn.uviewui.com/uview/empty/data.png" text="暂无数据"></u-empty>
	</view>
</template>

<script>
	import { getAppContactWay } from "@/api/user.js"
	export default {
		data() {
			return {
				loading: false,
				userId: '',
				imgurl: "",
			}
		},
		onLoad() {
			const user= JSON.parse(uni.getStorageSync('userInfo')) ;
			this.userId= user.userId;
			this.getQRCode()
		},
		methods: {
			getQRCode() {
				this.loading = true
				getAppContactWay(this.userId).then(res=>{
					this.loading = false
					if(res.code==200){
						this.imgurl = res.data || ''
					} else {
						uni.showToast({
							title: res.msg,
							icon: ''
						})
					}
				}).catch(()=>{
					this.loading = false
				})
			},
			longpressImg() {
				// #ifndef MP-WEIXIN
				uni.showActionSheet({
					title: "保存图片，微信扫图片描二维码添加健康管家",
					itemList: ["保存图片"],
					success: (res)=> {
						console.log("保存图片")
						this.downImg()
					}
				});
				// #endif
			},
			downImg() {
				uni.showLoading({
					title: "图片保存中..."
				})
				uni.downloadFile({
					url: this.imgurl,
					fail: function(res) {
					  uni.showModal({
						title: '提示',
						content: '保存失败',
					  })
					  uni.hideLoading();
					},
					success: function(res) {
						var tempFilePath = res.tempFilePath;
						uni.saveImageToPhotosAlbum({
							filePath: tempFilePath,
							success:()=> {
							  uni.showToast({
								title: "保存成功",
								duration: 2000
							  })
							},
							fail:()=>{
							  console.log("图片失败");
							  uni.showToast({
									title: "图片失败",
									duration: 2000,
									icon: "error"
							  })
							  uni.hideLoading();
							},
							complete: function() {
							  uni.hideLoading();
							}
						})
					},
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		position: relative;
	}
	.bg {
		width: 100%;
	}
	.qrcode {
		width: 186rpx;
		height: 186rpx;
		box-sizing: border-box;
		background: #FFFFFF;
		border-radius: 6rpx;
		border: 3px solid #FB601F;
		position: absolute;
		right: 39rpx;
		bottom: 32rpx;
		z-index: 9;
	}
</style>