<template>
	<view class="container" >
	<view ><!-- vsv-if="!wxOpendata&&isload" -->
		<view class="logoBox">
			<image src="../../static/img/euro.png" mode="" class="euroImg"></image>
		</view>
		<view class="title1">请完成微信授权以继续使用</view>
		<button class="wxlogin"  @click="wxgologin">授权微信用户信息</button>
		<view class="title2" @click="goIndex">取消授权</view>
	</view>
	
   </view>

</template>

<script>
export default {
	data() {
		return {
			
			wxdata:'',
			wxCode:'',
			openID:'',
			session_key:'',
			wxPhoneData:'',
			type:'',
			wxOpendata:'',
			isload:false,
			shopID:'',
			loginType:'',
		}
	},
	onLoad(opt) {
      this.type=opt.type;
	  this.loginType=opt.loginType;
	  this.wxOpendata=uni.getStorageSync("wxOpendata");
	  if(this.loginType==1){
		  
	  }else{
		 if( this.wxOpendata){
		 		  /* uni.switchTab({
		 		  	url:'../index/index'
		 		  }) */
		 		  uni.navigateTo({
		 		  	url:'../index/index'
		 		  })
		 }else{
		 		  this.isload=true;
		 } 
	  }
	  if(opt.shopId){
		  this.shopID=opt.shopId
	  }
	  
	},
	methods: {
		goIndex(){
			/* uni.switchTab({
				url:'../index/index'
			}) */
			uni.navigateTo({
				url:'../index/index'
			})
		},
		 getPhoneNumber: function(e) {
		                console.log(e);
			this.wxPhoneData=e.detail;
			this.gologin()
		     //this.wxgologin()         
		 },
        privacyClick(){
			this.privacyCk=!this.privacyCk
		},
		iphoneLogin(){
			uni.navigateTo({
				url:'iphoneLogin'
			})
		},
		wxgologin(){
			var that=this;
			uni.getUserProfile({
				lang:'zh_CN',
				desc:'登录',
				success:(res)=>{
					console.log(res);
					that.wxdata=res;
					 uni.setStorage({
						key: 'wxdata',
						data: res,
						success: function () {
						    that.ipLogin()
						 
						}
					}); 
				},
				fail:(res)=>{
					console.log(res)
				}
			});
		},
		ipLogin(){
			var that=this;
			 uni.showLoading({ title: '加载中'});
			 uni.login({
			   provider: 'weixin',
			   success: function (loginRes) {
			     console.log(loginRes)
				that.wxCode=loginRes.code
				that.getWxOpenID()
					//that.gologin()
				 
			   
			   }
			 });
			
		},
		gologin(){
			/* if(this.wxPhoneData&&this.wxCode){
				this.getWxOpenID()
			} */
		},
		getWxOpenID(){
		
			var that=this;
			 this.$http('km66MiniApp/sys/getWxOpenID', {
			   code:this.wxCode,
			   shopID:this.shopID,
			   nickName:this.wxdata.userInfo.nickName,
			   headUrl:this.wxdata.userInfo.avatarUrl,
			 },'GET').then(res => {
				console.log(res)
				this.openID=res.data.openid;
				this.session_key=res.data.session_key;
				this.shopID=res.data.shopID
				console.log(this.openID)
				uni.setStorage({
					key: 'wxOpendata',
					data: res.data,
					success: function (rrr) {
					   // that.ipLogin()
					   uni.hideLoading();
					   if(that.shopID){
						   uni.redirectTo({
						    	url:'login?shopId='+that.shopID
						    })
					   }else{
						   uni.navigateTo({
						   	url:'../user/scanCode'
						   })
					   }
					   
					  /* if(!that.type){
						  uni.switchTab({
						  	url:'../index/index'
						  }) 
					  }else{
						  uni.redirectTo({
						  	url:'login'
						  }) 
					  } */
					}
				}); 
				//this.Login()
			})
		},
		Login(){
			var that=this;
			this.$http('miniApp/sys/wxLogin', {
			   openID:this.openID,
			   encrypData:this.wxPhoneData.encryptedData,
			   iv:this.wxPhoneData.iv,
			   sessionKey:this.session_key,
			   nickName:this.wxdata.userInfo.nickName,
			   headUrl:this.wxdata.userInfo.avatarUrl,
			 },).then(res => {
				//console.log(res)
				uni.hideLoading();
				if(res.code==0){
					uni.setStorage({
						key: 'logodata',
						data: res.data,
						success: function () {
						 
						}
					}); 
					uni.showToast({
					     title: '登录成功',
					 	 icon:'none',
					     duration: 3000
					 });
					 uni.switchTab({
						 url:'../index/index'
					 }) 
				}else{
					uni.showToast({
					     title: res.msg,
					 	 icon:'none',
					     duration: 3000
					 });
				}
				
			})
		}
	}
}
</script>

<style scoped>
 .euroImg{
	 width: 290rpx;
	 height: 361rpx;
 }
 .logoBox{
	 text-align: center;
	 padding-top: 160rpx;
 }
 .title1{
	 font-size: 26rpx;
	 text-align: center;
	 color: #576B95;
	 padding-top: 150rpx;
 }
 .wxlogin{
	 width: 658rpx;
	 height: 88rpx;
	 background: linear-gradient(113deg, #FF630F 0%, #FF450F 100%);
	 border-radius: 10rpx;
	 line-height: 88rpx;
	 text-align: center;
	 color: #FFFFFF;
	 font-size: 30rpx;
	 margin-top: 20rpx;
 }
 .title2{
	 width: 100vw;
	 position: fixed;
	 left: 0;
	 bottom: 50rpx;
	 font-size: 26rpx;
	 text-align: center;
	 color: #576B95;
 }
</style>
