From ba4c7cc744d04270f2742a4636b36733ca978c8a Mon Sep 17 00:00:00 2001 From: bb_pan Date: Thu, 19 Jun 2025 10:55:32 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AD=BE=E5=90=8D=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/my/signature.vue | 89 +++++++++++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 2 deletions(-) diff --git a/src/pages/my/signature.vue b/src/pages/my/signature.vue index 0e8d921..bb6e5e1 100644 --- a/src/pages/my/signature.vue +++ b/src/pages/my/signature.vue @@ -91,11 +91,31 @@ const getSignData = () => { }) } +function base64ToBlob(base64Data) { + // 分离Base64的MIME类型和实际数据 + const parts = base64Data.split(';base64,'); + const contentType = parts[0].split(':')[1]; + const raw = window.atob(parts[1]); + const rawLength = raw.length; + const uInt8Array = new Uint8Array(rawLength); + + for (let i = 0; i < rawLength; ++i) { + uInt8Array[i] = raw.charCodeAt(i); + } + + return new Blob([uInt8Array], { type: contentType }); +} + +function blobToFile(blob, fileName) { + return new File([blob], fileName, { type: blob.type }); +} + + // 签名完成 const complete = (e) => { signType.value = 0 console.log(e) // 返回本地生成的base图片路径 - uploadImg(e) + uploadImg2(e) } // 打开签名 @@ -128,9 +148,74 @@ const handlePhoto = () => { }) } // 上传 +const uploadImg2 = (base64Data) => { + // 1. 将Base64转换为Blob + const blob = base64ToBlob(base64Data); + + // 2. 将Blob转换为File(uni-app的uploadFile也可以直接使用Blob) + const file = blobToFile(blob, 'image.png'); + console.log('🚀 ~ uploadImg ~ file:', file) + + // 3. 创建FormData(H5环境下需要) + const formData = new FormData(); + formData.append('file', file); + uni.uploadFile({ + url: '/file/upload', + // filePath: file, + name: 'file', + formData: formData, + header: { + // Authorization: this.token, + }, + success: async (res) => { + console.log('🚀 ~ uploadImg ~ res:', res) + try { + const signUrl = JSON.parse(res.data).data.url + if (opts.isLease) { + const params = { + id: opts.id, + leaseSignUrl: signUrl, + leaseSignType: signType.value, + } + console.log('🚀 ~ success: ~ params:', params) + const res = await updateLeaseApplyInfoSign(params) + console.log('🚀 ~ uploadImg-领料 ~ res:', res) + uni.navigateBack() + } else if (opts.isBack) { + const params = { + id: opts.id, + backSignUrl: signUrl, + backSignType: signType.value, + } + console.log('🚀 ~ success: ~ params:', params) + const res = await updateSignById(params) + console.log('🚀 ~ uploadImg-退料 ~ res:', res) + uni.navigateBack() + } else { + const params = { + signUrl: signUrl, + signType: signType.value, + } + const res = await updateSign(params) + console.log('🚀 ~ uploadImg-个人中心 ~ res:', res) + getSignData() + } + } catch (error) { + console.log('🚀 ~ uploadImg ~ error:', error) + uni.showToast({ + title: '上传失败', + icon: 'none', + }) + } + }, + fail: (err) => { + console.log('🚀 ~ uploadImg ~ err:', err) + }, + }) +} const uploadImg = (path) => { uni.uploadFile({ - url: actionUrl, + url: '/file/upload', filePath: path, name: 'file', header: {