<template>
    <el-main></el-main>
</template>
<script setup lang="ts">
//useRouter
import { useRoute, useRouter } from "vue-router";
// import { useMsg } from "@/hooks/useMsg";
//重定向
import { usePathUrl } from "@/hooks/usePathUrl";
//登录请求接口
import { loginApi } from "@/api/modules/login";

//用户信息存储
import { useUserStore } from "@/stores/modules/user";

const userStore = useUserStore();
//路由;
const $route = useRoute();
const $router = useRouter();

// 登录
const loginHttp = async (code: any) => {
    const result: Record<string, any> = await loginApi(code);
    if (result.code === 0) {
        userStore.setToken(result?.data?.access_token);
        console.log(result?.data?.access_token, "=data.access_token=");
        // 设置用户信息
        userStore.setUserInfo(result?.data?.user_data);
        //设置好了token和用户信息跳转到首页
        setTimeout(() => {
            $router.push({ path: "/" });
        }, 500);
    } else {
        location.href = usePathUrl();
    }
};

// 登录前的判断
const login = () => {
    const { code } = $route.query;
    // 没有code直接跳转到登录页
    if (!code) {
        location.href = usePathUrl();
        return;
    }
    // 有code就登录请求
    loginHttp(code);
};
login();
</script>
<style lang="scss">
.el-main {
    height: 100vh;
}
</style>
