Commit 05b3171c authored by 罗超's avatar 罗超

調整

parent 811247e8
import { AuthMenuType } from '../@types'
import { ResultType } from '../@types/enumHelper'
import router from '../router/index'
import { boot } from 'quasar/wrappers'
import { getAuth, getUserAllMenu } from '../utils/auth'
import { LoadingBar } from 'quasar'
......@@ -13,86 +13,90 @@ LoadingBar.setDefaults({
let loadAsyncRouter = false
const whiteList = ['/auth/login','/auth/regist','/auth/forget','/auth/newpassword']
router.beforeEach((to, from, next) => {
localStorage.setItem('routerBefore', from.path)
LoadingBar.start()
/** 请求头包含授权信息 并且 页面必须授权 直接进入 */
if (getAuth()) {
//debugger;
if (to.path === '/auth/login' || to.path === '/') {
next('/index')
LoadingBar.stop()
} else {
if (!loadAsyncRouter) {
// 判断当前用户是否获取权限
loadAsyncRouter = true
const allAuth = getUserAllMenu()
console.log(allAuth)
next({
...to,
replace: true
})
if(process.env.NODE_ENV=='development'){
const whiteList = ['/auth/login','/auth/regist','/auth/forget','/auth/newpassword','/404']
export default boot(({ router, store }) => {
router.beforeEach((to, from, next) => {
localStorage.setItem('routerBefore', from.path)
LoadingBar.start()
/** 请求头包含授权信息 并且 页面必须授权 直接进入 */
if (getAuth()) {
//debugger;
if (to.path === '/auth/login' || to.path === '/') {
next('/index')
LoadingBar.stop()
} else {
if (!loadAsyncRouter) {
// 判断当前用户是否获取权限
loadAsyncRouter = true
const allAuth = getUserAllMenu()
console.log(allAuth)
next({
...to,
replace: true
})
}else{
if (allAuth != ResultType.EmptyArray) {
//TODO: 动态生成并追加路由 //router.addRoutes(store.getters.addRouters);
// if (to.path === '/404') {
// //如果用户直接访问404调回原页面或首页
// next(to.redirectedFrom || '/')
// } else {
//检查是否有权限访问
const authMenu = allAuth.findIndex((x: AuthMenuType) => {
console.log(x.menuUrl,to.path)
return x.menuUrl == to.path
if(process.env.NODE_ENV=='development'){
next({
...to,
replace: true
})
console.log(authMenu)
if (authMenu != -1) {
next({
...to,
replace: true
}else{
if (allAuth != ResultType.EmptyArray) {
//TODO: 动态生成并追加路由 //router.addRoutes(store.getters.addRouters);
// if (to.path === '/404') {
// //如果用户直接访问404调回原页面或首页
// next(to.redirectedFrom || '/')
// } else {
//检查是否有权限访问
const authMenu = allAuth.findIndex((x: AuthMenuType) => {
console.log(x.menuUrl,to.path)
return x.menuUrl == to.path
})
console.log(authMenu)
if (authMenu != -1) {
next({
...to,
replace: true
})
} else {
next('/404')
}
LoadingBar.stop()
} else {
next('/404')
LoadingBar.stop()
}
LoadingBar.stop()
} else {
next('/404')
LoadingBar.stop()
}
} else {
next()
}
} else {
}
} else {
let urlPath=to.matched[0].path
if(urlPath.indexOf('/:')!=-1) urlPath=urlPath.substring(0,urlPath.indexOf('/:'))
if (whiteList.indexOf(urlPath) !== -1) {
console.log(whiteList,to,'do something')
next()
} else {
next(`/auth/login?redirect=${decodeURIComponent(to.path)}`) // 否则全部重定向到登录页
LoadingBar.stop()
}
}
} else {
let urlPath=to.matched[0].path
if(urlPath.indexOf('/:')!=-1) urlPath=urlPath.substring(0,urlPath.indexOf('/:'))
if (whiteList.indexOf(urlPath) !== -1) {
console.log(whiteList,to.path)
next()
} else {
next(`/auth/login?redirect=${decodeURIComponent(to.path)}`) // 否则全部重定向到登录页
LoadingBar.stop()
})
router.afterEach(() => {
LoadingBar.stop() // 结束Progress
})
router.onError(error => {
const pattern = /Loading chunk (\d)+ failed/g
const isChunkLoadFailed = error.message.match(pattern)
const targetPath = error.to
console.log('異常:',error,targetPath)
if (isChunkLoadFailed) {
router.replace(targetPath)
}
}
})
router.afterEach(() => {
LoadingBar.stop() // 结束Progress
})
router.onError(error => {
const pattern = /Loading chunk (\d)+ failed/g
const isChunkLoadFailed = error.message.match(pattern)
const targetPath = error.to
if (isChunkLoadFailed) {
router.replace(targetPath)
}
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment