Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
bdc
/
bdcdj-web
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
de2be3ea
authored
2023-06-02 11:22:58 +0800
by
苗菁
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
feat: 单点登录token
1 parent
86f1e19d
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
87 additions
and
94 deletions
src/layout/components/Navbar.vue
src/permission.js
src/utils/request.js
src/layout/components/Navbar.vue
View file @
de2be3e
...
...
@@ -77,7 +77,7 @@
if
(
process
.
env
.
NODE_ENV
===
'development'
)
{
localStorage
.
removeItem
(
'token'
)
}
else
{
Cookies
.
remove
(
'
token
'
)
Cookies
.
remove
(
'
ACCESS_TOKEN
'
)
}
if
(
window
.
_config
.
casEnable
)
{
window
.
location
.
href
=
window
.
_config
.
casBaseURL
+
'/logout?service='
+
encodeURIComponent
(
window
.
location
.
href
);
...
...
src/permission.js
View file @
de2be3e
...
...
@@ -8,99 +8,101 @@ import axios from 'axios'
import
router
from
'./router'
import
store
from
'./store'
import
Cookies
from
'js-cookie'
import
{
getMenuInfo
}
from
'@/api/user'
import
{
getUrlParam
}
from
'@/utils/operation'
import
{
getMenuInfo
}
from
'@/api/user'
import
{
getUrlParam
}
from
'@/utils/operation'
import
NProgress
from
'nprogress'
// progress bar
import
'nprogress/nprogress.css'
// progress bar style
import
getPageTitle
from
'@/utils/get-page-title'
NProgress
.
configure
({
showSpinner
:
false
})
// NProgress Configuration
NProgress
.
configure
({
showSpinner
:
false
})
// NProgress Configuration
router
.
beforeEach
(
async
(
to
,
from
,
next
)
=>
{
Vue
.
prototype
.
$currentRoute
=
to
NProgress
.
start
()
document
.
title
=
getPageTitle
(
to
.
meta
.
title
)
let
hasAddDict
=
store
.
state
.
dict
.
addDict
let
hasAddRoute
=
store
.
state
.
permission
.
addRoutes
// cas操作
const
token
=
localStorage
.
getItem
(
"token"
)
||
Cookies
.
get
(
'token'
);
if
(
to
.
path
===
'/login'
)
{
if
(
token
)
{
next
(
'/'
)
}
else
{
next
()
}
return
Vue
.
prototype
.
$currentRoute
=
to
NProgress
.
start
()
document
.
title
=
getPageTitle
(
to
.
meta
.
title
)
let
hasAddDict
=
store
.
state
.
dict
.
addDict
let
hasAddRoute
=
store
.
state
.
permission
.
addRoutes
// cas操作
const
token
=
localStorage
.
getItem
(
"token"
)
||
Cookies
.
get
(
'token'
);
if
(
to
.
path
===
'/login'
)
{
if
(
token
)
{
next
(
'/'
)
}
else
{
next
()
}
if
(
window
.
_config
.
casEnable
===
true
)
{
let
locationUrl
=
window
.
location
.
protocol
+
'//'
+
window
.
location
.
host
+
window
.
location
.
pathname
;
if
(
!
token
)
{
let
ticket
=
getUrlParam
(
'ticket'
);
if
(
ticket
)
{
axios
.
get
(
Vue
.
prototype
.
BASE_API
.
ip
+
"/management/cas/validate"
,
{
params
:
{
'ticket'
:
ticket
,
'service'
:
locationUrl
}
}).
then
(
async
(
res
)
=>
{
if
(
process
.
env
.
NODE_ENV
===
'development'
)
{
localStorage
.
setItem
(
'token'
,
res
.
data
.
content
.
accessToken
)
}
else
{
Cookies
.
set
(
'token'
,
res
.
data
.
content
.
accessToken
)
}
window
.
location
.
href
=
localStorage
.
getItem
(
'location'
)
return
}
if
(
window
.
_config
.
casEnable
===
true
)
{
let
locationUrl
=
window
.
location
.
protocol
+
'//'
+
window
.
location
.
host
+
window
.
location
.
pathname
;
if
(
!
token
)
{
let
ticket
=
getUrlParam
(
'ticket'
);
if
(
ticket
)
{
axios
.
get
(
Vue
.
prototype
.
BASE_API
.
ip
+
"/management/cas/validate"
,
{
params
:
{
'ticket'
:
ticket
,
'service'
:
locationUrl
}
}).
then
(
async
(
res
)
=>
{
if
(
process
.
env
.
NODE_ENV
===
'development'
)
{
localStorage
.
setItem
(
'token'
,
res
.
data
.
content
.
accessToken
)
}
else
{
Cookies
.
set
(
'ACCESS_TOKEN'
,
res
.
data
.
content
.
accessToken
)
}
window
.
location
.
href
=
localStorage
.
getItem
(
'location'
)
}).
catch
(
e
=>
{
console
.
log
(
e
)
})
}
else
{
localStorage
.
setItem
(
"location"
,
window
.
location
.
href
)
window
.
location
.
href
=
window
.
_config
.
casBaseURL
+
'/login?service='
+
encodeURIComponent
(
locationUrl
);
}
}
else
{
permission
()
}
async
function
permission
()
{
if
(
!
hasAddDict
)
{
store
.
dispatch
(
'dict/generateDic'
)
}
if
(
hasAddRoute
)
{
next
()
// next({ ...to, replace: true })
}
else
{
const
{
result
:
getMenuData
}
=
await
getMenuInfo
()
const
accessRoutes
=
await
store
.
dispatch
(
'permission/generateRoutes'
,
getMenuData
)
// 获取用户信息
await
store
.
dispatch
(
'user/getUserInfo'
)
router
.
addRoutes
([...
accessRoutes
,
{
path
:
'*'
,
redirect
:
'/404'
,
hidden
:
true
}])
const
routeTo
=
Cookies
.
get
(
'routerTo'
)
if
(
routeTo
&&
routeTo
!==
'/'
)
{
next
({
...
to
,
replace
:
true
})
}
else
{
next
(
'/home'
)
}
}
}
}).
catch
(
e
=>
{
console
.
log
(
e
)
})
}
else
{
localStorage
.
setItem
(
"location"
,
window
.
location
.
href
)
window
.
location
.
href
=
window
.
_config
.
casBaseURL
+
'/login?service='
+
encodeURIComponent
(
locationUrl
);
}
}
else
{
if
(
!
token
)
{
const
redirectData
=
{
path
:
'/login'
,
replace
:
true
,
}
if
(
to
.
path
)
{
redirectData
.
query
=
{
...
redirectData
.
query
,
redirect
:
to
.
path
,
};
}
next
(
redirectData
)
return
}
permission
()
}
async
function
permission
()
{
if
(
!
hasAddDict
)
{
store
.
dispatch
(
'dict/generateDic'
)
}
if
(
hasAddRoute
)
{
next
()
// next({ ...to, replace: true })
}
else
{
const
{
result
:
getMenuData
}
=
await
getMenuInfo
()
const
accessRoutes
=
await
store
.
dispatch
(
'permission/generateRoutes'
,
getMenuData
)
// 获取用户信息
await
store
.
dispatch
(
'user/getUserInfo'
)
router
.
addRoutes
([...
accessRoutes
,
{
path
:
'*'
,
redirect
:
'/404'
,
hidden
:
true
}])
const
routeTo
=
Cookies
.
get
(
'routerTo'
)
if
(
routeTo
&&
routeTo
!==
'/'
)
{
next
({...
to
,
replace
:
true
})
}
else
{
next
(
'/home'
)
}
}
}
}
else
{
if
(
!
token
)
{
const
redirectData
=
{
path
:
'/login'
,
replace
:
true
,
}
if
(
to
.
path
)
{
redirectData
.
query
=
{
...
redirectData
.
query
,
redirect
:
to
.
path
,
};
}
next
(
redirectData
)
return
}
NProgress
.
done
()
next
()
}
NProgress
.
done
()
})
router
.
afterEach
(
to
=>
{
// 解决刷新页面报404问题
Cookies
.
set
(
"routerTo"
,
to
.
fullPath
)
NProgress
.
done
()
// 解决刷新页面报404问题
Cookies
.
set
(
"routerTo"
,
to
.
fullPath
)
NProgress
.
done
()
})
...
...
src/utils/request.js
View file @
de2be3e
...
...
@@ -34,14 +34,6 @@ service.interceptors.request.use(
}
else
{
config
.
headers
.
delete
(
'Authorization'
)
}
}
else
{
const
token
=
Cookies
.
get
(
'token'
)
// 添加请求头
if
(
token
)
{
config
.
headers
[
'Authorization'
]
=
'Bearer '
+
token
}
else
{
config
.
headers
.
delete
(
'Authorization'
)
}
}
return
config
},
...
...
@@ -78,7 +70,7 @@ service.interceptors.response.use(
if
(
process
.
env
.
NODE_ENV
===
'development'
)
{
localStorage
.
removeItem
(
'token'
)
}
else
{
Cookies
.
remove
(
'
token
'
)
Cookies
.
remove
(
'
ACCESS_TOKEN
'
)
}
if
(
window
.
_config
.
casEnable
)
{
window
.
location
.
href
=
window
.
_config
.
casBaseURL
+
'/logout?service='
+
encodeURIComponent
(
locationUrl
);
...
...
@@ -92,7 +84,6 @@ service.interceptors.response.use(
return
false
}
}
}
else
{
// 对响应错误做点什么
Message
({
...
...
Please
register
or
sign in
to post a comment