From a9e4b6766f83af41a7e33f2e473de33ec21b4453 Mon Sep 17 00:00:00 2001 From: bb_pan Date: Fri, 8 Aug 2025 10:28:02 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AD=BE=E5=90=8D=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/my/signature.vue | 18 +++++++++----- src/utils/bnsBase.js | 50 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 src/utils/bnsBase.js diff --git a/src/pages/my/signature.vue b/src/pages/my/signature.vue index 70eb1ac..7d47bae 100644 --- a/src/pages/my/signature.vue +++ b/src/pages/my/signature.vue @@ -26,6 +26,7 @@ import { } from '@/services/signature.js' import { toolsLeaseApplyInfoSign, updateLeaseApplyInfoSignApi } from '@/services/materialsStation' import { baseURL } from '@/utils/http' +import { uploadImgSignUrl } from '@/utils/bnsBase' let isCanvas = ref(false) let imgPath = ref('') @@ -38,7 +39,7 @@ onLoad((opt) => { // console.log('🚀 ~ onLoad ~ opt:', opt) const params = opt.params ? JSON.parse(opt.params) : {} Object.assign(opts, params) - if (opts.isLease || opts.isToolsLease) { + if (opts.isLease) { signType.value = opts.leaseSignType imgPath.value = opts.leaseSignUrl } @@ -112,7 +113,10 @@ const complete = (e) => { .replace(/^data:image\/(jpeg|png|webp);base64,/, '') // console.log('🚀 ~ WebP base64:', webpBase64) - uploadSignUrl(webpBase64) + uploadImgSignUrl(webpBase64).then(item => { + console.log('🚀 ~ complete ~ item:', item) + uploadSignUrl(item.url) + }) } } @@ -182,14 +186,16 @@ const onCameraError = (message) => { const onCameraSuccess = (file) => { isRotate.value = false signType.value = 1 - const signUrl = file - uploadSignUrl(signUrl) + uploadImgSignUrl(file).then(item => { + console.log('🚀 ~ complete ~ item:', item) + uploadSignUrl(item.url) + }) } // 上传 -const uploadSignUrl = async (base64Data) => { +const uploadSignUrl = async (url) => { try { - const signUrl = base64Data + const signUrl = url if (opts.isLease) { console.log('🚀 ~ uploadSignUrl ~ opts:', opts) const params = { diff --git a/src/utils/bnsBase.js b/src/utils/bnsBase.js new file mode 100644 index 0000000..6b9e826 --- /dev/null +++ b/src/utils/bnsBase.js @@ -0,0 +1,50 @@ +import { decryptWithSM4 } from './sm' + +const generateRandomString = (length) => { + const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' + return Array.from({ length }, () => chars.charAt(Math.floor(Math.random() * chars.length))).join('') +} + +export const uploadImgSignUrl = (file) => { + return new Promise((resolve, reject) => { + uni.request({ + url: '/file/uploadBase64', + method: 'POST', + data: { + base64File: file, + fileName: `${generateRandomString(10)}_${Date.now()}.png`, + fileType: 'image/png', + }, + success: (uploadRes) => { + try { + let result + if (typeof uploadRes.data === 'string') { + result = JSON.parse(decryptWithSM4(uploadRes.data)) + } else { + result = uploadRes.data + } + + if (result.code === 200) { + uni.showToast({ title: '上传成功', icon: 'none' }) + resolve({ + name: result.data.name, + url: result.data.url, + }) + } else { + uni.showToast({ title: '上传失败', icon: 'none' }) + reject(new Error(result.message || '上传失败')) + } + } catch (e) { + console.error('处理返回数据出错', e) + uni.showToast({ title: '上传失败', icon: 'none' }) + reject(e) + } + }, + fail: (err) => { + console.error('上传失败', err) + uni.showToast({ title: '上传失败', icon: 'none' }) + reject(err) + }, + }) + }) +}