import router from "./router";
import { getCookie } from "@/utils/auth"; // getToken from cookie
const whiteList = ["/login", "/logout", '/check']; // 不重定向白名单
import store from "@/store";

router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title} | 发电能力分析平台`;
    const token = getCookie("accessToken");
    //   const role = sessionStorage.getItem("ms_chinesename");
    if (token) {
        if (to.path == "/login") {
            next("/");
        } else {
            if (store.getters.roles.length == 0) {
                store.dispatch("user/GetInfo").then(async () => {
                    next();
                });
            } else {
                next();
            }
        }
    } else if (whiteList.indexOf(to.path) !== -1) {
        next();
    } else {
        next(`/login?redirect=${to.fullPath}`);
    }
});

router.afterEach((to, from, next) => {});