<template>
	<view class="login">
		<view class="login-logo">
			<image src="https://cdn.catmdogd.com/Work/image/user/logo.png"></image>
		</view>
		<view class="login-submit">
			<up-button type="primary" text="授权手机号登录" open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber"
				shape="circle" color="#FFBF60"></up-button>
			<up-button class="mt24" type="primary" text="微信授权登陆" @tap="loginWx" shape="circle"
				color="#FFBF60"></up-button>
		</view>
		<view class="flex-rowc">
			<view class="select">
				<image mode="widthFix"></image>
			</view>
			<view class="size-26">
				<text>我已阅读并同意猫爸狗妈</text>
				<text class="main-color">《注册协议》</text>
				<text>与</text>
				<text class="main-color">《隐私政策》</text>
			</view>
		</view>
	</view>
	<view class="footer size-30 flex-rowc w100">
		暂不登陆
	</view>
</template>

<script setup>
	import {
		getOpenIdKey,
		getToken,
		setIsLogin,
		setOpenIdKey,
		setStorage,
		setToken
	} from "../../utils/auth";
	import {
		getOpenId,
		getPersonalInfo,
		getPhoneNumber,
		wxLogin
	} from "../../api/system/user";
	import {
		ref
	} from "vue"

	const value = ref(false)


	// 微信授权登陆
	const loginWx = () => {
		uni.login({
			success: (res) => {
				const code = res.code
				console.log(code)
				wxLogin({
					code
				}).then(res => {
					if (res.code === 200) {
						uni.setStorageSync("token", res.data.token)
						uni.setStorageSync("baseInfo", JSON.stringify(res.data.userInfo))
						uni.navigateBack({
							delta: 1
						})
					}
				})
			}
		})
	}



	// 获取openid
	// if (!getOpenIdKey() && !getToken()) {
	// 	uni.login({
	// 		provider: 'weixin',
	// 		success: async (res) => {
	// 			const {
	// 				code,
	// 				data
	// 			} = await getOpenId(res.code)
	// 			if (code === 200 && data) {
	// 				let resData = JSON.parse(data)
	// 				setOpenIdKey(resData.openId)
	// 			}
	// 		},
	// 		fail: function(error) {
	// 			// 授权失败处理
	// 			uni.showToast('授权失败，请授权后再试')
	// 		}
	// 	});
	// }
	// 获取token
	const onGetPhoneNumber = async (e) => {
		// uni.login({
		// 	success: res => {
		// 		console.log("====>", res)
		// 	}
		// })
		console.log(e)
		getPhoneNumber({
			phoneCode: e.detail.code
		})
		// if (e.detail.errMsg === "getPhoneNumber:ok") {
		// 	if (getOpenIdKey()) {
		// 		try {
		// 			const {
		// 				data
		// 			} = await getPhoneNumber({
		// 				"openId": getOpenIdKey(),
		// 				"code": e.detail.code,
		// 				sourceType: 1
		// 			})
		// 			setToken(data.token)
		// 			setIsLogin(true)
		// 			await getUserInfo()
		// 		} catch (e) {
		// 			uni.showToast({
		// 				icon: 'error',
		// 				title: '获取手机号失败'
		// 			})
		// 		}
		// 	}
		// }
	}

	// // h获取用户信息
	// const getUserInfo = async () => {
	// 	const {
	// 		data
	// 	} = await getPersonalInfo()
	// 	setStorage('userInfo', data)
	// 	uni.navigateBack()
	// }
</script>

<style scoped lang="scss">
	@import "index";
	@import "@/style/index.scss";

	.select {
		width: 24rpx;
		height: 24rpx;
		border-radius: 50%;
		border: 1rpx solid #C7C7C7;
		margin-right: 8rpx;

		.active {
			background-color: $mainColor;
		}
	}

	.footer {
		color: #C7C7C7;
		position: fixed;
		bottom: 100rpx;

	}
</style>