<template>
	<view style="height: 100%;">
		<image mode="widthFix" src="/static/login_bg.jpg" class="full_img"></image>
		<u-navbar title="登录" background="{ background: '#ffffff',opacity:0.4; }" title-color="#ffffff" back-icon-color="#ffffff"></u-navbar>
		<view style="padding: 0 40rpx;margin-top: 90%;"><u-button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">微信授权登录</u-button></view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			code: ''
		};
	},
	mounted() {},
	methods: {
		getPhoneNumber(e) {
			let that = this;
			uni.checkSession({
			  success () {
			    //session_key 未过期，并且在本生命周期一直有效
				that.putInfo(e)
			  },
			  fail () {
			    // session_key 已经失效，需要重新执行登录流程
			    uni.login({
			    	provider: 'weixin',
			    	success: function(loginRes) {
			    		that.code = loginRes.code;
						that.putInfo(e)
			    	}
			    });
			  }
			})
		},
		putInfo(e){
			let that = this;
			if (e.detail.encryptedData) {
				let inviteCode =  uni.getStorageSync("inviteCode") 
				//用户同意授权
				var  datas = {
					iv: e.detail.iv,
					encryptedData: e.detail.encryptedData,
					code: that.code
				};
				if(inviteCode){
					datas.inviteCode = inviteCode
				}
				that.$api.login(datas).then(
					res => {
						if (res.data.code == 200) {
							uni.setStorageSync('union_id', res.data.data.union_id);
							uni.setStorageSync('token', res.data.data.token);
							that.$api.getInfo().then(resdata => {
								if(resdata.data.code == 200){
									uni.navigateBack();
									that.$store.state.userInfo = resdata.data.data;
								}
								
							});
						} else {
							uni.showModal({
								title: '提示',
								content: res.data.msg,
								showCancel: false
							});
						}
					},
					err => {
						console.log(err);
					}
				);
			}
		}
	},
	onLoad(option) {
		let that = this;
		this.from = option.from;
		uni.login({
			provider: 'weixin',
			success: function(loginRes) {
				that.code = loginRes.code;
			}
		});
	}
};
</script>
<style>
.u-border-bottom:after,
.u-border-left:after,
.u-border-right:after,
.u-border-top-bottom:after,
.u-border-top:after,
.u-border:after {
	border: none !important;
}
page {
	background-color: #bee0ff;
	height: 100%;
	width: 100%;
}
</style>
<style scoped>
.full_img {
	position: absolute;
	left: 0;
	display: block;
	width: 100%;
	z-index: -999;
	top: 0;
}

.head {
	height: 96rpx;
	width: 100%;
	line-height: 96rpx;
	margin-top: 40rpx;
	text-align: center;
	display: flex;
	position: relative;
	justify-content: center;
}
.icon {
	position: absolute;
	left: 30rpx;
}
</style>
