<template>
	<view class="page-bg container">
		<view class="back-container">
			<navigator url="/pages/index/index">
				<uni-icons type="back" size="40" color="#FFFFFF"></uni-icons>
			</navigator>
		</view>
		<view class="login-container">
			<view class="avator-container">
				<uni-icons type="person" size="130" color="#FFFFFF"></uni-icons>
			</view>
			<view class="btn-container" @click="login">登录</view>
		</view>
	</view>
</template>

<script setup>
	import { ref } from 'vue';
	import { loginApi } from '@/service/apis.js';
	/**
	 * 头像
	 */
	const avatar = ref("");
	
	/**
	 * 昵称
	 */
	const nickname = ref("");
	
	const login = async () => {
		// 获取供应商
		let providerResult = await uni.getProvider({
			service: 'oauth'
		});
		const provider = providerResult.provider[0];
		
		// 获取登录的code
		let loginResult = await uni.login({
			provider
		});
		
		// 获取用户的头像和昵称
		let userInfoResult = await uni.getUserInfo({
			 provider
		});
		
		// 获取后端token
		let token = await loginApi.login({
			"code": loginResult.code,
			"avatar": userInfoResult.userInfo.avatarUrl,
			"nickname": userInfoResult.userInfo.nickName
		});
		
		// 保存token
		uni.setStorageSync('token', token);
		
		// 登录结束返回主页
		uni.navigateTo({
			url: "/pages/index/index"
		})
		
	}
</script>

<style lang="scss" scoped>
.container {
	width: 100vw;
	height: 100vh;
	padding: 16rpx;
	
	.back-container {
		left: 16rpx;
		top: 39px;
		width: 90rpx;
		height: 90rpx;
		border-radius: 50%;
		background-color: rgba(0, 0, 0, 0.1);
		
		display: flex;
		justify-content: center;
		align-items: center;
	}
	
	.login-container {
		width: 100%;
		height: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		
		.avator-container {
			left: 222rpx;
			top: 254rpx;
			width: 307rpx;
			height: 307rpx;
			background: #D2EFF3;
			border-radius: 50%;
			box-shadow: 0px 2px 2px 0px #DBDBDB;
		
			display: flex;
			justify-content: center;
			align-items: center;
		}
		
		.btn-container {
			margin-top: 57rpx;
			
			width: 500rpx;
			height: 100rpx;
			border-radius: 20rpx;
			background: #2A82E4;
		
			font-size: 48rpx;
			font-weight: 700;
			letter-spacing: 0rpx;
			line-height: 69.5rpx;
			color: #FFFFFF;
			
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}
}	       
</style>
