Commit da373d1e by 展昭
parents 4f48b4c5 7c72fb15
......@@ -27,19 +27,84 @@ const router = new VueRouter({
return { x: 0, y: 0 }
}
})
//自动登录
function autoLogin(payload = {autoCode, redirectPath}){
store.dispatch("authenticateOidc",{
options:{
extraQueryParams:{
authcode: payload.autoCode
},
redirectPath: payload.redirectPath
}
});
}
//router.beforeEach(vuexOidcCreateRouterMiddleware(store));
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 authcode = to.query.authcode;
const params = { to, authcode }
store.dispatch('oidcCheckAccess', params).then(function (hasAccess) {
if (hasAccess) {
next();
router.beforeEach((to,from, next)=>{
const authcode= to.query.authcode;
if(!!authcode){
var toPath = to.path||"/";
for(let key in to.query){
if(key=='authcode'){
continue;
}
toPath+= toPath.indexOf("?")>-1?"&":"?";
toPath+=`${key}=${to.query}`;
}
});
const userId = to.query.userid || to.query.userId;
const isAuth = store.getters.oidcIsAuthenticated;
console.log(isAuth)
// if(isAuth){
store.dispatch("getOidcUser").then(oidcUser=>{
if(oidcUser){
//如果登录用户与当前登录用户不匹配,则保存登录参数,退出
if(oidcUser.profile.sub!= userId){
setAutoLogin({autoCode: authcode, redirectPath: toPath})
store.dispatch("signOutOidc")
}else{
next()
}
}else{
autoLogin({autoCode: authcode,redirectPath: toPath})
}
}).catch(()=>{
autoLogin({autoCode: authcode,redirectPath: toPath})
});
}else{
next()
}
})
router.beforeEach(vuexOidcCreateRouterMiddleware(store));
//TODO: 把callBack中的app_register_id逻辑替换到这里
export default router
......@@ -5,7 +5,8 @@ import oidcSettings from "../utils/oidcSettings";
import bill from './modules/bill'
import token from './modules/token'
import any from './modules/any'
import more from './modules/more'
import more from './modules/more';
import api from '../api/index'
Vue.use(Vuex)
......@@ -15,6 +16,14 @@ export default new Vuex.Store({
token,
any,
more,
oidc: vuexOidcCreateStoreModule(oidcSettings)
oidc: vuexOidcCreateStoreModule(oidcSettings,
{
isAuthenticatedBy: 'access_token',
dispatchEventsOnWindow: true
},{
userLoaded:(user)=>{
console.log("userLoaded=>",user)
}
})
}
});
......@@ -18,6 +18,9 @@ export default {
methods: {
...mapActions(["oidcSignInCallback", "getOidcUser"]),
getQueryString(urlsearch, name) {
if(!urlsearch){
return null;
}
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = urlsearch.match(reg);
if (r != null) return unescape(r[2]);
......@@ -60,9 +63,10 @@ export default {
mounted() {
this.oidcSignInCallback()
.then(redirectPath => {
console.log(2);
console.log(2,redirectPath);
this.getOidcUser()
.then(user => {
let urlsearch = redirectPath.split("?")[1];
let appid = this.getQueryString(urlsearch, "appid");
this.getToken(user.access_token, appid, redirectPath);
......
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