123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- import router from './router'
- // import Layout from '@/layout'
- import {
- getToken
- // deleteToken
- } from './utils/auth'
- // import store from './store'
- import Vue from 'vue'
- // 路由拦截
- router.beforeEach((to, from, next) => {
- // debugger
- // 无需登录无需获取动态路由
- if (to?.meta?.noLogin) {
- next()
- return
- }
- // 前往登陆页面判断
- if (to.path === '/login-local') {
- if (!getToken()) {
- next()
- return
- }
- // onFilterRoutes(to, next)
- next('/')
- return
- }
- if (to.path === '/login') {
- next('/home')
- // return
- }
- if (!getToken()) {
- next('/login-local')
- return
- }
- next()
- // onFilterRoutes(to, next)
- })
- router.afterEach((to, from) => {
- document.title = to.meta.title ?? Vue.prototype.$DEFAULT_TITLE
- })
- // 路由过滤和跳转
- // function onFilterRoutes (to, next) {
- // // 判断是否刷新页面重新加载路由
- // if (!store.getters.refresh) {
- // next()
- // return
- // }
- // // if (store.getters.roles && store.getters.roles.length) {
- // // initRoutes(to, next, store.getters.roles)
- // // return
- // // }
- // store.dispatch('menu/getMenu2').then(res => {
- // initRoutes(to, next, res)
- // })
- // // .catch(err => {
- // // deleteToken()
- // // next({ ...to, replace: true })
- // // // next('/login-local')
- // // Vue.prototype.$snackbar.error(err)
- // // })
- // }
- // function initRoutes (to, next, e) {
- // // debugger
- // store.commit('menu/SET_REFRESH', false) // 关闭更新
- // // 清除路由
- // resetRouter()
- // const routes = mapASyncRoutes(e)
- // // 路由替换
- // // debugger
- // // if (!routes.length) {
- // // deleteToken()
- // // // next('/login-local')
- // // next({ ...to, replace: true })
- // // return
- // // }
- // router.addRoute({ path: '/', redirect: '/home' })
- // routes.forEach(item => {
- // console.log(item)
- // router.addRoute(item)
- // })
- // router.addRoute({ path: '*', redirect: '/404', hidden: true })
- // // const hasHome = routes.find(_e => _e.name === 'home')
- // // router.addRoute({ path: '/', redirect: hasHome ? '/home' : chooseFirst(routes[0]) })
- // // router.addRoute({ path: '*', redirect: '/404', hidden: true })
- // console.log(router.getRoutes()) // 检查新路由是否已添加
- // debugger
- // next({ ...to, replace: true })
- // }
- // function chooseFirst (item) {
- // if (item.children && item.children.length > 0 && item.component.name === 'layoutIndex') {
- // return chooseFirst(item.children[0])
- // }
- // return item.path
- // }
- // 递归路由 转换为组件对象和路径
- // function mapASyncRoutes (data) {
- // return data.map(item => {
- // if (!item.redirect) {
- // item.redirect = undefined
- // }
- // item.component = item.component === 'Layout' ? Layout : loadView(item.component)
- // if (item.children && item.children.length > 0) {
- // item.children = mapASyncRoutes(item.children)
- // }
- // return item
- // })
- // }
- // // 路由插件
- // function loadView (view) {
- // return !view ? { render: (c) => c('router-view') } : () => import(`@/views/${view}`)
- // }
|