import { useMsg } from "@/hooks/useMsg";
import { useUserStore } from "@/stores/modules/user";
import { usePathUrl } from "@/hooks/usePathUrl";
//下线通知,但是这个下线通知有问题,当401的时候token已经失效,这里时候拿的token是失效的了,即使跳转了也会提示账号处于登录状态.
export const useUcOffline = () => {
    const userStore = useUserStore();
    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(() => {
            useMsg("success", "退出登录成功 ！");
            setTimeout(() => {
                //根据不同的环境跳转到不同环境的单点系统
                location.href = usePathUrl();
                // 2.重置用户数据
                userStore.$reset();
                // 3.清除 本地
                localStorage.clear();
            }, 800);
        })
        .catch(() => {
            location.href = usePathUrl();
            // 2.重置用户数据
            userStore.$reset();
            // 3.清除 本地
            localStorage.clear();
            useMsg("error", "退出登录失败,请稍后再试 ！");
        });
};
