<template>
	<view class='container'>
		<view class="flex-column" style="width: 100%;">
			<view class="flex h-center">
				<image class="w-240 h-240" src="@/static/icon_logo.png"></image>
			</view>

			<view class="mb-100 mlr-50" style="margin-top: 300rpx;">
				<button v-if="agreement" class="fullscreen-btn" style="border-radius: 100rpx;"
					open-type="getPhoneNumber" @getphonenumber="onGetphonenumber">
					<text>立即登录</text>
				</button>
				<view v-else @click="$u.toast('请同意用户协议与隐私政策')" class="fullscreen-btn"
					style="background: #999;border-radius: 100rpx;">
					立即登录
				</view>
			</view>

			<view class="flex vh-center ">
				<image class="w-40 h-40" v-if="!agreement" @click="agreement = !agreement" src="@/static/select.png">
				</image>
				<image class="w-40 h-40" v-else @click="agreement = !agreement" src="@/static/is-select.png"></image>
				<text class="ml-10">阅读并同意<text @click="onGetXieyi(1)" style="color: #4083F0;">《用户协议》</text>和<text
						@click="onGetXieyi(2)" style="color: #4083F0;">《隐私政策》</text></text>
			</view>
		</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 {
		display: flex;
		flex-direction: column;
		height: 100%;
		align-items: center;
		justify-content: center;
	}
</style>