2026-04-13 17:04:38 +08:00
|
|
|
import router from './router'
|
|
|
|
|
import store from './store'
|
2026-04-14 20:36:01 +08:00
|
|
|
import { Message } from 'element-ui'
|
2026-04-13 17:04:38 +08:00
|
|
|
import NProgress from 'nprogress'
|
|
|
|
|
import 'nprogress/nprogress.css'
|
2026-04-14 20:36:01 +08:00
|
|
|
import { getToken } from '@/utils/auth'
|
|
|
|
|
import { isRelogin } from '@/utils/request'
|
2026-04-13 17:04:38 +08:00
|
|
|
|
2026-04-14 20:36:01 +08:00
|
|
|
NProgress.configure({ showSpinner: false })
|
2026-04-13 17:04:38 +08:00
|
|
|
|
|
|
|
|
const whiteList = ['/login', '/register']
|
|
|
|
|
|
|
|
|
|
router.beforeEach((to, from, next) => {
|
|
|
|
|
NProgress.start()
|
|
|
|
|
if (getToken()) {
|
|
|
|
|
to.meta.title && store.dispatch('settings/setTitle', to.meta.title)
|
|
|
|
|
if (to.path === '/login') {
|
2026-04-14 20:36:01 +08:00
|
|
|
next({ path: '/' })
|
2026-04-13 17:04:38 +08:00
|
|
|
NProgress.done()
|
|
|
|
|
} else {
|
|
|
|
|
if (store.getters.roles.length === 0) {
|
|
|
|
|
isRelogin.show = true
|
|
|
|
|
store.dispatch('GetInfo').then(res => {
|
|
|
|
|
isRelogin.show = false
|
|
|
|
|
store.dispatch('GenerateRoutes').then(accessRoutes => {
|
2026-04-14 20:36:01 +08:00
|
|
|
|
|
|
|
|
const officeMenu = accessRoutes.find(m => m.path === '/oa')
|
|
|
|
|
if (officeMenu) {
|
|
|
|
|
const staticOa = router.options.routes.find(r => r.path === '/oa')
|
|
|
|
|
const historyMenu = staticOa?.children?.find(c => c.name === 'ApprovalHistory')
|
|
|
|
|
if (historyMenu) {
|
|
|
|
|
officeMenu.children.push(historyMenu)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
router.addRoutes(accessRoutes)
|
|
|
|
|
next({ ...to, replace: true })
|
2026-04-13 17:04:38 +08:00
|
|
|
})
|
|
|
|
|
}).catch(err => {
|
|
|
|
|
store.dispatch('LogOut').then(() => {
|
|
|
|
|
Message.error(err)
|
2026-04-14 20:36:01 +08:00
|
|
|
next({ path: '/' })
|
2026-04-13 17:04:38 +08:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
next()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (whiteList.indexOf(to.path) !== -1) {
|
|
|
|
|
next()
|
|
|
|
|
} else {
|
2026-04-14 20:36:01 +08:00
|
|
|
next(`/login?redirect=${to.fullPath}`)
|
2026-04-13 17:04:38 +08:00
|
|
|
NProgress.done()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
router.afterEach(() => {
|
|
|
|
|
NProgress.done()
|
2026-04-14 20:36:01 +08:00
|
|
|
})
|