派生自 wuyushui/SewerAndRainNetwork

陈泽平
2021-05-31 ff68d386c8f03f67045b4e6b176fc86dc2f7a597
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import Vue from 'vue'
import Router from 'vue-router'
import { routes } from './routes'
import { getToken } from '@/utils/navigation'
import 'nprogress/nprogress.css'
const appConfig = require('@/app.config')
const { homeRouterName, loginRouteName, routeMode } = appConfig
const LOGIN_PAGE_ROUTE_NAME = loginRouteName
Vue.use(Router)
const baseName = process.env.NODE_ENV === 'production' ? `/${appConfig.projectName}/` : '/'
const router = new Router({
  base: baseName,
  mode: routeMode || 'hash',
  routes: routes
})
router.beforeEach((to, from, next) => {
  Vue.prototype.$cancels.forEach((cancel) => {
    cancel()
  })
  Vue.prototype.$cancels = []
  const token = getToken()
  if (!token && to.name !== LOGIN_PAGE_ROUTE_NAME) {
    // 未登录且要跳转的页面不是登录页
    /* next({
      name: LOGIN_PAGE_ROUTE_NAME // 跳转到登录页
    }) */
    next() // 跳转
  } else if (!token && to.name === LOGIN_PAGE_ROUTE_NAME) {
    // 未登陆且要跳转的页面是登录页
    next() // 跳转
  } else if (token && to.name === LOGIN_PAGE_ROUTE_NAME) {
    // 已登录且要跳转的页面是登录页
    // next({
    //   name: homeRouterName // 跳转到homeName页
    // })
    console.log(homeRouterName)
    next()
  } else if (!token) {
    // next({
    //   name: LOGIN_PAGE_ROUTE_NAME // 跳转到登录页
    // })
    console.log(LOGIN_PAGE_ROUTE_NAME)
    next()
  } else {
    next()
  }
  // 不需要登录认证的路由
  if (Object.hasOwnProperty.call(to.meta, 'noLoginIdentify') && to.meta.noLoginIdentify) {
    next()
  }
  // next()
})
router.beforeResolve((to, from, next) => {
  next()
})
router.afterEach((to, from) => {
})
export default router