import Vue from 'vue'
import Router from 'vue-router'
import { MATERIALMenu } from "../config/menu";

Vue.use(Router)

const originalPush = Router.prototype.push
Router.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

let routes = [];
MATERIALMenu.forEach(item => {
  routes.push({
    name: item.name,
    path: `${item.link}`,
    meta: {
      belong: item.belong
    },
    component: () => import(`../views/material/${item.name}/index.vue`)
  });
});

const router = new Router({
  routes: routes
})


router.beforeEach(async (to, from, next) => {
  if (to.path === '/login') {
    next();
  } else {
    let token = localStorage.getItem('token');
 
    if (token === null || token === '') {
      next('/login');
    } else {
      next();
    }
  }

  if (to.path == '/') {
      return next({path: "/works" })
  }
  next()
})




export default router;
