<template>
	<view class="index">
		<view class="indextextbackground">
			<view class="indextext flex-colum">
				<view class="indextextone">
					分享到微信 提升店铺影响力
				</view>
				<image class="indextexttowimage" :src="src" @longpress="openimg" mode=""></image>
				<view class="indextextbox flex-center yks-sub1" @click="saveImg">
					<u-icon name="weixin-fill" color="#42ae3e" size="28"></u-icon>
					<text class="indextextboxtext">分享到微信</text>
				</view>
			</view>

		</view>

	</view>
</template>
<script>
	export default {
		data() {
			return {
				src: ''
			}
		},
		async onLoad() {
			let res = await this.api.usercreateminicode()
			this.src = res.data
			console.log(res.data)
		},
		onShow() {},
		methods: {
			openimg(e) {
				uni.previewImage({
					// 需要预览的图片链接列表
					urls:[this.src],
					// 为当前显示图片的链接/索引值
					current:[this.src],
					// 图片指示器样式	
					indicator: 'default',
					// 是否可循环预览
					loop: false,
					// 长按图片显示操作菜单，如不填默认为保存相册
					// longPressActions:{
					// 	itemList:[this.l('发送给朋友'),this.l]
					// },
					success: res => {
						console.log('res', res);
					},
					fail: err => {
						console.log('err', err);
					}
				});
			},
			saveImg() {
				var this_ = this;
				//用户需要授权
				wx.getSetting({
					success: (res) => {
						if (!res.authSetting['scope.writePhotosAlbum']) {
							wx.authorize({
								scope: 'scope.writePhotosAlbum',
								success: () => { // 同意授权        
									this.saveImg1();
								},
								fail: (res) => {
									//authorize:fail auth deny" 用户首次点拒绝
									//authorize:fail 系统错误，错误码：-12006,auth deny  用户点了拒绝后除非清缓存（用户删小程序），否则会默认这个选项
									//上述两个都要弹出引导说要授权相册              
									var string = res.errMsg;
									if (string.indexOf("auth deny") >
										0
									) { //用户点了拒绝后，以后都会出现这个错误                                        
										//c.handleSetting(c);
										wx.showModal({
											title: '提示',
											content: '保存图片失败，请点击下方去授权按钮开启相册访问权限再点击保存',
											showCancel: false,
											success(res) {
												if (res.confirm) {
													this_.setData({
														is_shouquan: 0
													})
												}
											}
										})
									}
								}
							})
						} else {
							this_.saveImg1();
						}
					},
					fail: (res) => {
						wx.showToast({
							title: res,
							icon: 'none',
							duration: 2000,
						})
						console.log(res);
					}
				})
			},
			saveImg1() {
				uni.downloadFile({
					url: this.src,
					success: (data) => {
						console.log(data)
						wx.showShareImageMenu({
							path: data.tempFilePath,
							success: (res) => {
								wx.showToast({
									title: '已成功！',
									icon: 'none',
									duration: 3000,
								});
							},
						})

					},
					fail: (res) => {
						console.log(res)
						if (res.errMsg.indexOf('deny') != -1 || res.errMsg.indexOf('denied') != -1) {
							wx.showToast({
								title: '保存相册失败，请设置权限！',
								icon: 'none',
								duration: 2000,
							})

						} else {
							wx.showToast({
								title: '保存相册失败，请重试！',
								icon: 'none',
								duration: 2000,
							});
						}
					}
				});
				// uni.getimageinfo({
				// 	src: this.src,
				// 	success:(data)=>{
				// 		console.log(data)
				// 		wx.showShareImageMenu({
				// 			path: data.path
				// 		})
				// 	},

				// })
			},
		}
	}
</script>

<style lang="scss">
	page {
		background: #f5f5f5;
	}

	.index {
		.indextextbackground {
			position: relative;
			width: 100%;
			height: 250rpx;
			padding: 28rpx 32rpx;
			border-bottom-left-radius: 35%;
			border-bottom-right-radius: 35%;
			background: #fec24d;

			.indextext {
				width: 100%;
				margin-top: 40rpx;
				padding: 28rpx 0;
				justify-content: flex-start;
				border-radius: 20rpx;
				background: #fff;
			}

			.indextexttowimage {
				width: 400rpx;
				height: 400rpx;
			}

			.indextextone {
				font-size: 28rpx;
				font-family: PingFang SC-Bold, PingFang SC;
				font-weight: bold;
				color: #000;
			}

			.indextextbox {
				margin-top: 20rpx;
				border-radius: 50rpx;
				padding: 16rpx 0;
				text-align: center;
				width: 70%;
				background: linear-gradient(115deg, #ffe873 0%, #fec44f 100%);

				.indextextboxtext {
					margin-left: 20rpx;
					font-size: 28rpx;
					font-family: PingFang SC-Bold, PingFang SC;
					font-weight: 400;
					color: #000;
				}
			}
		}
	}
</style>