Commit 9584dd6c authored by guang.wu's avatar guang.wu

feat: 添加token过期新标签页登录功能

parent d61ab8eb
......@@ -17,6 +17,7 @@ const instance = axios.create(Object.assign({}, config));
// 挂载扩展配置
instance._extend = {
notice: '',
modal: '', // modal组件
loginUrl: '',
refresTokenHost: '',
dataParseMode: 'default'
......@@ -86,6 +87,54 @@ function clearRequest() {
}
httpQueue = [];
}
const showLoginResultPopups = () => {
window.modalPopup = instance._extend.modal;
instance._extend.modal.info({
title: '温馨提示',
content: '登录完成',
okText: '关闭',
});
};
const openWindow = url => {
let a = document.createElement('a');
document.body.appendChild(a);
a.addEventListener('click', function(){
o = window.open();
o.focus();
showLoginResultPopups();
o.location.href = url;
});
a.click();
document.body.removeChild(a);
return false;
};
const showLoginPopups = () => {
if (instance._extend.modal) {
let href = window.location.href;
href = href.replace(/\?token=[a-z0-9\-A-Z]+/g, '');
href = instance._extend.loginUrl + '?url=' + window.btoa(href);
instance._extend.modal.confirm({
title: '会话过期',
content: '当前会话已过期,请重新登录。',
okText: '登录',
cancelText: '关闭',
onOk: function() {
instance._extend.modal.remove();
openWindow(href);
setTimeout(() => {
showLoginResultPopups();
}, 1000);
return false;
},
});
} else {
window.location.href = instance._extend.loginUrl + '?url=' + window.btoa(href);
}
};
// http request 拦截器
instance.interceptors.request.use(
config => {
......@@ -163,9 +212,7 @@ instance.interceptors.response.use(
return refreshRequest(response.config);
}
if (+code === 4010 || +code === 4011) {
let href = window.location.href;
href = href.replace(/\?token=[a-z0-9\-A-Z]+/g, '');
window.location.href = instance._extend.loginUrl + '?url=' + window.btoa(href);
showLoginPopups();
return;
}
instance._extend.notice.error({
......@@ -190,9 +237,7 @@ instance.interceptors.response.use(
switch (error.response.status) {
case 401:
// todo: 系统中有3处处理401的地方
let href = window.location.href;
href = href.replace(/\?token=[a-z0-9\-A-Z]+/g, '');
window.location.href = instance._extend.loginUrl + '?url=' + window.btoa(href);
showLoginPopups();
return;
}
}
......
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 to comment