<template>
	<view class="content">
		<image class="logo" src="/static/logo.png"></image>
		<view class="getUserInfoModel">
			<!-- 隐私协议 -->
			<view class="privacyAgreement">
				<u-icon v-if="isAgree" @click="isAgree = false"
					name="http://amy.yyinhong.cn/uploads/wximg/loginxz.png"></u-icon>
				<u-icon v-if="!isAgree" color="#D3195E" @click="isAgree = true"
					name="http://amy.yyinhong.cn/uploads/wximg/loginc.png"></u-icon>
				<view class="privacyAgreementtext">阅读并同意<span @click.stop="previewWechat('https://amyapi.yyinhong.cn/uploads/yonghu.docx'
						)">《用户协议》</span>及<span @click.stop="previewWechat('https://amyapi.yyinhong.cn/uploads/yinsi.docx')">《隐私协议》</span>
				</view>
			</view>
			<u-toast ref="uToast"></u-toast>
			<!-- 授权登录按钮 -->
			<u-button type="primary" open-type="getPhoneNumber" @getphonenumber="getphonenumber" shape="circle"
				color="#D3195E" text="授权登录"></u-button>
			<u-gap height="20" bgColor="#fff"></u-gap>
			<u-button type="primary" :plain="true" open-type="getPhoneNumber" @click="goback" shape="circle"
				color="#D3195E" text="返回首页"></u-button>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: null,
				isAgree: false,
				globalTimer: null
			}
		},
		// 每次已进入页面就请求code，存放本地，过了有效期五分钟，重新请求code
		onShow() {
			this.wxlogin()
			// 微信登录code有效期 五分钟
			this.globalTimer = setInterval(() => {
				this.wxlogin()
			}, 299990)
		},
		// 离开页面 清除定时器
		onHide() {
			if (this.globalTimer) {
				clearInterval(this.globalTimer)
			}
		},
		methods: {
			goback() {
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
			// 请求code方法
			wxlogin() {
				console.log("执行login方法")
				uni.login({
					success: function(loginRes) {
						uni.setStorageSync('code', loginRes.code);
					}
				})
			},
			wx_login(encryptedData, iv) {
				this.$request('/login', 'POST', {
					encryptedData: encryptedData,
					iv: iv,
					code: uni.getStorageSync('code')
				}).then(res => {
					console.log(res.data)
					uni.setStorageSync('token', res.data.Authorization)
					var pageUrl = uni.getStorageSync('pageUrl');
					console.log(pageUrl)
					if (pageUrl == '/pages/login/login') {
						pageUrl = '/pages/index/index'
					}
					if (pageUrl) {
						// 如果为tabbar页面则用reLaunch跳转
						if (['/pages/personalCenter/personalCenter'].includes(pageUrl) || [
								'/pages/shoppingCart/shoppingCart'
							].includes(pageUrl) || ['/pages/index/index'].includes(pageUrl) || [
								'/pages/product/product'
							].includes(pageUrl)) {
							uni.switchTab({
								url: pageUrl
							})
						} else {
							uni.redirectTo({
								url: pageUrl,
								fail: function(err) {
									uni.switchTab({
										url: '/pages/index/index'
									})
								}
							})
						}
						//跳转后，删除url记录避免重复跳转
						uni.removeStorageSync('pageUrl');
					} else {
						// 如果没有默认跳转到首页
						uni.switchTab({
							url: '/pages/index/index'
						})
					}
				}).catch(err => {
					this.$refs.uToast.show({
						type: 'error',
						message: '' + err.message,
						iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/error.png'
					})
				})

			},
			getphonenumber(e) {
				console.log(e);
				if (!this.isAgree) {
					this.$refs.uToast.show({
						type: 'error',
						message: '请先勾选同意用户协议和隐私政策！',
						iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/error.png'
					})
					return;
				} else {
					if (e.detail.errMsg == 'getPhoneNumber:ok') {
						this.wx_login(e.detail.encryptedData, e.detail.iv);
					} else {
						this.wxlogin()
						this.$refs.uToast.show({
							type: 'error',
							message: '获取用户号码失败！',
							iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/error.png'
						})

					}
				}

			},
			previewWechat(urlPdf) {
				uni.showLoading({
					title: '正在加载中..'
				})
				uni.downloadFile({
					url: urlPdf,
					success: function(res) {
						var filePath = res.tempFilePath;
						uni.openDocument({
							filePath: filePath,
							showMenu: true,
							success: function(res) {
								console.log('打开文档成功');
								uni.hideLoading()
							},
						});
					},
					complete: function(r) {
						uni.hideLoading()
					}
				});
			}
		},
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		width: 296rpx;
		height: 136rpx;
		margin-top: 300rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 365rpx;
	}

	.getUserInfoModel {
		width: calc(100% - 100rpx);
		margin: 50rpx;
	}

	.privacyAgreement {
		display: flex;
		flex-direction: row;
		align-items: center;
		margin-bottom: 40rpx;
	}

	.privacyAgreementtext {
		font-size: 14px;
		text-indent: 10rpx;
	}

	::v-deep .u-button {
		height: 92rpx !important;
		line-height: 92rpx !important;
	}
</style>s