<template>
	<view class="se-pos se-h-vh-100">
		<view class="se-flex se-pt-200 se-flex-h-c se-flex-ff-cw">
			<image class="se-w-150 se-h-150" :src="logo" mode=""></image>
			<view class="se-flex se-flex-h-c se-fs-34 se-fw-6 se-c-black se-mt-40">
				<!-- 租房小程序 -->
				{{name}}
			</view>
		</view>
		<view class="se-pb-200 se-pos-cb se-w-p-100">
			<button  @getuserinfo="onLogin" open-type="getUserInfo" class="se-mx-80  se-br-40 se-flex-h-c se-h-80 se-lh-80 se-ta-c se-fs-32 se-c-white se-bgc-green">
				<image class="se-w-45 se-h-35" src="@/static/image/wechat.png" mode=""></image>
				<text class="se-ml-10">微信登录</text>
			</button>
			<button  @getuserinfo="onLogin" 
			style="margin-top: 30rpx;background-color: rgba(#0f0, 0.4);color: #0f0;"
			class="se-mx-80  se-br-40 se-flex-h-c se-h-80 se-lh-80 se-ta-c se-fs-32">
				<text class="se-ml-10">取消登录</text>
			</button>
			<!-- <view class="se-mx-80 se-br-40 se-mt-40 se-flex-h-c se-h-80 se-lh-80 se-ta-c se-fs-32 se-c-66 se-bgc-f5">
				<text>手机验证码登录</text>
			</view> -->
			<view class="se-mx-80 se-br-40 se-mt-40 se-flex-h-c se-h-80 se-lh-80 se-ta-c se-fs-24 se-c-33">
				<uv-checkbox-group  @change="checkboxChange" v-model="checked">
					<uv-checkbox name="1" activeColor="#07C160" label-size="12" shape="circle" label="阅读并同意"></uv-checkbox>
					<text class="se-c-green" @click="open()">《隐私政策》</text>
					<text class="se-c-green" @click="open()">《服务条款》</text>
				</uv-checkbox-group>
			</view>
		</view>
		<uv-popup ref="popup" :round="20"  mode="center" bg-color="transparent" :close-on-click-overlay="false">
			<view class="se-w-600  se-bgc-white se-br-40 se-p-40">
				<view class="se-flex se-flex-h-c">
					<image class="se-a-80" src="@/static/image/6588.png" mode=""></image>
				</view>
				<view class="se-flex se-flex-h-c se-fs-34 se-fw-6 se-c-black se-mt-40">
					服务协议和隐私权政策
				</view>
				<view class="se-lh-40 se-mt-40 se-fs-24 se-c-99">
					<text>请您务必审慎阅读、充分理解用户协议和隐私政策各条款，包括但不限于用户注意事项、用户行为规范以及为了向你提供服务而收集、使用、存储你个人信息的情况等。你可阅读</text>
					<text class="se-c-black se-fw-6" @click="handleOpenPrivacyContract()">《{{userXyObj.keyValue}}》</text>
					<text>和</text>
					<text class="se-c-black se-fw-6" @click="handleOpenPrivacyContract()">《{{userYsObj.keyValue}}》</text>
					<text>了解详细信息。如你同意，请点击下方按钮开始接受我们的服务。</text>
				</view>
				<view class="se-flex se-flex-h-sb se-mt-40">
					<view @click="onCancel" class="se-br-20 se-flex-1 se-flex-h-c se-h-80 se-lh-80 se-ta-c se-fs-32 se-c-66 se-bgc-f5">
						<text>不同意</text>
					</view>
					<view @click="onAgree" class="se-br-20 se-ml-20 se-flex-1 se-flex-h-c se-h-80 se-lh-80 se-ta-c se-fs-32 se-c-white se-bgc-green">
						<text class="se-ml-10">同意</text>
					</view>
				</view>
			</view>
			</uv-popup>
	</view>
</template>

<script>
	import { fetchMenu,wxLogin,appletLogin,getInfo } from "@/common/api.js"
	export default {
		name : 'Auth',
		data() {
			return {
				logo:"../../static/image/logo.png",
				name:"租房小程序",
				checked:[],
				show:false,
				userXyObj:{},
				userYsObj:{}
			}
		},
		mounted() {
			this.userXyObj = this.$utils.getkeyContent('user_xy')
			this.userYsObj = this.$utils.getkeyContent('user_ys')
		},
		methods: {
			open(){
				this.$refs.popup.open();
			},
			checkboxChange(n) {
				console.log('change', n);
			},
			onCancel(){
				this.$refs.popup.close();
				this.checked=[];
			},
			onAgree(){
				this.$refs.popup.close();
				this.checked=['1'];
			},
			onLogin(event){
				console.info(event)
				
				let that = this
				if(that.checked.length==0){
					return uni.showToast({
						icon: "none",
						title: "请勾选隐私协议"
					})
				}
				uni.showLoading({
					title: '登录中...'
				})
				uni.login({
					success : res => {
						console.info(res.code)
						if(res.errMsg != "login:ok"){
							return
						}
						let params={
							code : res.code,
							encryptedData: event.detail.encryptedData,
							headimgurl:event.detail.userInfo.avatarUrl,
							iv:event.detail.iv,
							nickName:event.detail.userInfo.nickName,
							session_key:"",
							shareId:uni.getStorageSync('userId'),
							vid:""
						}
						appletLogin(params).then((response) => {
							console.info(`response`,response)
							uni.hideLoading()
							uni.setStorageSync('token', response.result.token)
							uni.setStorageSync('sessionKey', response.result.userInfo.sessionKey)
							uni.setStorageSync('userInfo', response.result.userInfo)
							
							let userInfo =response.result.userInfo
							
							if(!userInfo.nickName
							|| !userInfo.phone){
								uni.navigateTo({
									url:"/pages/login/index"
								})
							}else{
								uni.switchTab({
									url:"/pages/home/index"
								})
							}
							
						}).catch((error) =>{
							
						})
					}
				})
			},
			handleOpenPrivacyContract() {
				// 打开隐私协议页面
				wx.openPrivacyContract({
					success: () => {}, // 打开成功
					fail: () => {}, // 打开失败
					complete: () => {}
				})
			}
			
		}
	}
</script>

<style scoped lang="scss">
</style>
