实现检测用户一段时间内无操作,自动退出登录功能(网页端)

本项目使用vue-cookies存储用户信息(vue-session同理),在检测到用户无操作5分钟后调用logout函数进行用户登出操作。

部分函数使用Element-UI的接口,如this.$confirm等。

// App.vue
<template>
  <div id="app">
    <router-view/>
  </div>
</template>

<script>
export default {
  data() {
    return {
      logoutLimit: 1000 * 60 * 5, // 5分钟
    }
  },

  mounted() {
    // 超时登出
    let logoutTimer = setTimeout(this.logout, this.logoutLimit);
  
    let userOpDelay = () => {
      clearTimeout(logoutTimer);
      logoutTimer = setTimeout(this.logout, this.logoutLimit);
    }

    document.getElementById("app").addEventListener('keydown', function(e){ userOpDelay(); });
    document.getElementById("app").addEventListener('mousemove', function(e){ userOpDelay(); });
    document.getElementById("app").addEventListener('mousedown', function(e){ userOpDelay(); });
    document.getElementById("app").addEventListener('click', function(e){ userOpDelay(); });
    document.getElementById("app").addEventListener('scroll', function(e){ userOpDelay(); });
  },

  methods: {
    logout() {
      if (this.$cookies && this.$cookies.get("token") && this.$cookies.get("token") != "") {
        this.axios.get(`api/user/logout?token=${this.$cookies.get("token")}`).then(
          (res) => {
            let response = res.data;
            if (!response.errorCode) {
              this.$confirm('检测到最近无任何操作,账户自动登出。请重新登录!', '提示', {
                confirmButtonText: '确定',
                showCancelButton: false,
                type: 'warning'
              }).then(() => {
                this.$cookies.remove("token");
                this.$router.push({
                  name: "login"
                });
              }).catch(() => {
  
              }); 
            } else {
              this.$message({
                message: "登出失败",
                type: "error"
              });
            }
          }
        ).catch(
          (error) => {
            this.$message({
              message: error,
              type: "error"
            });
          }
        )
      }
    }
  }
}
</script>
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个基于 OpenCV 库的 C++ 代码,实现了多人的人脸检测自动取景功能: ```c++ #include <iostream> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; int main() { // 加载人脸检测器 CascadeClassifier face_cascade; face_cascade.load("haarcascade_frontalface_alt.xml"); // 打开摄像头 VideoCapture cap(0); if (!cap.isOpened()) { cerr << "Failed to open camera!" << endl; return -1; } while (true) { // 读取图像 Mat frame; cap >> frame; if (frame.empty()) { cerr << "Failed to read frame!" << endl; break; } // 转换为灰度图像 Mat gray; cvtColor(frame, gray, COLOR_BGR2GRAY); // 检测人脸 vector<Rect> faces; face_cascade.detectMultiScale(gray, faces); // 调整取景框 if (!faces.empty()) { int x = INT_MAX, y = INT_MAX, w = INT_MIN, h = INT_MIN; for (const auto& face : faces) { x = min(x, face.x); y = min(y, face.y); w = max(w, face.x + face.width); h = max(h, face.y + face.height); } int margin_x = w - x; int margin_y = h - y; int margin = max(margin_x, margin_y); int cx = x + margin_x / 2; int cy = y + margin_y / 2; int size = max(margin, max(frame.cols, frame.rows)); int x0 = cx - size / 2; int y0 = cy - size / 2; int x1 = cx + size / 2; int y1 = cy + size / 2; Rect roi(x0, y0, x1 - x0, y1 - y0); Mat roi_frame = frame(roi); // 显示图像 imshow("Face detection", roi_frame); } else { // 显示图像 imshow("Face detection", frame); } // 退出循环 if (waitKey(1) == 'q') { break; } } // 释放资源 cap.release(); destroyAllWindows(); return 0; } ``` 这段代码使用 CascadeClassifier 类加载已经训练好的人脸检测器,并使用 detectMultiScale 函数检测图像中的人脸。然后,根据检测到的人脸位置和数量,使用图像处理函数调整图像的取景框,并在窗口中显示调整后的图像。需要注意的是,这段代码只演示了如何对单张图像进行人脸检测自动取景,如果要对视频流进行处理,需要在循环中不断读取图像帧,并针对每帧进行处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZTao-z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值