SignDialog.vue 2.7 KB

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