django-vue-admin/web/src/router/index.js

117 lines
3.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* @创建文件时间: 2021-06-01 22:41:21
* @Auther: 猿小天
* @最后修改人: 猿小天
* @最后修改时间: 2021-11-19 21:49:43
* 联系Qq:1638245306
* @文件介绍:
*/
import Vue from 'vue'
import VueRouter from 'vue-router'
// 进度条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import store from '@/store/index'
import util from '@/libs/util.js'
// 路由数据
import routes from './routes'
import { getMenu, handleAsideMenu, handleRouter, checkRouter } from '@/menu'
// fix vue-router NavigationDuplicated
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
return VueRouterPush.call(this, location).catch(err => err)
}
const VueRouterReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace (location) {
return VueRouterReplace.call(this, location).catch(err => err)
}
Vue.use(VueRouter)
// 导出路由 main.js 里使用
const router = new VueRouter({
routes
})
/**
* 路由拦截
* 权限验证
*/
router.beforeEach(async (to, from, next) => {
// 白名单
const whiteList = ['/login', '/auth-redirect', '/bind', '/register', '/oauth2']
// 确认已经加载多标签页数据 https://github.com/d2-projects/d2-admin/issues/201
await store.dispatch('d2admin/page/isLoaded')
// 确认已经加载组件尺寸设置 https://github.com/d2-projects/d2-admin/issues/198
await store.dispatch('d2admin/size/isLoaded')
// 进度条
NProgress.start()
// 关闭搜索面板
store.commit('d2admin/search/set', false)
// 验证当前路由所有的匹配中是否需要有登录验证的
// 这里暂时将cookie里是否存有token作为验证是否登录的条件
// 请根据自身业务需要修改
const token = util.cookies.get('token')
if (token && token !== 'undefined') {
if (!store.state.d2admin.menu || store.state.d2admin.menu.aside.length === 0) {
// 动态添加路由
getMenu().then(ret => {
// 校验路由是否有效
ret = checkRouter(ret)
const routes = handleRouter(ret)
// 处理路由 得到每一级的路由设置
store.commit('d2admin/page/init', routes)
router.addRoutes(routes)
// routes.forEach(route => router.addRoute(route))
const menu = handleAsideMenu(ret)
const aside = handleAsideMenu(ret.filter(value => value.visible === true))
store.commit('d2admin/menu/asideSet', aside) // 设置侧边栏菜单
store.commit('d2admin/search/init', menu) // 设置搜索
next({ path: to.fullPath, replace: true, params: to.params })
})
} else {
next()
const childrenPath = window.qiankunActiveRule || []
if (to.name) {
// name 属性说明是主应用的路由
next()
} else if (childrenPath.some((item) => to.path.includes(item))) {
next()
} else {
next({ name: '404' })
}
}
} else {
// 没有登录的时候跳转到登录界面
// 携带上登陆成功之后需要跳转的页面完整路径
// https://github.com/d2-projects/d2-admin/issues/138
if (whiteList.indexOf(to.path) !== -1) {
// 在免登录白名单直接进入
next()
} else {
next({
name: 'login',
query: {
redirect: to.fullPath
}
})
NProgress.done()
}
}
})
router.afterEach(to => {
// 进度条
NProgress.done()
// 多页控制 打开新的页面
store.dispatch('d2admin/page/open', to)
// 更改标题
util.title(to.meta.title)
})
export default router