123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- // import Vue from 'vue'
- import router from './router'
- import store from './store'
- import { Message } from 'element-ui'
- import NProgress from 'nprogress' // progress bar
- import 'nprogress/nprogress.css' // progress bar style
- import { getToken } from '@/utils/auth' // get token from cookie
- import getPageTitle from '@/utils/get-page-title'
- NProgress.configure({ showSpinner: false }) // NProgress Configuration
- const whiteList = ['/login','/noPermission'] // no redirect whitelist
- let isOpen = false
- router.onError((error) => {
- if (error instanceof Error) {
- const isChunkLoadFailed = error.name.indexOf('chunk')
- const targetPath = router.history.pending.fullPath;
- if (isChunkLoadFailed && !isOpen) {
- isOpen = true
- // (router.app.$confirm)
- // router.push({ path: "/403", query: { path: targetPath } })
- router.app.$confirm("网站有更新请点击确定刷新页面?", "更新提示", {
- confirmButtonText: "确定",
- cancelButtonText: "取消",
- type: "warning"
- })
- .then(() => {
- // router.replace(targetPath);
- location.hash = targetPath
- window.location.reload()
- })
- .catch(() => {
- return
- });
- }
- }
- });
- function getFirstMenu(routes) {
- // console.log(routes)
- // let firstMenu = null
- // routes.forEach(item => {
- // if(!firstMenu && item.children?.length > 0) {
- // firstMenu = pathErgodic(item)
- // }
- // })
- // console.log('firstMenu',firstMenu)
- return routes[0].redirect
- }
- function pathErgodic(item) {
- let firstMenu = null
- item.children.forEach(i => {
- if(!firstMenu && i.children?.length > 0) {
- firstMenu = pathErgodic(i)
- } else {
- if(!firstMenu && checkPathUrl(i.path)) {
- firstMenu = i.path
- } else {
- if(!firstMenu && !i.hidden) {
- firstMenu = item.path + '/' + i.path
- }
- }
- }
- })
- return firstMenu
- }
- // 判断path有没有带/,并且是第一个位置
- function checkPathUrl(path) {
- return path.indexOf('/') === 0 ? true : false
- }
- router.beforeEach(async (to, from, next) => {
- // from.query = to.query
- // start progress bar
- NProgress.start()
- // set page title
- // document.title = getPageTitle(to.meta.title)
- document.title = getPageTitle()
- // determine whether the user has logged in
- const hasToken = getToken()
- if (hasToken) {
- if (to.path === '/login') {
- // 如果有tonken直接跳转到首页
- next({ path: '/' })
- NProgress.done()
- } else {
- const hasGetUserInfo = store.getters.phone
- // 有名字 说明有用户信息 跳走
- if (hasGetUserInfo) {
- // const accessRoutes = await store.dispatch('permission/generateRoutes')
- // 动态添加可访问的路由
- // router.addRoutes(accessRoutes)
- next()
- } else {
- try {
- // 异步获取用户信息
- await store.dispatch('user/getInfo')
- // 请求接口 生成可访问路由
- const accessRoutes = await store.dispatch('permission/generateRoutes')
- // console.log(accessRoutes, 'accessRoutes')
- const isMenu = accessRoutes && accessRoutes.length > 0 ? true : false
- accessRoutes.push({ path: '*', redirect: '/404', hidden: true })
- console.log(accessRoutes,'accessRoutes')
- // 动态添加可访问的路由
- router.addRoutes(accessRoutes)
- // 确保addroutes完整的hack方法
- localStorage.removeItem('firstMenuUrl')
- // 设置replace:true,这样导航就不会留下历史记录。
- let firstMenu = getFirstMenu(accessRoutes)
- localStorage.setItem('firstMenuUrl', firstMenu)
- // console.log(firstMenu, 'firstMenu', isMenu)
- // 判断是否有菜单
- if(isMenu) {
- if(to.path == '/workbench') {
- next({ path: firstMenu, replace: true })
- } else {
- next({ ...to, replace: true })
- }
- } else {
- next({ path: '/noPermission', replace: true })
- }
- } catch (error) {
- // remove token and go to login page to re-login
- await store.dispatch('user/resetToken')
- if(error.msg) {
- Message.error(error.msg)
- }
- next(`/login`)
- NProgress.done()
- }
- }
- }
- } else {
- /* has no token*/
- console.log(to.path,'to.path')
- if (whiteList.indexOf(to.path) !== -1) {
- // in the free login whitelist, go directly
- next()
- } else {
- // other pages that do not have permission to access are redirected to the login page.
- next(`/login`)
- NProgress.done()
- }
- }
- })
- router.afterEach(() => {
- // finish progress bar
- NProgress.done()
- })
|