<template>
	<div class="auth-page">
		<u-popup v-model="showDialog" mode="center" :mask-close-able="false" custom-style="padding:0;background:none;">
			<img :src="pageinfo.pic_url" mode="widthFix" />
			<button :style="sureStyle" class="hotsopt" open-type="getUserInfo" @getuserinfo="getUserInfo"></button>
			<button :style="cancelStyle" class="hotsopt" @click="close"></button>
		</u-popup>
		<coupon
		  v-if="showCoupons"
		  :coupon-message="couponMessage"
		  @goLook="goLook"
		  @closeBtn="closeBtn"
		></coupon>
		
	</div>
</template>

<script>
	import coupon from "@/components/coupons/coupons";
	
export default {
	components:{
		coupon
	},
	
	data() {
		return {
			pageinfo: {},
			sureStyle: '',
			cancelStyle: '',
			showDialog: true,
			showCoupons:false,
			couponMessage:'',
		};
	},
	created() {
		this.pageinfo = uni.getStorageSync('basedata') ? uni.getStorageSync('basedata').auth_page : {};
		let x = this.pageinfo.hotspot;
		let y = this.pageinfo.hotspot_cancel;
		this.sureStyle = `height:${x.height}rpx;width:${x.width}rpx;left:${x.left}rpx;top:${x.top}rpx;`;
		this.cancelStyle = `height:${y.height}rpx;width:${y.width}rpx;left:${y.left}rpx;top:${y.top}rpx;`;
	},
	methods: {
		getUserInfo(e) {
			var that = this;
			that.showDialog=true;
			uni.getProvider({
				service: 'oauth',
				success: function(res) {
					if (~res.provider.indexOf('weixin')) {
						uni.login({
							provider: 'weixin',
							success: res => {
								that.authorization = res.code;
								uni.getUserInfo({
									provider: 'weixin',
									success: info => {
										//这里请求接口
										let obj = {
											Source: 1,
											OpenId: '',
											Name: info.userInfo.nickName,
											Photo: info.userInfo.avatarUrl,
											Moblie: '',
											SuperiorId: 0,
											code: res.code
										};
										that.getCode(obj);
									},
									fail: () => {
										uni.showToast({ title: '微信登录授权失败', icon: 'none' });
									}
								});
							},
							fail: () => {
								uni.showToast({ title: '微信登录授权失败', icon: 'none' });
							}
						});
					} else {
						uni.showToast({
							title: '请先安装微信或升级版本',
							icon: 'none'
						});
					}
				}
			});
		},
		close() {
			this.showDialog=false;
			this.$emit('gbAuth');
		},
		//调用获取code
		getCode(obj) {
			var that = this;
			uni.getUserInfo({
				provider: 'weixin',
				success: info => {
					//这里请求接口
					var OpenId = '';
					that.request2(
						{
							url: '/api/mall/GetWeChatOpenId',
							data: {
								Code: obj.code
							}
						},
						res => {
							if (res.resultCode == 1) {
								obj.OpenId = res.data;
								that.getLogin(obj);
							}
						}
					);
				}
			});
		},
		//登录
		getLogin(obj) {
			var that = this;
			let pid = uni.getStorageSync("pid")?uni.getStorageSync("pid").pid:0;
			that.request2(
				{
					url: '/api/AppletLogin/Login',
					data: {
						Source: 1,
						OpenId: obj.OpenId,
						Name: obj.Name,
						Photo: obj.Photo,
						Moblie: '',
						SuperiorId: pid
					}
				},
				res => {
					if (res.resultCode == 1) {
						this.showDialog=false;
						uni.setStorageSync('mall_UserInfo', res.data);
						that.$emit('changeuserinfo');
						if(res.couponResultCode==1){
							this.showCoupons = true;
							this.couponMessage = res.couponMessage;
						}
						uni.removeStorageSync('pid');
					}
				}
			);
		},
		goLook(){
			this.showCoupons = false;
			
		},
		closeBtn(){
			this.showCoupons = false
		}
	}
};
</script>

<style>
.auth-page img {
	width: 650rpx;
	height: 700rpx;
}
.auth-page .hotsopt {
	position: absolute;
	background: transparent;
	border: none;
	outline: none;
}
.auth-page .hotsopt::after {
	border: none;
}
</style>
