<template>
	<view class="loginPage">
		<image class="bk" src="/static/img/ka.jpeg" mode=""></image>
		<view class="box">
			<view class="tip">
				<radio :checked="checkbox1" @click="checkbox1 = !checkbox1" />
				<view class="text">我已阅读并同意<span class="textColor1">《用户协议》</span>及 <span class="textColor1">《隐私政策》</span></view>
			</view>
			<button @click="wxLogin" class="m_button" type="default">微信授权登录</button>
		</view>
		
	</view>
</template>

<script>
	import {
		loginByWeixinApi
	} from "@/api/user.js"
	export default {
		data() {
			return {
				checkbox1: false, 
			}
		},
		methods: {
			wxLogin: function(e) {
				if (!this.checkbox1) {
					wx.showToast({
						title: '请先阅读并同意《用户协议》',
						icon: 'none',
					})
				}
				let _this = this
				wx.getUserProfile({
					desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
					success: (res) => {
						console.log(res, 'res111')
						_this.getWXCode(res.userInfo)
				
					},
					fail: () => {
						console.log('微信登录失败')
						// util.showErrorToast('微信登录失败');
					}
				})
			},
			getWXCode(userInfo) {
				let _this = this
				wx.login({
					success: function(res) {
						console.log(res, 'res222')
						if (res.code) {
							let params = {
								code: res.code,
								userInfo: userInfo
							}
							_this.executeLoginApi(params)
						}
					},
					fail: function(err) {
						reject(err);
					}
				});
			},
			executeLoginApi(params) {
				let _this = this
				console.log(222)
				let inviteId = getApp().globalData.inviteId
				if (inviteId) {
					console.log('有邀请id', inviteId)
					params.inviteId = inviteId
				}
				loginByWeixinApi(params).then(res => {
					console.log('调用登录api', res)
					//存储用户信息
					wx.setStorageSync('userInfo', res.data.userInfo);
					wx.setStorageSync('token', res.data.token);
					wx.setStorageSync('userId', res.data.userId);
					getApp().globalData.hasLogin = true;
					wx.navigateBack({
						delta: 1
					})
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.loginPage {
		height: 100vh;
		overflow: hidden;
		// background: url(@/static/img/ka.jpeg) no-repeat;
		// background-size: 100% 100%;
		.bk {
			height: 100%;
			width: 100%;
		}
		.box {
			position: absolute;
			bottom: 0;
			display: flex;
			align-items: center;
			justify-content: center;
			flex-direction: column;
			width: 100%;;
			background-color: rgba(255, 255, 255, 0.7);
			padding: 30px 10px;
			.tip {
				display: flex;
				justify-content: center;
				align-items: center;
				margin-bottom: 10px;
				color: #000;
				font-size: 12px;
				
				.checkBox {
					// font-size: 12px;
					height: 12px;
					width: 12px;
				}
			}
			.m_button {
				height: 40px;
				width: 50vw;
				border-radius: 20px;
				line-height: 40px;
				font-size: 16px;
			}
		}
	}
</style>
