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) => {
//   if (getCookie("authToken")) {
//     if (to.path == "/login") {
//       next({ path: "/" });
//     } else {
//       if (store.getters.roles.length === 0) {
//         // 判断当前用户是否已拉取完user_info信息
//         store
//           .dispatch("user/GetInfo")
//           .then(async () => {
//             let permissions = store.getters.permissions;
//             let accessRoutes = [];
//             accessRoutes = await store.dispatch(
//               "routes/setRoutes",
//               permissions
//             );
//             accessRoutes?.forEach((item) => {
//               router.addRoute(item);
//             });
//             next({ ...to, replace: true });
//           })
//           .catch((err) => {});
//       } else {
//         next();
//       }
//     }
//   } else if (whiteList.indexOf(to.path) !== -1) {
//     next();
//   } else {
//     next(`/login?redirect=${to.fullPath}`);
//   }

// });
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) => {});