Просмотр исходного кода

提交api接口的超时登录

frank 3 лет назад
Родитель
Сommit
08a96f41db

+ 20 - 0
backendApi/modules/v1/controllers/BaseController.php

@@ -37,6 +37,26 @@ class BaseController extends \yii\rest\ActiveController {
      */
     public function beforeAction($action) {
         $parentBeforeAction = parent::beforeAction($action);
+
+        // 增加的判断用户登录的操作间隔是否大于十分钟
+        if (Yii::$app->getUser()->getUserInfo()){
+            $adminId = Yii::$app->getUser()->getUserInfo()['id'];
+            $redisAdminKey = 'admin-'.$adminId;
+            $lastTime = '';
+            if (!Yii::$app->tokenRedis->hget($redisAdminKey, 'lastTime')) {
+                $lastTime = time();
+            }else{
+                $lastTime = Yii::$app->tokenRedis->hget($redisAdminKey, 'lastTime');
+            }
+            $currentTime = time();
+            $timeOut = 10 * 60 ;
+            if ($currentTime - $lastTime > $timeOut) {
+                return self::notice('页面由于长时间未进行操纵需要重新登录', 403);
+            } else {
+                Yii::$app->tokenRedis->hset($redisAdminKey, 'lastTime', time());
+            }
+        }
+
         // 校验用户权限
         if (!Yii::$app->user->validateAdminAction($this->id, $this->action->id)) {
             return self::notice('权限不足', 403);

+ 7 - 0
backendApi/modules/v1/models/LoginForm.php

@@ -137,6 +137,13 @@ class LoginForm extends Model {
             $this->_updateSuccessTimes();
             $transaction->commit();
             AdminLoginLogger::success($this->_user);
+
+            // 新需求添加的删除用户token , 重新统计
+            $redisKey = 'admin-'.$this->_user->getId();
+            if (Yii::$app->tokenRedis->exists($redisKey)){
+                Yii::$app->tokenRedis->del($redisKey);
+            }
+
             return Yii::$app->user->loginWithUAndP($this->_user);
         }catch(\Exception $e){
             $transaction->rollBack();

+ 0 - 1
backendEle/src/main.js

@@ -8,7 +8,6 @@ import router from './router'
 // import axios from 'axios'
 import axios from './utils/axiosPlugin'
 import errorInfo from './utils/errorCode'
-import './utils/loginTimeOut'    // 增加的超时登录退出的js
 
 Vue.use(ElementUI)
 

+ 0 - 34
backendEle/src/utils/loginTimeOut.js

@@ -1,34 +0,0 @@
-var lastTime = new Date().getTime();     // 获取最开始的操作时间
-var currentTime = new Date().getTime();     // 获取当前的操作时间
-var timeOut = 10 * 60 * 1000;       // 设置超时时间 : 10分 * 60秒 * 1000毫秒 , 因为1秒等于1000毫秒
-
-// 刷新的时候 ,触发重新加载的事件来执行更新最开始的操作时间
-window.onload = function () {
-  window.document.onmousedown = function () {
-    localStorage.setItem("lastTime",new Date().getTime());
-  }
-};
-
-function checkTimeout() {
-  currentTime = new Date().getTime();      // 更新当前的操作时间
-  lastTime = localStorage.getItem("lastTime");      // 获取最开始的操作时间
-
-  // console.log('开始了');
-  // console.log('当前时间 '+currentTime);
-  // console.log('最开始的时间'+lastTime);
-  // console.log('时间差'+currentTime - lastTime);
-  // console.log('超时时间'+timeOut);
-  // console.log('token'+localStorage.getItem("accessToken"));
-
-  if (currentTime - lastTime > timeOut) {     // 把最开始的和当前的操作时间差 和 规定的超时时间做对比 ,来判断是否超时
-    localStorage.removeItem("accessToken");      // 移除 token
-    // console.log("超时");
-    var url = window.location.href;      // 剩下就是跳转到登录页面重新登录
-    var newUrl=url.match(/(\S*)#/)[1];
-
-    window.open(newUrl + '#/login','_self');
-  }
-}
-
-/* 定时器 间隔30秒检测是否长时间未操作页面 */
-window.setInterval(checkTimeout, 30000);

+ 20 - 0
frontendApi/modules/v1/controllers/BaseController.php

@@ -46,6 +46,26 @@ class BaseController extends \yii\rest\ActiveController {
      */
     public function beforeAction($action) {
         $this->forbiddenQuicklyUser();
+
+        // 增加的判断用户登录的操作间隔是否大于十分钟
+        if (Yii::$app->getUser()->getUserInfo()){
+            $adminId = Yii::$app->getUser()->getUserInfo()['id'];
+            $redisAdminKey = 'user-'.$adminId;
+            $lastTime = '';
+            if (!Yii::$app->tokenRedis->hget($redisAdminKey, 'lastTime')) {
+                $lastTime = time();
+            }else{
+                $lastTime = Yii::$app->tokenRedis->hget($redisAdminKey, 'lastTime');
+            }
+            $currentTime = time();
+            $timeOut = 10 * 60 ;
+            if ($currentTime - $lastTime > $timeOut) {
+                return self::notice('页面由于长时间未进行操纵需要重新登录', 403);
+            } else {
+                Yii::$app->tokenRedis->hset($redisAdminKey, 'lastTime', time());
+            }
+        }
+
         return parent::beforeAction($action);
     }
 

+ 7 - 0
frontendApi/modules/v1/models/LoginForm.php

@@ -169,6 +169,13 @@ class LoginForm extends Model
             $transaction->commit();
 
             UserLoginLogger::success($this->_userInfo);
+
+            // 新需求添加的删除用户token , 重新统计
+            $redisKey = 'user-'.$this->_userInfo['USER_ID'];
+            if (Yii::$app->tokenRedis->exists($redisKey)){
+                Yii::$app->tokenRedis->del($redisKey);
+            }
+
             return Yii::$app->user->loginWithUAndP($this->_user);
         }catch(\Exception $e){
             $transaction->rollBack();

+ 0 - 2
frontendEle/src/main.js

@@ -9,8 +9,6 @@ import router from './router'
 import axios from './utils/axiosPlugin'
 import errorInfo from './utils/errorCode'
 import webSocketService from './utils/websocket';
-import './utils/loginTimeOut';    // 增加的超时登录退出的js
-
 
 Vue.use(ElementUI)
 Vue.prototype.$webSocket = webSocketService;

+ 0 - 34
frontendEle/src/utils/loginTimeOut.js

@@ -1,34 +0,0 @@
-var lastTime = new Date().getTime();     // 获取最开始的操作时间
-var currentTime = new Date().getTime();     // 获取当前的操作时间
-var timeOut = 10 * 60 * 1000;       // 设置超时时间 : 10分 * 60秒 * 1000毫秒 , 因为1秒等于1000毫秒
-
-// 刷新的时候 ,触发重新加载的事件来执行更新最开始的操作时间
-window.onload = function () {
-  window.document.onmousedown = function () {
-    localStorage.setItem("lastTime",new Date().getTime());
-  }
-};
-
-function checkTimeout() {
-  currentTime = new Date().getTime();      // 更新当前的操作时间
-  lastTime = localStorage.getItem("lastTime");      // 获取最开始的操作时间
-
-  console.log('会员端的开始了');
-  console.log('会员端的当前时间 '+currentTime);
-  console.log('会员端的最开始的时间'+lastTime);
-  console.log('会员端的时间差'+currentTime - lastTime);
-  console.log('会员端的超时时间'+timeOut);
-  console.log('会员端的token'+localStorage.getItem("accessToken"));
-
-  if (currentTime - lastTime > timeOut) {
-    localStorage.removeItem("accessToken");
-    // console.log("超时");
-    var url = window.location.href;
-    var newUrl=url.match(/(\S*)#/)[1];
-
-    window.open(newUrl + '#/login','_self');
-  }
-}
-
-/* 定时器 间隔30秒检测是否长时间未操作页面 */
-window.setInterval(checkTimeout, 30000);