import Vue from "vue";
import VueRouter from "vue-router";

// 路由数据
import { menuRouter, constantRouterMap } from "./routes";

Vue.use(VueRouter);

// 导出路由 在 main.js 里使用
const router = new VueRouter({
    // mode: 'history',
    routes: menuRouter,

});

//不拦截静态路由
function filterConstantRouterMap(toRoute) {
    // console.log("toRoute: ", toRoute);
    // console.log("constantRouterMap: ", constantRouterMap);
    let result = false;
    // 13985170102
    // if (toRoute.path === "/" || toRoute.path === "/home") return true;
    constantRouterMap.forEach((item) => {
        // console.log("filter",item.path , toRoute)
        console.log('item', item)
        if (item.path == toRoute.path) {
            result = true;
        }
    });
    if(!result && toRoute.path.indexOf("/admin") == -1) {
        result = true;
    }
    return result;
}

/**
 * 路由拦截
 * 权限验证
 */
router.beforeEach((to, from, next) => {
    let userInfo = localStorage.getItem("mh_userInfo");
    if (filterConstantRouterMap(to)) {
        next();
    } else {
        if (userInfo && userInfo != "") {
            next();
        } else {
            next("/login");
        }
    }
});

router.afterEach(to => {
    // 需要的信息
    const app = router.app;
    const {name, params, query} = to;
    // 多页控制 打开新的页面
    //app.$store.commit('d2admin/page/open', { name, params, query })
    // 更改标题
    //util.title(to.meta.title)
});

export default router;
