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

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

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

//通知单点上线
const getUcOnline = (phpToken: any, refreshToken: any) => {
    let httpUrl = import.meta.env.VITE_SINGLE_URL + "uc/online";
    fetch(httpUrl, {
        method: "POST",
        credentials: "include",
        keepalive: true,
        headers: {
            Authorization: phpToken,
            "Refresh-Authorization": refreshToken
        }
    });
};
//通知单点下线
// const getUcOffline = (redirectUrl: any) => {
//     let httpUrl = import.meta.env.VITE_SINGLE_URL + "uc/offline";
//     fetch(httpUrl, {
//         method: "POST",
//         credentials: "include",
//         keepalive: true,
//         headers: {
//             Authorization: userStore.phpToken,
//             "Refresh-Authorization": userStore.refreshToken
//         }
//     })
//         .then((res: any) => {
//             if (res.status === 200) {
//                 useMsg("success", "退出登录成功 ！");
//                 setTimeout(() => {
//                     location.href = redirectUrl;
//                 }, 300);
//             }
//         })
//         .catch(() => {
//             useMsg("error", "退出登录失败,请稍后再试 ！");
//         });
// };
//组织接口
const getOrgs = async () => {
    const relust = await getOrgsApi();
    if (relust.status === 200) {
        const { data } = relust;
        userStore.setOrgIdArr(data);
    }
};
//拼接token
const tokenSplicing = (data: any) => {
    // 组装token
    let newUserToken = data.tokenInfo.tokenType + " " + data.tokenInfo.token;
    let RefreshToken = data.tokenInfo.tokenType + " " + data.tokenInfo.refreshToken;
    let phpToken = data.tokenInfo.tokenType + " " + data.tokenInfo.phpToken;
    return {
        newUserToken,
        RefreshToken,
        phpToken
    };
};
// 设置用户数据
const setUserData = (data: any) => {
    const tokens = tokenSplicing(data);
    const { newUserToken, RefreshToken, phpToken } = tokens;
    // 设置token
    userStore.setToken(newUserToken);
    userStore.setRefreshToken(RefreshToken);
    userStore.setPhpToken(phpToken);
    // 设置用户信息
    userStore.setUserInfo(data.userInfo);
    // 设置组织id
    userStore.setOrgId(data.userInfo.orgId);
    //上线通知
    getUcOnline(phpToken, RefreshToken);

    //跳转到首页
    setTimeout(() => {
        $router.push({ path: "/" });
    }, 500);
    //获取组织
    getOrgs();
};

// 登录
const loginHttp = async (code: any) => {
    const result: Record<string, any> = await loginApi({ code: code });
    if (result.status === 200) {
        setUserData(result.data);
    } else {
        location.href = usePathUrl();
        // getUcOffline(usePathUrl());
    }
};

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