<template>
	<view class="main">
		<view class="wx">
			<view class="wx_1">
				<text class="iconfont icon-weixin"></text>
			</view>
			<view class="wx_2">
				<button size="mini" @tap="otherLogin()">微信信任登录</button>
			</view>
			<view class="wx_3">
				<checkbox-group @change="changeAgree">
					<label>
						<checkbox :checked="agree" />
						<text @tap.stop="toAgree()">我已阅读并同意“用户协议”和“隐私政策”</text>
					</label>
				</checkbox-group>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				// 用户协议
				agree: false,
				// 注册账号信息
				user: {},
			};
		},
		onShow: function() {
			const that = this;
			// 查询平台信息
			that.searchOpenids();
		},
		methods: {
			async searchOpenids() {
				const that = this;
				uni.getStorage({
					key: 'openid',
					success: function(res) {
						that.$set(that, `openid`, res.data);
					},
					fail: function(err) {
						uni.login({
							success: async function(res) {
								if (res.code) {
									const aee = await that.$app('/wechat/api/login/app',
									'GET', {
										js_code: res.code,
										config: that.$config.wx_projectkey
									})
									if (aee.errcode == '0') {
										uni.setStorage({
											key: "openid",
											data: aee.data.openid
										})
										that.$set(that, `openid`, aee.data.openid);
									} else {
										uni.showToast({
											title: aee.errmsg,
											icon: 'none'
										})
									}
								} else {
									uni.showToast({
										title: res.errMsg,
										icon: 'none'
									})
								}
							}
						});
					}
				})
			},
			// 其他登录方式
			async otherLogin() {
				const that = this;
				let agree = that.agree;
				let openid = that.openid;
				if (agree) {
					if (openid) {
						const aee = await that.$api(`/login/wxapp/${openid}`, 'POST', {})
						if (aee.errcode == '0') {
							uni.reLaunch({
								url: `/pages/home/index`
							})
						} else {
							uni.showToast({
								title: aee.errmsg,
								icon: 'none'
							})
						}
					} else {
						uni.showToast({
							title: '系统更新中，请稍后再试!',
							icon: 'none'
						})
					}
				} else {
					uni.showToast({
						title: '请阅读并同意用户协议和隐私政策',
						icon: 'none'
					})
				}
			},
			// 同意隐私协议
			changeAgree() {
				const that = this;
				let agree = !that.agree
				that.$set(that, `agree`, agree);
			},
			// 查看隐私协议
			toAgree() {
				const that = this;
				uni.navigateTo({
					url: `/pagesHome/other/agree`
				})
			},
		},
	}
</script>

<style lang="scss" scoped>
	.main {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;

		.wx {
			text-align: center;
			margin: 25vw 0 0 0;

			.wx_1 {
				margin: 0 0 5vw 0;

				.iconfont {
					color: var(--f35BColor);
				}

				text {
					font-size: 50px;
				}
			}

			.wx_2 {
				button {
					background: var(--f35BColor);
					color: var(--fffColor);
					font-size: var(--font16Size);
				}
			}

			.wx_3 {
				position: absolute;
				bottom: 10vw;
				text-align: center;
				font-size: 12px;
			}
		}
	}
</style>