Commit a23c6244 by 展昭

update

parent a486fa34
Showing with 69 additions and 55 deletions
......@@ -4,14 +4,10 @@ import { vuexOidcCreateRouterMiddleware } from 'vuex-oidc'
import store from '../store'
import billRouters from './bill-router.js'
import moreRouters from './more-router.js'
import api from '../api/More'
import qs from "qs";
import axios from "axios";
const oidc_config = JSON.parse(process.env.VUE_APP_OIDC_CONFIG);
Vue.use(VueRouter)
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
......@@ -33,72 +29,90 @@ const router = new VueRouter({
}
})
function getAccessToken(authCode) {
return new Promise((resolve, reject) => {
api.getAccessToken()
.then(res => {
if (res && res.status) {
accessToken = res.data.accessToken;
resolve(accessToken);
}
else {
reject(res)
}
});
//自动登录
function autoLogin(payload = { autoCode, redirectPath }) {
store.dispatch("authenticateOidc", {
options: {
extraQueryParams: {
authcode: payload.autoCode
},
redirectPath: payload.redirectPath
}
});
}
async function getToken(access_token, appid, next) {
let pms = {
client_id: oidc_config.client_id,
client_secret: oidc_config.client_secret,
grant_type: "application",
scope:
"workapps.client openid api.workapps.user api.workapps.org api.workapps.open",
app_register_id: appid,
token: access_token
};
let commonPms = GetCommonPms();
let url = `${oidc_config.authority}/connect/token?${commonPms}`;
await axios
.post(url, qs.stringify(pms))
.then(
res => {
if (res.status == 200) {
const accessToken = res.data.access_token;
localStorage.setItem("token", accessToken);
next()
}
},
err => {
this.errMessage = err.response.data.error_description;
}
)
.catch(err => {
console.log("456err=", err);
});
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 || to.query.authcode;
const appid = to.query.appId || to.query.appid;
const userId = to.query.userid || to.query.userId||0;
if (!!authcode && !!appid) {
getAccessToken(authcode).then(res => {
const token = res;
getToken(token, appid, next);
}).catch(err => {
console.log('err=', err);
})
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));
router.beforeEach(vuexOidcCreateRouterMiddleware(store));
export default router
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