import Vue from 'vue'
import Router from 'vue-router'
import login from '../views/login'
import register from '../views/register/register'
import manager from '../views/manager/index'
import menu from '../views/system/menu'
import stuClass from '../views/system/stuClass'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '',         // 访问的地址
      redirect:'/login', // 重定向
      component: login   // 组件名称
    },
    {
      path: '/login',
      name: 'login',
      component: login
    },
    {
      path: '/register',
      name: 'register',
      component: register
    },
    {
      path: '/manager',
      name: 'manager',
      component: manager
    },
    {
      path: '/system/menu',
      name: 'menu',
      component: menu
    },
    {
      path: '/system/stuClass',
      name: 'stuClass',
      component: stuClass
    }
  ]
});

// 导航守卫
// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next();
  } else {
    let token = localStorage.getItem('Authorization')
    if (token === 'null' || token === '') {
      next('/login');
    } else {
      next();
    }
  }
});

export default router;
