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

//用户信息存储
import { useUserStore } from "@/stores/modules/user";
//重定向
import { usePathUrl } from "@/hooks/usePathUrl";

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

// 设置用户数据
const setUserData = (data: any) => {
    // 设置token
    userStore.setToken(data.access_token);
    // 设置用户信息
    userStore.setUserInfo(data.user_data);

    //设置好了token和用户信息跳转到首页
    setTimeout(() => {
        $router.push({ path: "/" });
    }, 500);
};

// 登录
const loginHttp = async (code: any) => {
    const result: Record<string, any> = await loginApi(code);
    if (result.code === 0) {
        setUserData(result.data);
    } 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>
