<template>
	<view>
		<view :style="'height:' + height + 'px'" class="d-flex flex-column j-center a-center">
			<image class="logo-img" src="../../static/logoLogin.png" >
			</image>
			<view class="distence-top">
				<button class="button" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">微信手机号授权登录</button>
			</view>
			
		</view>
			
			
	</view>
</template>

<script>
	import {login,loginPhone,loginUserInfo} from '@/api/user.js'
	export default {

		data() {
			return {
				height:0,
				loginParams:{
					xcxCode:''
				},
				token:'',
				InviterId:'',//邀请人的id
			}
		},
		onLoad(option) {
			console.log(option.id,'第三节')
			let that = this
			// 获取系统的高度
			uni.getSystemInfo({
				success(res) {
					that.height = res.windowHeight
				}
			})
			this.getUserInfo()
			if(option.id){
				this.InviterId = option.id
			}
		},
		methods: {
			// 1.调用微信自带登录方法获取code
			getUserInfo(){
				let that = this
				uni.login({
					success(res) {
						that.getSessionkeyAndOpenId(res.code)
					}
				})
			},
			
			// 2.获取sessionKey,openId
			async getSessionkeyAndOpenId(xcxCode) {
				let that = this
				let codeParams = {}
				if(that.InviterId){
					that.$set(codeParams,'xcxCode',xcxCode)
					that.$set(codeParams,'InviterId',that.InviterId)
				}else{
					that.$set(codeParams,'xcxCode',xcxCode)
				}
				const back_data = await login(codeParams)
				const {code,data} = back_data
				if(code === 200){
					uni.setStorageSync('token',data.token)
				}
			},
			
			// 3.授权手机号登录
			getPhoneNumber(e) {
				let that = this
				// 同意登录
				if (e.detail.errMsg == "getPhoneNumber:ok") {
					console.log(e.detail.code,'Dasnj ')
					that.getPhoneLogin(e.detail.code)
				} else {
					// 取消授权 清空头像和昵称
					uni.navigateBack({
						delta: 1
					})
				}
			},
			// 获取手机号
			async getPhoneLogin(xcxCode){
				let that = this
				const data_back = await loginPhone({xcxCode:xcxCode})
				const {code} = data_back
				if(code === 200){
					that.getUserMsg()
					
				}
			},
			async getUserMsg(){
				const data_back = await loginUserInfo()
				const {code,data} = data_back
				if(code === 200){
					uni.setStorageSync('mobile',data.user.mobile)
					this.$uniGo.reLaunch({
						url:'/pages/home/index'
					})
				}
			},
		}
	}
</script>

<style>
	page{
		padding:0;
	}
	.logo-img{
		width: 200rpx;
		height:200rpx;
		border-radius: 50%;
	}
	.button {
		color: #FFFFFF;
		height: 90rpx;
		font-size: 18px;
		text-align: center;
		line-height: 90rpx;
		width: 550rpx;
		border-radius: 8px;
		background: #3476ff;
	}
	.use-text{
		text-align: center;
		font-size: 24rpx;
		color: #3476ff;
		margin: 20rpx auto;
	}
	.distence-top{
		margin-top: 429rpx;
	}
</style>
