<template>
	<view style="background-color: #FFFFFF;min-height: 100vh;">
		<!-- solt begin -->
		<view class="flex  flex-direction  align-center " style="padding-top: 160rpx;">
			<u-avatar  size="130" src="http://pic2.sc.chinaz.com/Files/pic/pic9/202002/hpic2119_s.jpg"></u-avatar>
			<text  style="margin:30rpx 0 100rpx 0rpx;color: #333333;font-size: 26rpx;font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;">Hi~登陆玩转社区吧</text>
			<button class="cu-btn round wx-btn " open-type="getPhoneNumber" @getphonenumber="tapGetPhoneNumber">
				<view class="text-center"  style="position: relative;left: -100rpx;">
					<text class="cuIcon-weixin" style="font-size: 40rpx;color: #216a21;"></text>
				</view>
				<view style="position: relative;left: -16rpx;">
					<text class="text-center" style="font-size: 28rpx;">微信立即登陆</text>
				</view>
			</button>
			<view @click="hide" class="cu-btn round line-gray " style="font-size: 26rpx;width: 200rpx;margin-top: 80rpx;">
				<text style="color: #333333;">暂不登陆</text>
			</view>
		</view>
		<!-- solt end -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				respWx:{},
				respLogin:{},
				timer:null,
				inviteCode:null,
			}
		},
		beforeDestroy() {
		  if(this.timer){
		    clearTimeout(this.timer)
		    this.timer = null
		    console.log('clearTimeout')
		  }
		},
		onReady() {
			this.show()
		},
		methods:{
			hide () {
			  if(this.timer){
			    clearTimeout(this.timer)
			    this.timer = null
			  }
			},  
			show(){
				  let that=this
				  this.inviteCode = this.$auth.getInviteCode() 
				  this.$mpi.wxLogin().then(res => {
					this.respWx = res
					console.log("res",res);
				  })
				  this.setTimer()
			},
			async setTimer(){
				this.respWx = await this.$mpi.wxLogin()
				this.timer = setTimeout(()=>{
					this.setTimer()
				},240000) // 每隔4分钟重新请求 jscode
			},
			async tapGetPhoneNumber (e) {
			  if(e.detail.errMsg != 'getPhoneNumber:ok') {
			    return
			  }
			  this.login(e.detail)
			},
			async login(detail){
			  console.log("2:login",detail);
			  try{
			    this.$dialog.showLoading()
			    if(!this.respLogin.sessionKey){
			      this.respLogin = await this.$api.loginByCode({
			        _isReject:true,
			        code:this.respWx.code
			      })
			    }
				console.log(this.respLogin);
			    let resp = await this.$api.loginByWxapp({
			      _isReject:true,
			      openId: this.respLogin.openId,
			      sessionKey: this.respLogin.sessionKey,
			      encryptedData: detail.encryptedData,
			      iv: detail.iv,
				  inviteCode: this.inviteCode
			    })
			    console.log(56,resp)
			    resp.openId = this.respLogin.openId
			    resp.sessionKey = this.respLogin.sessionKey
			    let userType = this.$global.userType.member
			    this.$auth.login(userType, resp.sessionId, resp.userId, resp)
			    this.hide()
			    this.$util.refreshPage(['/pagesM/pages/category','/pagesM/pages/shop_car'])
				// this.$auth.setInviteCode(null);
				this.$auth.removeInviteCode();
				let a = this.$auth.getInviteCode();
				if(this.inviteCode){
					this.$emit('getDistributorCoupon')
				}else{
					this.$emit('signIn', resp)
				}
			  }catch(err){
			    // console.log(72,err)
			    this.$dialog.alert({
			      content: err.errmsg || '服务繁忙~'
			    })
			  }finally{
			    this.$dialog.hideLoading()
			    if(this.timer){
			      clearTimeout(this.timer)
			      this.setTimer()
			    }
			  }
			},
		},
	}
</script>

<style lang="scss">
.wx-btn{
	background-color: #2ba246;
	color: #FFFFFF;
	width: 600rpx;
	padding: 40rpx 0rpx;
}
</style>
