Commit 9c50cb87 by 展昭

add router.beforeach autologin

parent 2e07d1c5
Showing with 77 additions and 77 deletions
......@@ -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));
......
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 sign in to comment