From 843742d1cc4ab6dd34a92033a42d6fef78d86cbb Mon Sep 17 00:00:00 2001 From: taorui Date: Fri, 18 Oct 2024 10:06:57 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E9=95=BF=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E6=9C=AA=E6=93=8D=E4=BD=9C=E9=A1=B5=E9=9D=A2=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E9=80=80=E5=87=BA=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sysom_web/src/app.jsx | 5 ++- sysom_web/src/overtime.js | 80 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 sysom_web/src/overtime.js diff --git a/sysom_web/src/app.jsx b/sysom_web/src/app.jsx index d8c83ae3..c8ba62ff 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 00000000..61baa5b6 --- /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 -- Gitee