Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
B
bigwood
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
罗超
bigwood
Commits
05b3171c
Commit
05b3171c
authored
Nov 14, 2022
by
罗超
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
調整
parent
811247e8
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
72 additions
and
68 deletions
+72
-68
permission.ts
src/boot/permission.ts
+72
-68
No files found.
src/boot/permission.ts
View file @
05b3171c
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
)
}
})
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment