diff --git a/sysom_web/src/app.jsx b/sysom_web/src/app.jsx index d8c83ae390cc6ed043c7e56c1d1f6fef2bd7be3d..c8ba62ffb6d75fe48576a2d0bd509c0cfca2ba3f 100644 --- a/sysom_web/src/app.jsx +++ b/sysom_web/src/app.jsx @@ -5,6 +5,7 @@ import RightContent from '@/components/RightContent'; import Footer from '@/components/Footer'; import { currentUser as queryCurrentUser } from './pages/user/Login/service'; import { message } from 'antd'; +import OvertimeLogin from "./overtime"; const loginPath = '/user/login'; const noNeedLoginRoutes = [ @@ -47,7 +48,9 @@ export const initialStateConfig = { * @see https://umijs.org/zh-CN/plugins/plugin-initial-state * */ -export async function getInitialState() { +export async function getInitialState () { + //windows上设置一个循环定时器,每隔一秒调用一次监听函数,并定义在全局global上,用于超时后清除 + global.overtime = window.setInterval(OvertimeLogin(), 1000); const fetchUserInfo = async (userId, token) => { try { const msg = await queryCurrentUser(userId, token); diff --git a/sysom_web/src/overtime.js b/sysom_web/src/overtime.js new file mode 100644 index 0000000000000000000000000000000000000000..61baa5b642e2290221fd4d38f4f3d9d85ad88a1d --- /dev/null +++ b/sysom_web/src/overtime.js @@ -0,0 +1,80 @@ +import { history, request } from 'umi'; +import { stringify } from 'querystring'; +import { message, } from "antd"; +let timeOut = 60 * 30 * 1000;//30 minutes +let lastTime = new Date().getTime(); +let currentTime = new Date().getTime(); +//获取最外层div +let ICE_CONTAINER = document.getElementById('root'); +// timeType 判断页面是否为登录页,如果是不执行踢出登录操作,初始状态为false +let timeType = false; + +/* +用户注销接口 +*/ +export async function logout(){ + const token = localStorage.getItem('token'); + return await request('/api/v1/logout/', { + method: 'GET', + headers: { + 'Authorization': token, + }, + }); +}; + +/** + * 退出登录,并且将当前的 url 保存 + */ +const loginOut = async () => { + const { query = {}, search, pathname } = history.location; + const { redirect } = query; // Note: There may be security issues, please note + if (window.location.pathname !== '/user/login' && !redirect) { + history.replace({ + pathname: '/user/login', + search: stringify({ + redirect: pathname + search, + }), + }); + } +}; + +const OvertimeLogin = () => { + let testTime=()=> {//定时器 + /* 检测鼠标移动事件 */ + ICE_CONTAINER.addEventListener('mousemove', function () { + // 更新最后的操作时间(鼠标每移动一次,就会重新计算时间) + lastTime = new Date().getTime(); + }) + /* 检测键盘操作事件 */ + ICE_CONTAINER.addEventListener('keydown', function() { + // 更新最后的操作时间(按键每按一次,就会重新计算时间) + lastTime = new Date().getTime(); + }) + //获取当前路径 是否在登录页 还在主页即为true + timeType = window.location.href.indexOf('login') < 0; + //更新当前时间 + currentTime = new Date().getTime(); + //判断是否超时 + if (currentTime - lastTime > timeOut) { + // 超时操作 + if (timeType) { + // 清除定时器 + window.clearInterval(global.overtime) + logout().then(( res ) => { + if (res.code === 200) { + message.error('长时间未操作,请重新登陆') + localStorage.removeItem('userId'); + localStorage.removeItem('token'); + loginOut(); + } + }) + lastTime = new Date().getTime(); + } else { + lastTime = new Date().getTime(); + } + } + } + return testTime +} + +export default OvertimeLogin \ No newline at end of file