<template>
	<div class="page" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
		<cus-header title='登录' :showback='false'></cus-header>
		<div class="box">
			<image :src="imgBase+'login/login_logo.png'"></image>
			<div class="title">
				欢迎登录传能
				<image :src="imgBase+'login/login_textbg.png'"></image>
			</div>
			<div class="tip">请完成微信授权以继续使用</div>
			<button class="login" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">同意授权登录</button>
			<div class="cancel" @tap="cancelLogin">取消登录</div>
		</div>
	</div>
</template>

<script>
	export default {
		data(){
			return {
				imgBase:this.$imgBase
			}
		},
		methods:{
			decryptPhoneNumber(e){
				if(uni.getStorageSync('token')){
					uni.reLaunch({
						url:'/pagesHome/index'
					})
					return
				}
				if(e.detail.code) this.wxLogin(e.detail.code);
			},
			wxLogin(code){
				uni.showLoading({ title:'登录中' });
				let that = this;
				wx.login({
					success(res){
						that.$api.get('/wx/login',{
							code:res.code,
							phoneCode:code
						},false).then(res=>{
							if(res.data.code===0){
								uni.setStorageSync('token',res.data.data.token);
								uni.setStorageSync('userInfo',JSON.stringify({
									id:res.data.data.id,
									gender:res.data.data.gender,
									realName:res.data.data.realName,
									email:res.data.data.email,
									headUrl:res.data.data.headUrl
								}));
								uni.hideLoading();
								that.$showToast('登录成功');
								setTimeout(()=>{
									uni.reLaunch({
										url:'/pagesHome/index'
									})
								},1500)
							}else that.$showToast(res.data.msg)
						})
					}
				})
			},
			cancelLogin(){
				uni.exitMiniProgram();
			}
		}
	}
</script>

<style scoped lang="scss">
	.page{
		background: #fff;
		.box{
			width: 100%;
			padding: 0 55rpx;
			box-sizing: border-box;
			display: flex;
			flex-direction: column;
			align-items: center;
			&>image{
				width: 182rpx;
				height: 182rpx;
				margin-top: 169rpx;
			}
			.title{
				font-family: PingFang-SC, PingFang-SC;
				font-weight: bold;
				font-size: 48rpx;
				color: #1D2129;
				line-height: 67rpx;
				text-align: center;
				position: relative;
				margin-top: 11rpx;
				z-index: 1;
				letter-spacing: 2rpx;
				&>image{
					width: 198rpx;
					height: 18rpx;
					position: absolute;
					z-index: -1;
					left: -14rpx;
					bottom: 4rpx;
				}
			}
			.tip{
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #86909C;
				line-height: 40rpx;
				margin-top: 88rpx;
				letter-spacing: 2rpx;
			}
			
			.login{
				width: 100%;
				height: 88rpx;
				background: #198CFF;
				border-radius: 16rpx;
				text-align: center;
				line-height: 88rpx;
				font-family: PingFang-SC, PingFang-SC;
				font-weight: bold;
				font-size: 32rpx;
				color: #FFFFFF;
				letter-spacing: 2rpx;
				margin-top: 36rpx;
			}
			
			.cancel{
				width: 100%;
				height: 88rpx;
				border: 1rpx solid #ECECEC;
				border-radius: 16rpx;
				text-align: center;
				line-height: 88rpx;
				font-family: PingFang-SC, PingFang-SC;
				font-weight: bold;
				font-size: 32rpx;
				color: #4E5969;
				letter-spacing: 2rpx;
				margin-top: 32rpx;
			}
		}
	}
</style>