当前位置:首页 > VUE

vue怎么实现手势密码

2026-01-23 00:21:23VUE

Vue 实现手势密码的方法

使用第三方库 vue-gesture-password

vue-gesture-password 是一个专门为 Vue 设计的轻量级手势密码组件,支持自定义样式和事件。

安装:

npm install vue-gesture-password --save

基本用法:

<template>
  <GesturePassword
    :width="300"
    :height="300"
    @complete="onComplete"
  />
</template>

<script>
import GesturePassword from 'vue-gesture-password'

export default {
  components: { GesturePassword },
  methods: {
    onComplete(password) {
      console.log('输入的密码:', password)
      // 验证密码逻辑
    }
  }
}
</script>

自定义实现手势密码

如果需要完全自定义实现,可以通过监听触摸事件和绘制逻辑来完成。

HTML 结构:

<template>
  <div class="gesture-container" ref="container">
    <canvas ref="canvas" @touchstart="onTouchStart" @touchmove="onTouchMove" @touchend="onTouchEnd"></canvas>
  </div>
</template>

核心逻辑:

<script>
export default {
  data() {
    return {
      points: [], // 所有点
      selectedPoints: [], // 选中的点
      isDrawing: false,
      ctx: null
    }
  },
  mounted() {
    this.initCanvas()
  },
  methods: {
    initCanvas() {
      const canvas = this.$refs.canvas
      this.ctx = canvas.getContext('2d')
      // 设置canvas尺寸
      canvas.width = this.$refs.container.offsetWidth
      canvas.height = this.$refs.container.offsetHeight
      // 初始化点
      this.initPoints()
    },
    initPoints() {
      // 创建3x3的点阵
      const radius = 10
      const spacing = (this.$refs.canvas.width - radius * 6) / 2
      for (let i = 0; i < 3; i++) {
        for (let j = 0; j < 3; j++) {
          this.points.push({
            x: radius + j * (radius * 2 + spacing),
            y: radius + i * (radius * 2 + spacing),
            radius,
            index: i * 3 + j
          })
        }
      }
      this.drawPoints()
    },
    drawPoints() {
      this.ctx.clearRect(0, 0, this.$refs.canvas.width, this.$refs.canvas.height)
      // 绘制所有点
      this.points.forEach(point => {
        this.ctx.beginPath()
        this.ctx.arc(point.x, point.y, point.radius, 0, Math.PI * 2)
        this.ctx.fillStyle = this.selectedPoints.includes(point) ? '#409EFF' : '#ddd'
        this.ctx.fill()
      })
      // 绘制连线
      if (this.selectedPoints.length > 1) {
        this.ctx.beginPath()
        this.ctx.moveTo(this.selectedPoints[0].x, this.selectedPoints[0].y)
        for (let i = 1; i < this.selectedPoints.length; i++) {
          this.ctx.lineTo(this.selectedPoints[i].x, this.selectedPoints[i].y)
        }
        this.ctx.strokeStyle = '#409EFF'
        this.ctx.lineWidth = 3
        this.ctx.stroke()
      }
    },
    onTouchStart(e) {
      this.isDrawing = true
      this.checkPoint(e.touches[0].clientX, e.touches[0].clientY)
    },
    onTouchMove(e) {
      if (!this.isDrawing) return
      this.checkPoint(e.touches[0].clientX, e.touches[0].clientY)
    },
    onTouchEnd() {
      this.isDrawing = false
      const password = this.selectedPoints.map(p => p.index).join('')
      this.$emit('complete', password)
      // 重置
      setTimeout(() => {
        this.selectedPoints = []
        this.drawPoints()
      }, 500)
    },
    checkPoint(x, y) {
      const rect = this.$refs.canvas.getBoundingClientRect()
      const touchX = x - rect.left
      const touchY = y - rect.top
      for (const point of this.points) {
        const distance = Math.sqrt(
          Math.pow(touchX - point.x, 2) + Math.pow(touchY - point.y, 2)
        )
        if (distance <= point.radius && !this.selectedPoints.includes(point)) {
          this.selectedPoints.push(point)
          this.drawPoints()
          break
        }
      }
    }
  }
}
</script>

样式优化

添加基础样式使组件更美观:

<style scoped>
.gesture-container {
  width: 100%;
  max-width: 300px;
  margin: 0 auto;
  background: #f5f5f5;
  border-radius: 8px;
  padding: 20px;
  box-sizing: border-box;
}
canvas {
  width: 100%;
  height: 300px;
  display: block;
  background: #fff;
  border-radius: 4px;
}
</style>

注意事项

  • 移动端需要添加 touch-action: none 样式防止页面滚动
  • 密码验证逻辑应该在父组件中实现
  • 可以通过 props 传入正确密码进行比对
  • 考虑添加错误提示和重试功能

vue怎么实现手势密码

标签: 手势密码
分享给朋友:

相关文章

js手势实现

js手势实现

手势识别实现方法 在JavaScript中实现手势识别通常涉及监听触摸事件(touchstart、touchmove、touchend)或鼠标事件(mousedown、mousemove、mouseu…

vue实现记住密码

vue实现记住密码

Vue 实现记住密码功能 在 Vue 中实现记住密码功能通常涉及前端表单处理、本地存储(如 localStorage 或 cookie)以及安全注意事项。以下是具体实现方法: 使用 localSto…

js实现密码

js实现密码

密码强度验证 使用正则表达式验证密码强度是一种常见方法。以下代码检查密码是否包含大小写字母、数字和特殊字符,且长度至少为8位: function checkPasswordStrength(pas…

php实现记住密码

php实现记住密码

实现记住密码功能的方法 使用PHP实现记住密码功能通常需要结合Cookie和Session技术。以下是几种常见的实现方式: 使用Cookie存储登录信息 在用户登录时创建一个长期有效的Cookie来…

vue 实现密码验证

vue 实现密码验证

密码验证的实现方法 在Vue中实现密码验证可以通过多种方式完成,以下是几种常见的实现方法: 使用Vuelidate进行验证 安装Vuelidate库: npm install vuelid…

vue实现手势返回

vue实现手势返回

Vue 实现手势返回功能 手势返回功能通常用于移动端应用,允许用户通过滑动屏幕边缘返回上一页。以下是几种实现方式: 使用 vue-touch 或 hammer.js 库 安装 vue-touch…