<template>
	<view class='container'>
		<view class="center">
			<image class="w-240 h-240" src="@/static/icon_logo.png"></image>
		</view>

		<view class="login-btn">
			<button v-if="agreement" class="lh-90 br-8" style="height: 100%;background-color: #1FCA7C;color: #FFF;"
				open-type="getPhoneNumber" @getphonenumber="onGetphonenumber">
				<text>立即登录</text>
			</button>
			<view v-else @click="$u.toast('请同意用户协议与隐私政策')" class="lh-90"
				style="height: 100%;background-color: #ccc;border-radius: 8rpx;">
				<text class="fs-30">立即登录</text>
			</view>
			<!-- <view @click="youkeLogin" class="lh-90 br-8 mt-30" style="height: 100%;background-color: #31AD6F;">
				<text>游客登录</text>
			</view> -->
		</view>

		<view class="agreement">
			<image v-if="!agreement" @click="agreement = !agreement" src="@/static/select.png"></image>
			<image v-else @click="agreement = !agreement" src="@/static/is-select.png"></image>
			<text>阅读并同意<text @click="onGetXieyi(1)" style="color: #4083F0;">《用户协议》</text>和<text @click="onGetXieyi(2)"
					style="color: #4083F0;">《隐私政策》</text></text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				agreement: false,
				code: '',
			};
		},
		onLoad(options) {
			const token = uni.getStorageSync('token')
			if (token) {
				this.$wskj.switchTab('/pages/wy-home');
			}

		},
		onShow() {

		},
		methods: {
			onGetphonenumber(e) {
				// 获取微信授权code
				const than = this
				uni.login({
					provider: 'weixin',
					success: (loginRes) => {
						than.code = loginRes.code
						// 获取用户信息
						uni.getUserInfo({
							provider: 'weixin',
							success: function(infoRes) {
								console.log("微信授权信息", loginRes)
								console.log("微信用户信息", infoRes);
								console.log("手机号授权信息", e)
								if (e.detail.iv && e.detail.encryptedData) {
									const params = {
										url: '/api/login/mobile',
										loadingTip: '登录中...',
										data: {
											payCode: than.code,
											loginCode: e.detail.code,
											encryptedData: infoRes.encryptedData,
											iv: infoRes.iv,
										}
									}
									than.$request(params).then(res => {
										console.log(res)
										if (res.code === 200) {
											uni.setStorageSync('token', res.data.token)
											than.$wskj.switchTab('/pages/wy-home');
										} else {
											than.$u.toast(res.msg || '登录失败')
										}
									})
								} else {
									than.$u.toast('手机号授权失败')
								}
							}
						});
					}
				});

			},
			youkeLogin() {
				if (!this.agreement) {
					return this.$u.toast('请同意用户协议与隐私政策')
				}
				uni.switchTab({
					url: '/pages/home'
				})
			},
			onGetXieyi(type) {
				const params = {
					url: '/api/protocol/' + type,
					loadingTip: '',
					method: 'GET',
					data: {}
				}
				this.$request(params).then(res => {
					let title = type == 1 ? '用户协议' : '隐私政策'
					this.$wskj.to('/pages/xieyi?title=' + title + '&content=' + res.data.content)
				})
			}
		}
	};
</script>

<style scoped>
	.container {
		height: 100%;
		padding: 100px 20rpx 0;
	}

	.login-btn {
		width: 94%;
		height: 90rpx;
		text-align: center;
		line-height: 90rpx;
		color: #FFF;
		position: absolute;
		bottom: 400rpx;
		left: 50%;
		margin-left: -47%;
	}


	.agreement {
		width: 100%;
		display: flex;
		align-items: center;
		font-size: 28rpx;
		position: fixed;
		bottom: 100rpx;
	}


	.agreement>image {
		width: 40rpx;
		height: 40rpx;
		margin-right: 20rpx;
	}
</style>