index.js 3.8 KB
Newer Older
yma16's avatar
yma16 已提交
1 2 3 4 5 6
import Vue from 'vue'

import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'
import { getToken } from '@/utils/auth'
import { isEmpty } from '@/utils'
yma16's avatar
yma16 已提交
7
import VueRouter from 'vue-router'
yma16's avatar
yma16 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// import Router from 'vue-router'
import store from '@/store'
// store
import Article from '@/components/Article'
import Login from '@/components/Login'
import Register from '@/components/Register'
import Onlinewebsocket from '@/components/Onlinewebsocket'
import Home from '@/components/Home'
import Bilicom from '@/components/Bilicom'
import Mavoneditor from '@/components/Mavoneditor'
import Loading from '@/components/Loading'

import GrilShow from '@/components/GrilShow'
import Csslearn from '@/components/Csslearn'

Vue.use(VueRouter)
yma16's avatar
yma16 已提交
24 25
const defaultRoutes = [
  {
yma16's avatar
yma16 已提交
26 27 28
    path: '/',
    name: 'Article',
    component: Article
yma16's avatar
yma16 已提交
29 30
  },
  {
yma16's avatar
yma16 已提交
31 32 33
    path: '/login',
    name: 'Login',
    component: Login
yma16's avatar
yma16 已提交
34 35
  },
  {
yma16's avatar
yma16 已提交
36 37 38
    path: '/register',
    name: 'Register',
    component: Register
yma16's avatar
yma16 已提交
39 40
  },
  {
yma16's avatar
yma16 已提交
41 42 43
    path: '/home',
    name: 'Home',
    component: Home
yma16's avatar
yma16 已提交
44 45
  },
  {
yma16's avatar
yma16 已提交
46 47 48
    path: '/onlinewebsocket',
    name: 'Onlinewebsocket',
    component: Onlinewebsocket
yma16's avatar
yma16 已提交
49 50
  },
  {
yma16's avatar
yma16 已提交
51 52 53
    path: '/bilicom',
    name: 'Bilicom',
    component: Bilicom
yma16's avatar
yma16 已提交
54 55
  },
  {
yma16's avatar
yma16 已提交
56 57 58
    path: '/mavoneditor',
    name: 'Mavoneditor',
    component: Mavoneditor
yma16's avatar
yma16 已提交
59 60
  },
  {
yma16's avatar
yma16 已提交
61 62 63
    path: '/loading',
    name: 'loading',
    component: Loading
yma16's avatar
yma16 已提交
64 65
  },
  {
yma16's avatar
yma16 已提交
66 67 68
    path: '/gril',
    name: 'grilshow',
    component: GrilShow
yma16's avatar
yma16 已提交
69 70
  },
  {
yma16's avatar
yma16 已提交
71 72 73
    path: '/css',
    name: 'css',
    component: Csslearn
yma16's avatar
yma16 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  }
  // {
  //     path: '/register',
  //     name: 'Register',
  //     component: () =>
  //         import ('../components/Register.vue')
  // },
  // {
  //     path: '/home',
  //     name: 'Home',
  //     component: () =>
  //         import ('../components/Home.vue')
  // },
  // {
  //     path: '/onlinewebsocket',
  //     name: 'Onlinewebsocket',
  //     component: () =>
  //         import ('../components/Onlinewebsocket.vue')
  // },
  // {
  //     path: '/mavoneditor',
  //     name: 'Mavoneditor',
  //     component: () =>
  //         import ('../components/Mavoneditor.vue')
  // },
  // {
  //     path: '/bilicom',
  //     name: 'Bilicom',
  //     component: () =>
  //         import ('../components/Bilicom.vue')
  // }
yma16's avatar
yma16 已提交
105 106
]

yma16's avatar
yma16 已提交
107 108 109 110 111 112 113 114 115 116
let routes = [
  ...defaultRoutes
  // ...modulesRoutes,
  // ...errorRoutes,
  // ...dashboardRouters
]
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch((err) => err)
}
yma16's avatar
yma16 已提交
117
const router = new VueRouter({
yma16's avatar
yma16 已提交
118 119
  routes
})
yma16's avatar
yma16 已提交
120

yma16's avatar
yma16 已提交
121
const whiteList = ['/login']
yma16's avatar
yma16 已提交
122 123

router.beforeEach(async (to, from, next) => {
yma16's avatar
yma16 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
  // Nprogress.start()
  // next()
  // let hasToken = getToken()
  next()
  console.log('localStorage', localStorage)
  let name = localStorage.getItem('yma16siteUserInfoName')
  let pwd = localStorage.getItem('yma16siteUserInfoPwd')
  let hasToken = {
    name: name,
    password: pwd
  }
  console.log('hasToken', hasToken)
  console.log('store.state 初始化', store.state)
  if (hasToken.name && hasToken.password) {
    console.log('store.state.user 存在', store.state.user)
    if (isEmpty(store.state.user.userInfo)) {
      // 空的 modules下的user
      console.log('路由的登录认证')
      // 用户自主登录
      await store.dispatch('user/loginUserInfo', hasToken)
      // await store.dispatch('user/loginUserInfo', hasToken)
      next()
    } else {
      next()
yma16's avatar
yma16 已提交
148
    }
yma16's avatar
yma16 已提交
149 150 151 152
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      console.log('store.state', store.state)
      next()
yma16's avatar
yma16 已提交
153
    } else {
yma16's avatar
yma16 已提交
154 155 156 157
      console.log('store.state 通过', store.state)
      next()
      // next({ path: '/login' }) // 否则全部重定向到登录页
      // Nprogress.done();
yma16's avatar
yma16 已提交
158
    }
yma16's avatar
yma16 已提交
159 160
  }
})
yma16's avatar
yma16 已提交
161 162

router.afterEach((to, from) => {
yma16's avatar
yma16 已提交
163
  Nprogress.done()
yma16's avatar
yma16 已提交
164 165
})

yma16's avatar
yma16 已提交
166
export default router