80 lines
2.7 KiB
Vue
Raw Normal View History

2025-01-09 13:50:08 +08:00
<template>
2025-01-16 13:01:06 +08:00
<el-dialog v-model="signDialogVisible" title="签名" width="935">
2025-01-09 13:50:08 +08:00
<div class="position-relative">
2025-01-16 13:01:06 +08:00
<Vue3Signature class="b b-solid b-gray" ref="signature" w="900px" h="400px" />
<!-- @lesan建议改成 unocss -->
2025-01-09 13:50:08 +08:00
<el-button
style="position: absolute; bottom: 20px; right: 10px"
type="primary"
text
size="small"
@click="signature.clear()"
>
2025-01-16 13:01:06 +08:00
<Icon icon="ep:delete" class="mr-5px" />
2025-01-09 13:50:08 +08:00
清除
</el-button>
</div>
<template #footer>
<div class="dialog-footer">
<el-button @click="signDialogVisible = false">取消</el-button>
2025-01-16 13:01:06 +08:00
<el-button type="primary" @click="submit"> 提交 </el-button>
2025-01-09 13:50:08 +08:00
</div>
</template>
</el-dialog>
</template>
<script setup lang="ts">
2025-01-16 13:01:06 +08:00
import Vue3Signature from 'vue3-signature'
2025-01-09 13:50:08 +08:00
import * as FileApi from '@/api/infra/file'
const message = useMessage() // 消息弹窗
const signDialogVisible = ref(false)
const signature = ref()
const open = async () => {
signDialogVisible.value = true
}
2025-01-16 13:01:06 +08:00
defineExpose({ open })
2025-01-09 13:50:08 +08:00
const emits = defineEmits(['success'])
const submit = async () => {
message.success('签名上传中请稍等。。。')
2025-01-16 13:01:06 +08:00
const res = await FileApi.updateFile({
file: base64ToFile(signature.value.save('image/png'), '签名')
})
2025-01-09 13:50:08 +08:00
emits('success', res.data)
signDialogVisible.value = false
}
2025-01-16 13:01:06 +08:00
// TODO @lesan这个要不抽到 download.js 里,让这个组件更简洁干净?
2025-01-09 13:50:08 +08:00
const base64ToFile = (base64, fileName) => {
// 将base64按照 , 进行分割 将前缀 与后续内容分隔开
2025-01-16 13:01:06 +08:00
let data = base64.split(',')
2025-01-09 13:50:08 +08:00
// 利用正则表达式 从前缀中获取图片的类型信息image/png、image/jpeg、image/webp等
2025-01-16 13:01:06 +08:00
let type = data[0].match(/:(.*?);/)[1]
2025-01-09 13:50:08 +08:00
// 从图片的类型信息中 获取具体的文件格式后缀png、jpeg、webp
2025-01-16 13:01:06 +08:00
let suffix = type.split('/')[1]
2025-01-09 13:50:08 +08:00
// 使用atob()对base64数据进行解码 结果是一个文件数据流 以字符串的格式输出
2025-01-16 13:01:06 +08:00
const bstr = window.atob(data[1])
2025-01-09 13:50:08 +08:00
// 获取解码结果字符串的长度
let n = bstr.length
// 根据解码结果字符串的长度创建一个等长的整形数字数组
// 但在创建时 所有元素初始值都为 0
const u8arr = new Uint8Array(n)
// 将整形数组的每个元素填充为解码结果字符串对应位置字符的UTF-16 编码单元
while (n--) {
// charCodeAt():获取给定索引处字符对应的 UTF-16 代码单元
u8arr[n] = bstr.charCodeAt(n)
}
// 利用构造函数创建File文件对象
// new File(bits, name, options)
const file = new File([u8arr], `${fileName}.${suffix}`, {
type: type
})
// 将File文件对象返回给方法的调用者
2025-01-16 13:01:06 +08:00
return file
2025-01-09 13:50:08 +08:00
}
</script>
2025-01-16 13:01:06 +08:00
<style scoped></style>