当前位置:首页 > VUE

vue实现锁屏

2026-03-10 08:33:20VUE

Vue 实现锁屏功能

锁屏功能通常用于在特定条件下阻止用户与页面交互,常见于后台管理系统或需要临时阻止操作的应用场景。以下是基于 Vue 的实现方案:

锁屏组件实现

创建锁屏组件 LockScreen.vue,包含蒙层和密码输入框:

<template>
  <div v-if="isLocked" class="lock-screen">
    <div class="lock-mask"></div>
    <div class="lock-content">
      <h3>系统已锁定</h3>
      <input 
        v-model="password" 
        type="password" 
        placeholder="请输入解锁密码"
        @keyup.enter="unlock"
      />
      <button @click="unlock">解锁</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isLocked: false,
      password: ''
    }
  },
  methods: {
    lock() {
      this.isLocked = true
      this.password = ''
    },
    unlock() {
      if (this.password === '预设密码') {
        this.isLocked = false
        this.$emit('unlocked')
      } else {
        alert('密码错误')
      }
    }
  }
}
</script>

<style scoped>
.lock-screen {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 9999;
}

.lock-mask {
  position: absolute;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
}

.lock-content {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: white;
  padding: 20px;
  border-radius: 5px;
  text-align: center;
}
</style>

全局状态管理

使用 Vuex 管理锁屏状态:

vue实现锁屏

// store/modules/lock.js
export default {
  state: {
    isLocked: false
  },
  mutations: {
    SET_LOCK(state, status) {
      state.isLocked = status
    }
  },
  actions: {
    lock({ commit }) {
      commit('SET_LOCK', true)
    },
    unlock({ commit }) {
      commit('SET_LOCK', false)
    }
  }
}

自动锁屏功能

实现无操作自动锁屏逻辑:

// utils/autoLock.js
let timer = null
const timeout = 30 * 60 * 1000 // 30分钟

export function setupAutoLock(store) {
  window.addEventListener('mousemove', resetTimer)
  window.addEventListener('keypress', resetTimer)

  function resetTimer() {
    clearTimeout(timer)
    timer = setTimeout(() => {
      store.dispatch('lock/lock')
    }, timeout)
  }

  resetTimer()
}

路由守卫控制

在路由跳转时检查锁屏状态:

vue实现锁屏

// router/index.js
router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth && store.state.lock.isLocked) {
    next({ path: '/unlock' })
  } else {
    next()
  }
})

实际应用示例

在 App.vue 中使用锁屏组件:

<template>
  <div id="app">
    <router-view/>
    <lock-screen 
      v-if="$store.state.lock.isLocked"
      @unlocked="$store.dispatch('lock/unlock')"
    />
  </div>
</template>

<script>
import LockScreen from '@/components/LockScreen'
import { setupAutoLock } from '@/utils/autoLock'

export default {
  components: { LockScreen },
  created() {
    setupAutoLock(this.$store)
  }
}
</script>

增强安全性

  1. 密码加密存储,避免明文保存
  2. 添加错误次数限制,防止暴力破解
  3. 记录锁屏日志,包括锁定时间、解锁尝试等
  4. 考虑添加二次验证机制

移动端适配

针对移动设备添加触摸事件支持:

window.addEventListener('touchstart', resetTimer)

调整锁屏组件样式,确保在移动设备上正常显示:

@media (max-width: 768px) {
  .lock-content {
    width: 80%;
  }
}

以上方案提供了完整的锁屏功能实现,可根据实际需求调整超时时间、密码验证方式等细节。

标签: vue
分享给朋友:

相关文章

vue表单实现搜索

vue表单实现搜索

实现Vue表单搜索功能 在Vue中实现表单搜索功能通常涉及表单绑定、事件处理和搜索逻辑。以下是具体实现方法: 数据绑定与表单结构 使用v-model实现表单与Vue数据的双向绑定: <tem…

vue实现微博发布动态

vue实现微博发布动态

使用Vue实现微博发布动态功能 创建Vue组件结构 新建一个WeiboPost.vue组件,包含文本框、图片上传和发布按钮: <template> <div class="we…

vue实现键盘

vue实现键盘

Vue 实现键盘功能 在Vue中实现键盘功能通常涉及监听键盘事件、处理输入以及渲染虚拟键盘界面。以下是几种常见的实现方式: 监听键盘事件 通过@keydown或@keyup指令监听键盘事件,适用于需…

vue 实现视频

vue 实现视频

Vue 实现视频播放功能 使用 Vue 实现视频播放功能可以通过 HTML5 的 <video> 标签或第三方库(如 video.js)来实现。以下是两种常见的方法: 使用 HTML5…

vue实现动画

vue实现动画

Vue 实现动画的方法 Vue 提供了多种方式来实现动画效果,包括内置的过渡系统、第三方动画库以及直接操作 CSS 或 JavaScript 动画。 使用 Vue 内置过渡系统 Vue 的 <…

vue实现开关

vue实现开关

Vue 实现开关组件的方法 使用原生HTML和Vue数据绑定 通过v-model绑定复选框的checked状态,结合CSS样式实现开关效果: <template> <label…