SignDialog.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <template>
  2. <el-dialog
  3. v-model="signDialogVisible"
  4. title="签名"
  5. width="935"
  6. >
  7. <div class="position-relative">
  8. <Vue3Signature class="b b-solid b-gray" ref="signature" w="900px" h="400px"/>
  9. <el-button
  10. style="position: absolute; bottom: 20px; right: 10px"
  11. type="primary"
  12. text
  13. size="small"
  14. @click="signature.clear()"
  15. >
  16. <Icon icon="ep:delete" class="mr-5px"/>
  17. 清除
  18. </el-button>
  19. </div>
  20. <template #footer>
  21. <div class="dialog-footer">
  22. <el-button @click="signDialogVisible = false">取消</el-button>
  23. <el-button type="primary" @click="submit">
  24. 提交
  25. </el-button>
  26. </div>
  27. </template>
  28. </el-dialog>
  29. </template>
  30. <script setup lang="ts">
  31. import Vue3Signature from "vue3-signature"
  32. import * as FileApi from '@/api/infra/file'
  33. const message = useMessage() // 消息弹窗
  34. const signDialogVisible = ref(false)
  35. const signature = ref()
  36. const open = async () => {
  37. signDialogVisible.value = true
  38. }
  39. defineExpose({open})
  40. const emits = defineEmits(['success'])
  41. const submit = async () => {
  42. message.success('签名上传中请稍等。。。')
  43. const res = await FileApi.updateFile({file: base64ToFile(signature.value.save('image/png'), '签名')})
  44. emits('success', res.data)
  45. signDialogVisible.value = false
  46. }
  47. const base64ToFile = (base64, fileName) => {
  48. // 将base64按照 , 进行分割 将前缀 与后续内容分隔开
  49. let data = base64.split(',');
  50. // 利用正则表达式 从前缀中获取图片的类型信息(image/png、image/jpeg、image/webp等)
  51. let type = data[0].match(/:(.*?);/)[1];
  52. // 从图片的类型信息中 获取具体的文件格式后缀(png、jpeg、webp)
  53. let suffix = type.split('/')[1];
  54. // 使用atob()对base64数据进行解码 结果是一个文件数据流 以字符串的格式输出
  55. const bstr = window.atob(data[1]);
  56. // 获取解码结果字符串的长度
  57. let n = bstr.length
  58. // 根据解码结果字符串的长度创建一个等长的整形数字数组
  59. // 但在创建时 所有元素初始值都为 0
  60. const u8arr = new Uint8Array(n)
  61. // 将整形数组的每个元素填充为解码结果字符串对应位置字符的UTF-16 编码单元
  62. while (n--) {
  63. // charCodeAt():获取给定索引处字符对应的 UTF-16 代码单元
  64. u8arr[n] = bstr.charCodeAt(n)
  65. }
  66. // 利用构造函数创建File文件对象
  67. // new File(bits, name, options)
  68. const file = new File([u8arr], `${fileName}.${suffix}`, {
  69. type: type
  70. })
  71. // 将File文件对象返回给方法的调用者
  72. return file;
  73. }
  74. </script>
  75. <style scoped>
  76. </style>