import Vue from 'vue'
import Router from 'vue-router'
import Cookies from 'js-cookie';
import store from '../vuex'

Vue.use(Router)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: (resolve) => require(['@/views/login/index'], resolve),
    meta: {
      'title': '登录'
    }
  },
  {
    path: '/404',
    name: '404',
    component: (resolve) => require(['@/views/errorPage/403'], resolve),
    meta: {
      'title': '无权限'
    }
  },
  {
    path: '/main',
    name: 'main',
    component: (resolve) => require(['@/views/main/index'], resolve),
    meta: {
      'title': '数据大屏'
    }
  },
  {
    path: '/command',
    name: 'command',
    component: (resolve) => require(['@/views/command/index'], resolve),
    meta: {
      'title': '指挥大屏'
    }
  }
]
const router = new Router({
  mode: 'history',
  routes
})

router.onError((error) => {
  router.push({path: '403'})
})

router.beforeEach(async (to, from, next) => {
  // 登录界面登录成功之后，会把用户信息保存在会话
  // 存在时间为会话生命周期，页面关闭即失效。
  let token = Cookies.get(location.host + '-token')
  next()
})

export default router;

