电子签名 vue-signature-pad

main.js

import VueSignature from 'vue-signature-pad'
Vue.use(VueSignature)

引用电子签名弹窗

<hand-painted-signature
        ref="handpaintedSignature"
        :caseId="caseId"
        @handleSignDialog="handleSignDialog"
        :signatureFlag="signatureFlag"
        :materialType="materialType"
      ></hand-painted-signature>

查询已经签过的名

querySign(){
      this.$API
        .Get('autograph/query', {})
        .then(res => {
           if (res.code !== 1) {
            this.$utils.message('error', res.msg)
            return
          }
          if (res.code === 1) {
            let data = res.data
            this.signatureFlag = true
            this.$nextTick(() => {
              this.$refs.handpaintedSignature.fromData(data[0])
            })
          }
        })
        .catch(err => {
          console.error('processForm--->queryBindSign()', err)
          this.$utils.message('error', this.$t('catchContent'))
        })
    },
    //手动操作签名dialog
    handleSignDialog(type) {
      this.signatureFlag = type
    },

电子签名弹窗

<template>
  <el-dialog
    class="sign"
    width="40%"
    :visible.sync="showSignature"
    :before-close="handleClose"
    title="签名"
  >
    <el-row style="border: 1px solid #ccc; margin-bottom: 18px">
      <el-col :span="24">
        <VueSignaturePad
          id="signature"
          width="100%"
          height="300px"
          ref="signaturePad"
          :options="options"
        ></VueSignaturePad>
      </el-col>
    </el-row>
    <el-row>
      <el-button
        style="color: #409eff; border-color: #c6e2ff; background-color: #ecf5ff"
        @click="clear"
      >
        重签
      </el-button>
      <el-button @click="save" :loading="loading" type="primary">确认</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
export default {
  name: 'handPaintedSignature',
  props: {
    caseId: {
      type: String,
    },
    signatureFlag: {
      type: Boolean,
    },
    materialType: {
      type: Number,
    },
  },
  data() {
    return {
      id: undefined,
      userId: undefined,
      options: {
        penColor: '#000',
      },
      signatureImaUrl: {
        fileUrl: '',
      },
      showSignature: false,
      loading: false,
    }
  },
  watch: {
    signatureFlag(val) {
      this.showSignature = val
    },
  },
  mounted() {},
  methods: {
    fromData(data) {
      this.id = data.id
      this.userId = data.userId
      let img = new Image()
      img.src = data.image
      let myCanvas = document.querySelector('#signature canvas').getContext('2d')
      //首次不显示  需要放到onload事件中
      img.onload = function () {
        myCanvas.drawImage(img, 0, 0)
      }
    },
    //需手动操作关闭dialog  再次打开dialog 层级有问题
    handleClose() {
      this.$refs.signaturePad.clearSignature()
      this.$emit('handleSignDialog', false)
    },
    clear() {
      this.$refs.signaturePad.clearSignature()
    },
    save() {
      if (this.$refs.signaturePad.isEmpty()) {
        this.$utils.message('error', '电子签名不能为空')
      } else {
        const { isEmpty, data } = this.$refs.signaturePad.saveSignature()
        this.updateFile(data)
      }
    },
    updateFile(data) {
      console.log(data)
      this.loading = true
      this.$API
        .Post('autograph/addOrUpdate', {
          // caseId: this.caseId,
          // image: data.replace(/^data:image\/\w+;base64,/, ''),
          id: this.id,
          userId: this.userId,
          image: data,
        })
        .then(res => {
          if (res.code === 1) {
            this.$utils.message('success', '保存成功')
            this.$emit('handleSignDialog', false)
            this.$refs.signaturePad.clearSignature()
          } else {
            this.$utils.message('error', '保存失败')
          }
          this.loading = false
        })
        .catch(error => {
          this.loading = false
        })
    },
  },
}
</script>

<style scoped>
/deep/.el-dialog__body {
  padding: 10px 20px 20px 20px;
}
.sign /deep/ .el-dialog__title {
  color: #000;
}
</style>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值