<template>
	<view class="container">
		<img src="../../static/index/logo.png" />
		<view class="msg">靖安乡村民宿</view>
		<view class="btn" @click="handleLogin">授权登录</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			code: '',
			encryptedData: '',
			iv: ''
		}
	},
	methods: {
		// 授权登录按钮回调
		handleLogin() {
			// 获取code
			uni.login({
				//使用微信登录
				provider: 'weixin',
				success: (loginRes) => {
					this.code = loginRes.code
					// 获取用户加密信息
					uni.getUserInfo({
						provider: 'weixin',
						lang: 'zh_CN',
						success: (res) => {
							console.log(res)
							this.encryptedData = res.encryptedData
							this.iv = res.iv
							// 发送请求到后端解密用户信息
							this.loginReq()
						}
					})
				}
			})
		},

		// 解密用户信息请求
		async loginReq() {
			const res = await this.$myRequest({
				url: '/mhotel2/appcode.action',
				method: 'post',
				data: {
					code: this.code,
					encryptedData: this.encryptedData,
					iv: this.iv
				}
			})
			console.log(res)
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	display: flex;
	flex-direction: column;
	align-items: center;
	height: 100vh;
	background-color: #fff;

	img {
		margin-top: 116rpx;
		width: 138rpx;
		height: 138rpx;
	}

	.msg {
		margin-top: 32rpx;
		font-size: 32rpx;
		font-weight: bold;
	}

	.btn {
		position: absolute;
		// left: 20rpx;
		bottom: 74rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		width: 710rpx;
		height: 96rpx;
		color: #fff;
		font-size: 32rpx;
		border-radius: 64rpx;
		background-color: #096562;
	}
}
</style>
