当前位置:首页 > VUE

vue实现签章

2026-01-12 22:12:54VUE

Vue 实现电子签章功能

电子签章功能通常包含绘制签名、保存签名图片、验证签名等环节。以下是基于 Vue 的实现方案:

使用 canvas 绘制签名

创建一个 Vue 组件,利用 HTML5 的 canvas 元素实现手写签名:

<template>
  <div class="signature-pad">
    <canvas 
      ref="canvas"
      @mousedown="startDrawing"
      @mousemove="draw"
      @mouseup="stopDrawing"
      @mouseleave="stopDrawing"
      @touchstart="startDrawing"
      @touchmove="draw"
      @touchend="stopDrawing">
    </canvas>
    <button @click="clearSignature">清除</button>
    <button @click="saveSignature">保存</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isDrawing: false,
      lastX: 0,
      lastY: 0
    }
  },
  mounted() {
    this.resizeCanvas()
    window.addEventListener('resize', this.resizeCanvas)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resizeCanvas)
  },
  methods: {
    resizeCanvas() {
      const canvas = this.$refs.canvas
      canvas.width = canvas.offsetWidth
      canvas.height = canvas.offsetHeight
    },
    startDrawing(e) {
      this.isDrawing = true
      const canvas = this.$refs.canvas
      const rect = canvas.getBoundingClientRect()
      this.lastX = (e.clientX || e.touches[0].clientX) - rect.left
      this.lastY = (e.clientY || e.touches[0].clientY) - rect.top
    },
    draw(e) {
      if (!this.isDrawing) return
      const canvas = this.$refs.canvas
      const ctx = canvas.getContext('2d')
      const rect = canvas.getBoundingClientRect()
      const currentX = (e.clientX || e.touches[0].clientX) - rect.left
      const currentY = (e.clientY || e.touches[0].clientY) - rect.top

      ctx.beginPath()
      ctx.moveTo(this.lastX, this.lastY)
      ctx.lineTo(currentX, currentY)
      ctx.strokeStyle = '#000'
      ctx.lineWidth = 2
      ctx.lineCap = 'round'
      ctx.lineJoin = 'round'
      ctx.stroke()

      this.lastX = currentX
      this.lastY = currentY
    },
    stopDrawing() {
      this.isDrawing = false
    },
    clearSignature() {
      const canvas = this.$refs.canvas
      const ctx = canvas.getContext('2d')
      ctx.clearRect(0, 0, canvas.width, canvas.height)
    },
    saveSignature() {
      const canvas = this.$refs.canvas
      const image = canvas.toDataURL('image/png')
      this.$emit('save', image)
    }
  }
}
</script>

<style>
.signature-pad {
  width: 100%;
  max-width: 500px;
  border: 1px solid #ccc;
}
canvas {
  display: block;
  width: 100%;
  height: 200px;
  background-color: #fff;
}
button {
  margin-top: 10px;
  padding: 5px 10px;
}
</style>

签名图片处理

保存的签名图片是 base64 编码的字符串,可以上传到服务器或直接嵌入文档:

// 在父组件中处理保存的签名
handleSaveSignature(imageData) {
  // 上传到服务器
  axios.post('/api/signature', { image: imageData })
    .then(response => {
      console.log('签名保存成功')
    })

  // 或者直接使用
  this.signatureImage = imageData
}

使用第三方库简化实现

对于更复杂的需求,可以考虑使用现成的签名库:

  1. 安装 signature_pad 库:

    npm install signature_pad
  2. 在 Vue 组件中使用:

    
    import SignaturePad from 'signature_pad'

export default { mounted() { const canvas = this.$refs.canvas this.signaturePad = new SignaturePad(canvas, { backgroundColor: 'rgba(255, 255, 255, 0)', penColor: 'rgb(0, 0, 0)' }) }, methods: { clear() { this.signaturePad.clear() }, save() { if (this.signaturePad.isEmpty()) { alert('请先签名') return } const data = this.signaturePad.toDataURL('image/png') this.$emit('save', data) } } }


#### 签名验证功能

实现简单的签名验证可以通过比较签名图片的哈希值:

```javascript
// 生成签名哈希
async function generateSignatureHash(imageData) {
  const buffer = await fetch(imageData)
    .then(res => res.arrayBuffer())
  const hashBuffer = await crypto.subtle.digest('SHA-256', buffer)
  const hashArray = Array.from(new Uint8Array(hashBuffer))
  return hashArray.map(b => b.toString(16).padStart(2, '0')).join('')
}

// 比较两个签名
async function compareSignatures(sig1, sig2) {
  const hash1 = await generateSignatureHash(sig1)
  const hash2 = await generateSignatureHash(sig2)
  return hash1 === hash2
}

注意事项

  1. 移动端适配需要处理 touch 事件
  2. 考虑添加签名时间戳和数字证书增强安全性
  3. 重要合同签名建议结合后端验证和区块链存证
  4. 签名图片建议添加水印防止篡改

以上方案提供了从基础绘制到高级验证的完整电子签章实现路径,可根据实际需求调整功能细节。

vue实现签章

标签: 签章vue
分享给朋友:

相关文章

vue实现图片轮播

vue实现图片轮播

使用 Swiper 实现图片轮播 安装 Swiper 依赖 npm install swiper 在 Vue 组件中引入 Swiper <template> <div c…

vue实现添加用户

vue实现添加用户

Vue 实现添加用户功能 数据绑定与表单设计 在 Vue 中实现添加用户功能,首先需要设计一个表单,用于收集用户输入的数据。通过 v-model 实现双向数据绑定,确保表单数据与 Vue 实例中的数据…

vue实现多级表头

vue实现多级表头

Vue 实现多级表头的方法 使用 el-table 组件 Element UI 的 el-table 组件支持多级表头的实现,通过嵌套 el-table-column 即可完成。 <el-t…

vue登录逻辑的实现

vue登录逻辑的实现

Vue 登录逻辑实现 前端实现 创建登录组件 在 Vue 项目中创建一个登录组件,通常命名为 Login.vue。该组件包含表单元素,如用户名和密码输入框,以及提交按钮。 <template…

vue为啥能实现监听

vue为啥能实现监听

Vue 实现监听的原理 Vue 的监听能力主要基于其响应式系统,该系统通过数据劫持和依赖收集实现。当数据变化时,系统能自动触发视图更新或执行回调函数。 核心机制:Object.defineProp…

vue实现点击页面切换

vue实现点击页面切换

实现点击页面切换功能 在Vue中实现点击页面切换功能,可以通过路由跳转或组件动态渲染两种方式完成。以下是具体实现方法: 使用Vue Router实现页面跳转 安装Vue Router后,在项目中配置…