Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
hrs_app_h5
/
bill
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
9c50cb87
authored
May 22, 2020
by
展昭
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
add router.beforeach autologin
parent
2e07d1c5
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
77 additions
and
77 deletions
src/router/index.js
src/router/index.js
View file @
9c50cb87
...
...
@@ -28,88 +28,88 @@ const router = new VueRouter({
}
})
//
//
自动登录
//
function autoLogin(payload = { autoCode, redirectPath }) {
//
store.dispatch("authenticateOidc", {
//
options: {
//
extraQueryParams: {
//
authcode: payload.autoCode
//
},
//
redirectPath: payload.redirectPath
//
}
//
});
//
}
//自动登录
function
autoLogin
(
payload
=
{
autoCode
,
redirectPath
})
{
store
.
dispatch
(
"authenticateOidc"
,
{
options
:
{
extraQueryParams
:
{
authcode
:
payload
.
autoCode
},
redirectPath
:
payload
.
redirectPath
}
});
}
//
const loginStorage = localStorage;
//
const autoLoginParamsKey = "autoLoginParams";
//
function setAutoLogin(payload = { autoCode, redirectPath }) {
//
loginStorage.setItem(autoLoginParamsKey, JSON.stringify(payload))
//
}
//
function getAutoLoginParams() {
//
const paramStr = loginStorage.getItem(autoLoginParamsKey);
//
if (!!paramStr) {
//
loginStorage.removeItem(autoLoginParamsKey);
//
return JSON.parse(paramStr);
//
}
//
return null;
//
}
const
loginStorage
=
localStorage
;
const
autoLoginParamsKey
=
"autoLoginParams"
;
function
setAutoLogin
(
payload
=
{
autoCode
,
redirectPath
})
{
loginStorage
.
setItem
(
autoLoginParamsKey
,
JSON
.
stringify
(
payload
))
}
function
getAutoLoginParams
()
{
const
paramStr
=
loginStorage
.
getItem
(
autoLoginParamsKey
);
if
(
!!
paramStr
)
{
loginStorage
.
removeItem
(
autoLoginParamsKey
);
return
JSON
.
parse
(
paramStr
);
}
return
null
;
}
//
//
如果有登录参数则优先登录
//
router.beforeEach((to, from, next) => {
//
const autoLoginParams = getAutoLoginParams();
//
if (autoLoginParams) {
//
autoLogin(autoLoginParams);
//
} else {
//
next();
//
}
//
})
//如果有登录参数则优先登录
router
.
beforeEach
((
to
,
from
,
next
)
=>
{
const
autoLoginParams
=
getAutoLoginParams
();
if
(
autoLoginParams
)
{
autoLogin
(
autoLoginParams
);
}
else
{
next
();
}
})
//
router.beforeEach((to, from, next) => {
//
const authcode = to.query.authCode || to.query.authcode;
//
const appid = to.query.appId || to.query.appid;
//
const userId = to.query.userid || to.query.userId||0;
//
if (!!authcode && !!appid) {
router
.
beforeEach
((
to
,
from
,
next
)
=>
{
const
authcode
=
to
.
query
.
authCode
||
to
.
query
.
authcode
;
const
appid
=
to
.
query
.
appId
||
to
.
query
.
appid
;
const
userId
=
to
.
query
.
userid
||
to
.
query
.
userId
||
0
;
if
(
!!
authcode
&&
!!
appid
)
{
//
var toPath = to.path || "/";
//
for (let key in to.query) {
//
if (key == 'authcode'||key == 'authCode') {
//
continue;
//
}
//
toPath += toPath.indexOf("?") > -1 ? "&" : "?";
//
toPath += `${key}=${to.query[key]}`;
//
}
//
const storeAppId = localStorage.getItem('appid');
//
store.dispatch("getOidcUser").then(oidcUser => {
//
if (oidcUser) {
//
if(storeAppId!= appid){
//
localStorage.setItem('appid',appid);
//
setAutoLogin({ autoCode: authcode, redirectPath: toPath })
//
store.dispatch("signOutOidc")
//
}else{
//
//如果登录用户与当前登录用户不匹配,则保存登录参数,退出
//
console.log('userId=',userId);
//
console.log('oidcUser.profile.sub=',oidcUser.profile.sub);
//
console.log('oidcUser.profile.sub==userId:',oidcUser.profile.sub==userId);
//
if (userId > 0 && oidcUser.profile.sub != userId) {
//
setAutoLogin({ autoCode: authcode, redirectPath: toPath })
//
store.dispatch("signOutOidc")
//
} else {
//
next()
//
}
//
}
//
} else {
//
localStorage.setItem('appid',appid);
//
autoLogin({ autoCode: authcode, redirectPath: toPath })
//
}
//
}).catch(() => {
//
localStorage.setItem('appid',appid);
//
autoLogin({ autoCode: authcode, redirectPath: toPath })
//
});
//
} else {
//
next()
//
}
var
toPath
=
to
.
path
||
"/"
;
for
(
let
key
in
to
.
query
)
{
if
(
key
==
'authcode'
||
key
==
'authCode'
)
{
continue
;
}
toPath
+=
toPath
.
indexOf
(
"?"
)
>
-
1
?
"&"
:
"?"
;
toPath
+=
`
${
key
}
=
${
to
.
query
[
key
]}
`
;
}
const
storeAppId
=
localStorage
.
getItem
(
'appid'
);
store
.
dispatch
(
"getOidcUser"
).
then
(
oidcUser
=>
{
if
(
oidcUser
)
{
if
(
storeAppId
!=
appid
){
localStorage
.
setItem
(
'appid'
,
appid
);
setAutoLogin
({
autoCode
:
authcode
,
redirectPath
:
toPath
})
store
.
dispatch
(
"signOutOidc"
)
}
else
{
//如果登录用户与当前登录用户不匹配,则保存登录参数,退出
console
.
log
(
'userId='
,
userId
);
console
.
log
(
'oidcUser.profile.sub='
,
oidcUser
.
profile
.
sub
);
console
.
log
(
'oidcUser.profile.sub==userId:'
,
oidcUser
.
profile
.
sub
==
userId
);
if
(
userId
>
0
&&
oidcUser
.
profile
.
sub
!=
userId
)
{
setAutoLogin
({
autoCode
:
authcode
,
redirectPath
:
toPath
})
store
.
dispatch
(
"signOutOidc"
)
}
else
{
next
()
}
}
}
else
{
localStorage
.
setItem
(
'appid'
,
appid
);
autoLogin
({
autoCode
:
authcode
,
redirectPath
:
toPath
})
}
}).
catch
(()
=>
{
localStorage
.
setItem
(
'appid'
,
appid
);
autoLogin
({
autoCode
:
authcode
,
redirectPath
:
toPath
})
});
}
else
{
next
()
}
//
})
})
router
.
beforeEach
(
vuexOidcCreateRouterMiddleware
(
store
));
...
...
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