Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
client
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
3894a0b7
authored
Jul 31, 2025
by
孙美琪
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改路由守卫
parent
d4b6b284
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
45 deletions
+26
-45
src/layout/user-layout.vue
+3
-3
src/permission.js
+23
-42
No files found.
src/layout/user-layout.vue
View file @
3894a0b7
...
@@ -45,11 +45,11 @@
...
@@ -45,11 +45,11 @@
</div>
</div>
<div
class=
"right-menu flex-align-center"
>
<div
class=
"right-menu flex-align-center"
>
<router-link
v-if=
"userStore.
id
"
to=
"/console/overview"
class=
"console"
style=
"font-size: 18px;"
>
控制台
<router-link
v-if=
"userStore.
token
"
to=
"/console/overview"
class=
"console"
style=
"font-size: 18px;"
>
控制台
</router-link>
</router-link>
<el-dropdown
v-if=
"userStore.
id
"
@
command=
"handleCommand"
class=
""
>
<el-dropdown
v-if=
"userStore.
token
"
@
command=
"handleCommand"
class=
""
>
<div
v-if=
"userStore.
id
"
class=
"avatar-wrapper flex-align-center"
>
<div
v-if=
"userStore.
token
"
class=
"avatar-wrapper flex-align-center"
>
<img
:src=
"userStore.avatar"
class=
"user-avatar"
/>
<img
:src=
"userStore.avatar"
class=
"user-avatar"
/>
<div
class=
"user-name"
>
{{ userStore.name }}
</div>
<div
class=
"user-name"
>
{{ userStore.name }}
</div>
</div>
</div>
...
...
src/permission.js
View file @
3894a0b7
...
@@ -3,13 +3,9 @@ import { ElMessage } from 'element-plus'
...
@@ -3,13 +3,9 @@ import { ElMessage } from 'element-plus'
import
NProgress
from
'nprogress'
import
NProgress
from
'nprogress'
import
'nprogress/nprogress.css'
import
'nprogress/nprogress.css'
import
{
getToken
}
from
'@/utils/auth'
import
{
getToken
}
from
'@/utils/auth'
import
{
isHttp
}
from
'@/utils/validate'
import
{
isRelogin
}
from
'@/utils/request'
import
useUserStore
from
'@/store/modules/user'
import
useSettingsStore
from
'@/store/modules/settings'
import
useSettingsStore
from
'@/store/modules/settings'
import
usePermissionStore
from
'@/store/modules/permission'
NProgress
.
configure
({
showSpinner
:
false
})
NProgress
.
configure
({
showSpinner
:
false
})
const
whiteList
=
[
const
whiteList
=
[
'/login'
,
'/login'
,
...
@@ -25,51 +21,36 @@ const whiteList = [
...
@@ -25,51 +21,36 @@ const whiteList = [
router
.
beforeEach
((
to
,
from
,
next
)
=>
{
router
.
beforeEach
((
to
,
from
,
next
)
=>
{
NProgress
.
start
()
NProgress
.
start
()
if
(
getToken
())
{
// 设置页面标题
to
.
meta
.
title
&&
useSettingsStore
().
setTitle
(
to
.
meta
.
title
)
to
.
meta
.
title
&&
useSettingsStore
().
setTitle
(
to
.
meta
.
title
)
/* has token*/
if
(
to
.
path
===
'/login'
)
{
// 1. 判断是否在白名单中
next
({
path
:
'/'
})
if
(
whiteList
.
includes
(
to
.
path
))
{
NProgress
.
done
()
// 白名单路由直接放行,不需要检查token
}
else
if
(
whiteList
.
indexOf
(
to
.
path
)
!==
-
1
)
{
useUserStore
().
getInfo
().
then
(
res
=>
{
})
next
()
next
()
}
/*else {
NProgress
.
done
()
if (useUserStore().roles.length === 0) {
return
isRelogin.show = true
// 判断当前用户是否已拉取完user_info信息
useUserStore().getInfo().then(() => {
isRelogin.show = false
usePermissionStore().generateRoutes().then(accessRoutes => {
// 根据roles权限生成可访问的路由表
accessRoutes.forEach(route => {
if (!isHttp(route.path)) {
router.addRoute(route) // 动态添加可访问路由表
}
}
})
next({...to, replace: true}) // hack方法 确保addRoutes已完成
// 2. 非白名单路由,检查是否登录
})
const
hasToken
=
getToken
()
}).catch(err => {
if
(
!
hasToken
)
{
useUserStore().logOut().then(() => {
// 未登录访问非白名单路由,重定向到登录页
ElMessage.error(err)
next
(
`/login?redirect=
${
to
.
fullPath
}
`
)
next({path: '/'})
NProgress
.
done
()
})
return
})
} else {
next()
}
}
}*/
// 3. 已登录访问非白名单路由,直接放行(不做权限检查)
if
(
to
.
path
===
'/login'
)
{
// 已登录但访问登录页,重定向到首页
next
({
path
:
'/'
})
NProgress
.
done
()
}
else
{
}
else
{
// 没有token
if
(
whiteList
.
indexOf
(
to
.
path
)
!==
-
1
)
{
// 在免登录白名单,直接进入
next
()
next
()
}
else
{
next
(
`/login?redirect=
${
to
.
fullPath
}
`
)
// 否则全部重定向到登录页
NProgress
.
done
()
NProgress
.
done
()
}
}
}
})
})
router
.
afterEach
(()
=>
{
router
.
afterEach
(()
=>
{
...
...
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