Commit eddcff1e authored by 徐光星's avatar 徐光星

Merge branch 'feature/timeout' into 'master'

feat: 添加记录最后一次请求时间,超5小时之间跳转登录没超5小时弹窗登录

See merge request !6
parents aa8dc4c5 83c876f1
import axios from 'axios'; import axios from 'axios';
import localStorage from './localStorage'; import localStorage from './localStorage';
import Bus from './bus'; import Bus from './bus';
import { debounce } from './utils';
let CancelToken = axios.CancelToken; // axios中断取消请求 let CancelToken = axios.CancelToken; // axios中断取消请求
let httpQueue = []; // 请求队列 let httpQueue = []; // 请求队列
...@@ -111,11 +112,35 @@ const openWindow = url => { ...@@ -111,11 +112,35 @@ const openWindow = url => {
return false; return false;
}; };
const showLoginPopups = () => { /**
if (instance._extend.modal) { * 验证token是否超过5小时
let href = window.location.href; * **/
href = href.replace(/\?token=[a-z0-9\-A-Z]+/g, ''); const checkTokenTimeOut5Hours = () => {
href = instance._extend.loginUrl + '?url=' + window.btoa(href); const tokenTime = +localStorage.get('token-time');
const now = new Date().getTime();
return now - tokenTime > 5 * 60 * 60 * 1000;
};
/**
* 刷新token时间戳 记录最后一次请求时间
* **/
const refreshTokenTimeOut = debounce(() => {
const now = new Date().getTime();
localStorage.set('token-time', now);
}, 100);
/**
* token 过期处理函数
* token过期超过5小时,直接跳转登录页
* 未超过5小时,弹窗提示跳转新标签页登录
* **/
const showLoginPopups = debounce(() => {
let href = window.location.href;
href = href.replace(/\?token=[a-z0-9\-A-Z]+/g, '');
href = instance._extend.loginUrl + '?url=' + window.btoa(href);
if (checkTokenTimeOut5Hours() || !instance._extend.modal) {
window.location.href = href;
} else {
instance._extend.modal.confirm({ instance._extend.modal.confirm({
title: '会话过期', title: '会话过期',
content: '当前会话已过期,请重新登录。', content: '当前会话已过期,请重新登录。',
...@@ -130,10 +155,8 @@ const showLoginPopups = () => { ...@@ -130,10 +155,8 @@ const showLoginPopups = () => {
return false; return false;
}, },
}); });
} else {
window.location.href = instance._extend.loginUrl + '?url=' + window.btoa(href);
} }
}; }, 100);
// http request 拦截器 // http request 拦截器
instance.interceptors.request.use( instance.interceptors.request.use(
...@@ -197,6 +220,8 @@ instance.interceptors.response.use( ...@@ -197,6 +220,8 @@ instance.interceptors.response.use(
const codeArr = [0, '0', 2000]; const codeArr = [0, '0', 2000];
const code = response.data && response.data.code; const code = response.data && response.data.code;
if (response.data && ((code === '0000' && response.data.businessCode === '0000') || codeArr.includes(code))) { if (response.data && ((code === '0000' && response.data.businessCode === '0000') || codeArr.includes(code))) {
// 记录最后一次请求时间
refreshTokenTimeOut();
if (instance._extend.dataParseMode === 'NOT_PARSE') { if (instance._extend.dataParseMode === 'NOT_PARSE') {
return response.data; return response.data;
} }
...@@ -211,7 +236,7 @@ instance.interceptors.response.use( ...@@ -211,7 +236,7 @@ instance.interceptors.response.use(
// token过期; // token过期;
return refreshRequest(response.config); return refreshRequest(response.config);
} }
if (+code === 4010 || +code === 4011) { if (+code === 4010 || +code === 4011 || (code === '0001' && response.url.indexOf('oauth/refreshtoken') > -1)) {
showLoginPopups(); showLoginPopups();
return; return;
} }
......
/**
* 防抖函数
* @param {Function} fn 需要防抖的函数
* @param {Number} interval 防抖时间间隔
* @param {Boolean} immediate 是否立即执行
* **/
export function debounce(fn, interval, immediate) {
let flag = true;
const time = interval || 1000;
let timer = null;
return function() {
if (!immediate) {
timer && clearTimeout(timer);
timer = setTimeout(() => {
fn.apply(this, arguments);
}, time);
} else {
if (flag) {
fn.apply(this, arguments);
flag = false;
return;
}
timer && clearTimeout(timer);
timer = setTimeout(() => {
fn.apply(this, arguments);
}, time);
}
};
}
\ No newline at end of file
{ {
"name": "@qg/sys-sdk", "name": "@qg/sys-sdk",
"version": "1.0.21", "version": "1.0.23",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
......
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