import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    redirect: '/commit',
  },
  {
    path: '/screen',
    name: 'screen',
    meta: {
      title: '大屏'
    },
    component: () => import('@/views/screen')
  },
  {
    path: '/login',
    name: 'login',
    meta: {
      title: '登录'
    },
    component: () => import('@/views/login')
  },
  {
    path: '/success',
    name: 'success',
    meta: {
      title: '操作成功'
    },
    component: () => import('@/views/success')
  },
  {
    path: '/opt',
    name: 'opt',
    meta: {
      title: '运维'
    },
    component: () => import('@/views/setTopBoxManage/opt')
  },
  {
    path: '/commit',
    name: 'commit',
    component: () => import('@/views/user/commit.vue')
  },
  {
    path: '/learn',
    name: 'learn',
    component: () => import('@/views/user/learn.vue')
  },
  {
    path: '/admin',
    name: 'admin',
    component: () => import('@/views/user/admin.vue')
  },
  {
    path: '/editPsd',
    name: 'editPsd',
    component: () => import('@/views/user/editPsd.vue')
  },
  {
    path: '/learnContent',
    name: 'learnContent',
    component: () => import('@/views/user/components/learnContent.vue')
  },
  {
    path: '/learnDetail',
    name: 'learnDetail',
    component: () => import('@/views/user/components/learnDetail.vue')
  },
  {
    path: '/adminInfo',
    name: 'adminInfo',
    component: () => import('@/views/user/components/adminInfo.vue')
  },
]

function getAbsolutePath () {
  let path = location.pathname
  return path.substring(0, path.lastIndexOf('/') + 1)
} 
const router = new VueRouter({
  routes: routes,
  mode: "history",
  base: getAbsolutePath()
})
const whiteList =['/login','/success','/screen']
router.beforeEach((to, from, next) => {
  let user = sessionStorage.getItem("token");
  if (!user && !whiteList.includes(to.path)) {  // 通过vuex state获取当前的token是否存在
    next({
      path: '/login',
    })
  }else {
    next();
  }

})
export default router
