<template>
	<view class="login-box">
		<view class="login-title">登录</view>
		<view class="logo">
			<image src="@/static/login/logo.png" mode="aspectFit"></image>
		</view>
		<view class="desc">
			<image src="@/static/login/desc.png"></image>
		</view>
		<view class="form">
			<view class="form_item">
				<image class="img1" src="@/static/login/phone.png"></image>
				<input class="inp1" placeholder-class="pl" type="number" v-model="phone" placeholder="请输入手机号码" />
			</view>
			<view class="form_item">
				<image class="img1" src="@/static/login/password.png"></image>
				<input class="inp1" password="true" placeholder-class="pl" type="text" v-model="password" placeholder="请输入密码" />
			</view>
		</view>
		<view style="padding-top: 76rpx;">
			<button class="btn" @click="userLogin()">登录</button>
		</view>

		<view class="other-login" @click="getUserProfile()">
			<view class="ol-tilte">
				<text>快捷登录</text>
			</view>
			<div class="wx-login">
				<image src="@/static/login/weixin_icon.png" mode="aspectFit" />
			</div>
		</view>

		<image class="bottom_img" src="@/static/login/bottom_img.png" mode="widthFix" />

		<view class="popup-box" v-show="showPopup">
			<view class="login-popup">
				<view class="lp-top">
					<view class="lpt-left">
						<view class="title"> <text>登录</text> </view>
						<view class="tips"> <text>登录即可解锁更多权益</text> </view>
					</view>
					<view class="lpt-right" @click="showPopup=false">
						<image src="@/static/login/close_icon.png" mode="aspectFill"></image>
					</view>
				</view>

				<view class="lp-center">
					<view class="title">申请获取以下权限</view>
					<view class="tips">获得你的公开信息（昵称、头像等）</view>
					<view class="tips">获得你的手机号码</view>
				</view>

				<view class="lp-bottom">
					<!-- getUserInfo -->
					<button class="option-btn"
						open-type="getPhoneNumber"
						@getphonenumber="getPhoneNumber">
						获取手机号
					</button>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				phone: '',
				password: '',
				backpage: '',
				backtype: '',
				wxUserInfo: null, // 保存微信用户数据(临时)
				wxLoginInfo: {
					code: null,
					openid: null,
					session_key: null
				}, // 微信登录返回信息（临时）
				showPopup: false, // 获取电话弹窗
			}
		},
		onLoad(e) {
			this.backpage = e.backpage == undefined ? 'task' : e.backpage
			this.backtype = e.backtype == undefined ? 1 : e.backtype
		},
		methods: {
			...mapMutations(['login']),

			/* 账号登录 */
			userLogin() {
				if (!(/^1[3456789]\d{9}$/.test(this.phone))) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					})
					return false;
				}
				if (this.password == '') {
					uni.showToast({
						title: '请输入密码',
						icon: 'none'
					})
					return false;
				}
				let params = {
					phone: this.phone,
					password: this.password,
					ip: ''
				}
				let that = this
				that.request("get","rider/wxLogin/loginRider",null,params).then(res => {
					if (res.code === 200) {
						that.login(res.data);
						uni.showToast({
							title: '登录成功',
							icon: 'none',
							duration: 1500
						})
						setTimeout(function() {
							that.loginBack(that.backpage, that.backtype)
						}, 1500)
					}
				})
			},

			/* 获取微信用户信息 */
			getUserProfile() {
				// #ifdef MP-WEIXIN
				let that = this;
				if(that.wxLoginInfo.openid) {
					// 已登录，但未获取手机号
					that.showPopup = true;
				}
				else if(uni.getUserProfile){
					// 未登录，且版本支持 uni.getUserProfile
					uni.getUserProfile({
						lang: 'zh_CN',
						desc: "获取您的昵称、头像、手机号",
						success: async(res) => {
							console.log('用户同意了授权', res)
							that.wxUserInfo = res.userInfo;  // 临时保存微信用户基础数据
							that.mpWeixinTologin(); // 微信一键登录
						},
						fail:(err) => {
							console.log('授权失败:', err)
							uni.showToast({ title: err })
						}
					})
				} else {
					uni.showToast({ title: '当前版本过低' })
				}
				// #endif
			},

			/* 微信登录 */
			 mpWeixinTologin() {
				let that = this;
				uni.login({
					provider: 'weixin',
					async success(res) {
						console.log('微信一键登录', res);
						if (res.errMsg == 'login:ok') {
							that.wxLoginInfo.code = res.code;
							that.serverLogin(1);
						}
					},
					fail(err) {
						console.log('uni.login', err);
					}
				});
			},

			/* 获取手机号 */
			getPhoneNumber(e) {
				console.log('获取手机号', e);
				let that = this;
				if(!that.wxLoginInfo.openid || !e.detail.encryptedData){
					return false;	// 未登录
				}
				let content = {
					openid: that.wxLoginInfo.openid,
					sessionkey: that.wxLoginInfo.session_key,
					encryptedData: e.detail.encryptedData,
					iv: e.detail.iv,
				}
        let params = {
          raw: JSON.stringify(content)
        };
        that.request("get","rider/wxLogin/decryptS5",null,params).then(resolve => {
					//console.log('获取解密手机号', resolve)
					if (resolve.code === 200) {
						let content = {
							code: mpres.code,
							nickName: that.wxUserInfo.nickName,
							avatarUrl: that.wxUserInfo.avatarUrl
						};
						that.serverLogin(2);
					} else{
						uni.showToast({ title: resolve.msg })
					}
				})
			},

			/* 后台登录 */
			serverLogin(type){
				// type: 1-第一次后台登录(返回无token) 2-第一次后台登录(返回带token)
				let that = this;
        let content = {
          code: that.wxLoginInfo.code,
          phone: that.wxUserInfo.phone,
          nickName: that.wxUserInfo.nickName,
          avatarUrl: that.wxUserInfo.avatarUrl,
          openid : that.wxLoginInfo.openid,
          session_key : that.wxLoginInfo.session_key,
        };
        that.request("post","rider/wxLogin/wechatLogin",null,content).then(res => {
					console.log('获取数据库返回的用户信息', res);
					if (res.code === 200) {
						// 至此实际已登录
						that.wxLoginInfo.openid = res.data.openId;
						that.wxLoginInfo.session_key = res.data.sessionKey;
						// 保存数据库返回的用户信息

						if(type === 1){
							// 返回无token, 另需获取手机号绑定对应的openid
							that.showPopup = true; // 显示获取手机号按钮
						}
						else {
							// 返回带token, 进入订单页
							let duration = 1500;  // 延迟跳转页面
							that.login(res.data); // 缓存用户数据token
							uni.showToast({ title: '登录成功', duration })
							setTimeout(() => {
								that.loginBack(that.backpage, that.backtype)
							}, duration)
						}
					}
					else{
						uni.showToast({ title: res.msg })
					}
				})
			}

		}
	}
</script>

<style lang="less" scoped>
	.login-box{
		position: relative;
		width: 100vw;
		height: 100vh;
	}

	.login-title {
		text-align: center;
		padding-top: 74rpx;
		font-size: 36rpx;
		color: #333333;
		font-weight: bold;
	}

	.logo {
		text-align: center;
		padding-top: 72rpx;
		image {
			width: 172rpx;
			height: 172rpx;
		}
	}

	.desc {
		text-align: center;
		padding-top: 48rpx;
		image {
			width: 284rpx;
			height: 46rpx;
		}
	}

	.form {
		width: 618rpx;
		margin: 0 auto;
		padding-top: 32rpx;
		.form_item {
			height: 100rpx;
			border-bottom: 2rpx solid #EFEFEF;
			position: relative;
		}

		.img1 {
			width: 36rpx;
			height: 40rpx;
			position: absolute;
			left: 26rpx;
			bottom: 28rpx;
		}

		.inp1 {
			width: 524rpx;
			display: inline-block;
			position: absolute;
			bottom: 0;
			height: 98rpx;
			left: 96rpx;
			/* color: #BBBBBB; */
			font-size: 30rpx;
			line-height: 98rpx;
		}

		.pl {
			color: #BBBBBB;
			font-size: 30rpx;
		}
	}

	.btn {
		width: 640rpx;
		height: 96rpx;
		line-height: 96rpx;
		color: #FFFFFF;
		background: linear-gradient(to right, #5095F4, #3662DD);
		font-size: 32rpx;
		border-radius: 8rpx;
		margin: 0 auto;
	}

	.other-login{
		position: relative;
		z-index: 5;
		width: 618rpx;
		margin: 80rpx auto 0;
		.ol-tilte{
			display: flex;

			align-items: center;
			justify-content: center;
			&:before,
			&:after{
				display: block;
				content: '';
				width: 30%;
				border-bottom: 2rpx solid #EFEFEF;
			}
			text{
				color: #666;
				font-size: 26rpx;
				padding: 0 20rpx;
			}
		}
		.wx-login{
			width: 88rpx;
			height: 88rpx;
			margin: 40rpx auto 0;
			image {
				display: block;
				width: 100%;
				height: 100%;
			}
		}
	}

	.bottom_img {
		position: absolute;
		left: 0;
		bottom: 0;
		display: block;
		width: 100%;
	}

	.popup-box{
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		z-index: 10;
		background-color: rgba(0,0,0,.5);
	}

	.login-popup{
		position: absolute;
		left: 0;
		bottom: 0;
		width: 100%;
		z-index: 10;
		background-color: #fff;
		padding-bottom: 40rpx;
		.lp-top{
			display: flex;
			align-items: center;
			color: #000;
			line-height: 1.2;
			.lpt-left{
				flex: 1;
				padding: 30rpx;
				.title{
					font-size: 28rpx;
					font-weight: 500;
				}
				.tips{
					color: #9a9b9b;
					font-size: 18rpx;
					margin-top: 10rpx;
				}
			}
			.lpt-right{
				padding: 30rpx;
				image{
					display: block;
					width: 28rpx;
					height: 28rpx;
				}
			}
		}
		.lp-center{
			position: relative;
			padding: 10rpx 30rpx 40rpx;
			color: #000;
			&:after{
				position: absolute;
				bottom: 0;
				left: 30rpx;
				right: 30rpx;
				content: '';
				border-bottom: 1rpx solid #ededed;
			}
			.title{
				font-size: 28rpx;
				font-weight: 600;
			}
			.tips{
				font-size: 26rpx;
				margin-top: 10rpx;
			}
		}
		.lp-bottom{
			padding: 0 30rpx;
			margin-top: 40rpx;
			.option-btn{
				height: 86rpx;
				color: #fff;
				font-size: 22rpx;
				text-align: center;
				line-height: 86rpx;
				padding: 0 30rpx;
				background: linear-gradient(to right, #5095F4, #3662DD);
			}
		}
	}

</style>
