<template>
	<div class="h-screen flex-c">
		<VanLoading type="spinner" size="30px" vertical>
			<div v-if="loading" class="fs-18 pt-10">登录中...</div>
			<div v-else>微信授权登录失败，没有获取到Code</div>
		</VanLoading>
	</div>
</template>

<script setup>
	import {
		onMounted,
		ref
	} from "vue"
	import {
		useRoute,
		useRouter
	} from "vue-router"
	import {
		loginAPI,
		LoadUserInfoAPI
	} from "../../services/wechart";
	import {
		getUrlParam
	} from "../../utils/wxrequest.js";
	import {
		setToken,
		getToken,
		removeToken,
		setUserinfo,
		getUserinfo,
		getReginfo,
		setReginfo,		
		setUsername,
		getUsername,
		setOpenid
	} from "../../utils/wxtools.js";
	// import { useUserStore } from "@/store/modules/user"
	import {
		Loading as VanLoading
	} from "vant"
	import {
		showNotify,
		showDialog
	} from "vant";
	// import { bindReferrer } from "@/api/user"

	// const userStore = useUserStore()

	const route = useRoute()
	const router = useRouter()
	const loading = ref(true)

	onMounted(async () => {

		console.log("token值：" + getToken());
		// const code = route.query.code;
		const code = getUrlParam('code');
		console.log(code);
		if (code) {
			try {
				// console.log("submit", values);
				const res = await loginAPI({
					code: code
				});
				console.log(res);
				if (res.code === 200) {
					setToken(res.result.token);
					// saveClientIdAPI(res.userinfo.)
					// alert("登录成功");
					showNotify({
						message: "登录成功",
						type: "success",
					});
					// router.push({
					// 	name: "Index",
					// });
					router.push("/");
				} else {
					// alert(res.data);
					showNotify({
						message: res.message,
						type: "danger",
					});
				}

				// const user = await LoadUserInfoAPI();
				// console.log(user.result);
				// setUserinfo(user.result);
				// setOpenid(user.result.openid);

				// await userStore.login(code)
				// userStore.getUserInfo().then((res) => {
				//   // 判断有uid绑定上级
				//   const uid = route.query.uid
				//   // console.log("获取到的uid :>> ", uid)
				//   if (uid && res.referrerId === null) {
				//     bindReferrer({ userid: uid }).then(() => {
				//       // console.log("上级绑定成功")
				//     })
				//   }
				//   // console.log("userInfo :>> ", res)
				//   const path = route.query.path
				//   router.push(path ? path : "/account")
				// })
			} catch (error) {
				console.log(error);
				loading.value = false
			}
		} else {
			removeToken(() => {
				router.push("/login")
			})
		}
	})
</script>

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