index.js 2 KB
Newer Older
乐宝呗666's avatar
乐宝呗666 committed
1 2 3
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
xulili's avatar
xulili committed
4
const routes = [
乐宝呗666's avatar
乐宝呗666 committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
  {
    path: '/',
    redirect: '/commit',
  },
  {
    path: '/screen',
    name: 'screen',
    meta: {
      title: '大屏'
    },
    component: () => import('@/views/screen')
  },
  {
    path: '/login',
    name: 'login',
    meta: {
      title: '登录'
    },
乐宝呗666's avatar
乐宝呗666 committed
23
    component: () => import('@/views/login')
乐宝呗666's avatar
乐宝呗666 committed
24
  },
乐宝呗666's avatar
乐宝呗666 committed
25 26 27 28 29 30
  {
    path: '/success',
    name: 'success',
    meta: {
      title: '操作成功'
    },
乐宝呗666's avatar
乐宝呗666 committed
31
    component: () => import('@/views/success')
乐宝呗666's avatar
乐宝呗666 committed
32
  },
乐宝呗666's avatar
乐宝呗666 committed
33 34 35 36 37 38 39 40 41 42 43
  {
    path: '/opt',
    name: 'opt',
    meta: {
      title: '运维'
    },
    component: () => import('@/views/setTopBoxManage/opt')
  },
  {
    path: '/commit',
    name: 'commit',
xulili's avatar
xulili committed
44 45 46
    component: () => import('@/views/user/commit.vue')
  },
  {
乐宝呗666's avatar
乐宝呗666 committed
47 48 49
    path: '/learn',
    name: 'learn',
    component: () => import('@/views/user/learn.vue')
xulili's avatar
xulili committed
50 51
  },
  {
乐宝呗666's avatar
乐宝呗666 committed
52 53 54
    path: '/admin',
    name: 'admin',
    component: () => import('@/views/user/admin.vue')
xulili's avatar
xulili committed
55 56
  },
  {
乐宝呗666's avatar
乐宝呗666 committed
57 58 59
    path: '/editPsd',
    name: 'editPsd',
    component: () => import('@/views/user/editPsd.vue')
xulili's avatar
xulili committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  },
  {
    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')
  },
乐宝呗666's avatar
乐宝呗666 committed
76 77
]

乐宝呗666's avatar
乐宝呗666 committed
78 79 80 81
function getAbsolutePath () {
  let path = location.pathname
  return path.substring(0, path.lastIndexOf('/') + 1)
} 
乐宝呗666's avatar
乐宝呗666 committed
82 83
const router = new VueRouter({
  routes: routes,
乐宝呗666's avatar
乐宝呗666 committed
84 85
  mode: "history",
  base: getAbsolutePath()
乐宝呗666's avatar
乐宝呗666 committed
86
})
87
const whiteList =['/login','/success','/screen']
乐宝呗666's avatar
乐宝呗666 committed
88 89
router.beforeEach((to, from, next) => {
  let user = localStorage.getItem("token");
90
  if (!user && !whiteList.includes(to.path)) {  // 通过vuex state获取当前的token是否存在
乐宝呗666's avatar
乐宝呗666 committed
91 92 93 94 95 96
    next({
      path: '/login',
    })
  }else {
    next();
  }
xulili's avatar
xulili committed
97

乐宝呗666's avatar
乐宝呗666 committed
98
})
xulili's avatar
xulili committed
99
export default router