Commit 8c7bdd80 authored by guang.wu's avatar guang.wu

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

parent aa8dc4c5
import axios from 'axios';
import localStorage from './localStorage';
import Bus from './bus';
import { debounce } from './utils';
let CancelToken = axios.CancelToken; // axios中断取消请求
let httpQueue = []; // 请求队列
......@@ -111,8 +112,32 @@ const openWindow = url => {
return false;
};
const showLoginPopups = () => {
if (instance._extend.modal) {
/**
* 验证token是否超过5小时
* **/
const checkTokenTimeOut5Hours = () => {
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(() => {
if (checkTokenTimeOut5Hours() || !instance._extend.modal) {
window.location.href = instance._extend.loginUrl + '?url=' + window.btoa(href);
} else {
let href = window.location.href;
href = href.replace(/\?token=[a-z0-9\-A-Z]+/g, '');
href = instance._extend.loginUrl + '?url=' + window.btoa(href);
......@@ -130,10 +155,8 @@ const showLoginPopups = () => {
return false;
},
});
} else {
window.location.href = instance._extend.loginUrl + '?url=' + window.btoa(href);
}
};
}, 100);
// http request 拦截器
instance.interceptors.request.use(
......@@ -197,6 +220,8 @@ instance.interceptors.response.use(
const codeArr = [0, '0', 2000];
const code = response.data && response.data.code;
if (response.data && ((code === '0000' && response.data.businessCode === '0000') || codeArr.includes(code))) {
// 记录最后一次请求时间
refreshTokenTimeOut();
if (instance._extend.dataParseMode === 'NOT_PARSE') {
return response.data;
}
......
/**
* 防抖函数
* @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",
"version": "1.0.21",
"version": "1.0.22",
"description": "",
"main": "index.js",
"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