import { createRouter, createWebHashHistory } from "vue-router";
import { useUserStore } from "../stores/useUserStore";
import routes from "./routes.js";

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

//全局前置守卫
router.beforeEach((to) => {
    // showLoadingToast({message: t('Loading'), forbidClick: true, duration: 0})
    const userStore = useUserStore();
    if (to.meta.no_login) {
        // 无需登录的页面直接进入
        return true;
    } else if (!userStore.token) {
        // 未登录或登录失效
        return { path: "/login", replace: true };
    } else {
        // 已经登陆
        return true;
    }
});

router.afterEach(() => {
    // closeToast()
});

export default router;
