当前位置:首页 > 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 编码的字符串,可以上传到服务器或直接嵌入文档:

vue实现签章

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

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

使用第三方库简化实现

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

  1. 安装 signature_pad 库:

    vue实现签章

    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 表格实现方法 使用原生HTML表格 在Vue模板中直接使用HTML的<table>标签,结合v-for动态渲染数据: <template> <table&…

vue实现好评弹框

vue实现好评弹框

Vue 实现好评弹框的方法 使用 Vue 实现好评弹框可以通过组件化方式完成,结合动态数据绑定和事件处理。以下是具体实现步骤: 1. 创建弹框组件 新建一个 RatingDialog.vue…

vue实现查询替换

vue实现查询替换

Vue 实现查询替换功能 在 Vue 中实现查询替换功能,可以通过数据绑定和字符串操作方法结合实现。以下是具体实现方式: 基础实现 <template> <div>…

vue实现下拉层级

vue实现下拉层级

Vue实现下拉层级菜单 使用Vue实现下拉层级菜单可以通过递归组件或动态渲染的方式完成。以下是两种常见实现方法: 递归组件实现 创建一个递归组件处理多级菜单结构: <template>…

vue实现活动倒计时

vue实现活动倒计时

实现活动倒计时的基本思路 在Vue中实现活动倒计时通常需要获取当前时间与活动结束时间的差值,并将其转换为天、小时、分钟、秒的格式展示。通过定时器每秒更新显示的时间。 定义倒计时数据 在Vue组件的d…

用vue实现滑动输入条

用vue实现滑动输入条

使用 Vue 实现滑动输入条 使用原生 HTML5 input range 实现 在 Vue 模板中直接使用 <input type="range">,通过 v-model 绑定数据:…