From 6c958914766b327ac0f90005d29e4a46abc0484d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=A2=E8=B6=8A?= <552369664@qq.com> Date: Wed, 25 Sep 2024 08:25:20 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=8A=9F=E8=83=BD=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E3=80=91=E7=AE=80=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../s-uploader/choose-and-upload-file.js | 37 +++---------------- 1 file changed, 6 insertions(+), 31 deletions(-) diff --git a/sheep/components/s-uploader/choose-and-upload-file.js b/sheep/components/s-uploader/choose-and-upload-file.js index a92e0ace..18f0487a 100644 --- a/sheep/components/s-uploader/choose-and-upload-file.js +++ b/sheep/components/s-uploader/choose-and-upload-file.js @@ -206,12 +206,10 @@ async function uploadFiles(choosePromise, { onChooseFile, onUploadProgress }) { // 如果是前端直连上传 if (UPLOAD_TYPE.CLIENT === import.meta.env.SHOPRO_UPLOAD_TYPE) { for (const file of files) { - // 获取二进制文件对象 - const fileBuffer = await convertToArrayBuffer(file); - // 1.1 生成文件名称 - const fileName = await generateFileName(fileBuffer, file.name); - // 1.2 获取文件预签名地址 + // 1.1 获取文件预签名地址 const { data: presignedInfo } = await FileApi.getFilePresignedUrl(file.name); + // 1.2 获取二进制文件对象 + const fileBuffer = await convertToArrayBuffer(file); // 1.3 上传文件 await uni.request({ url: presignedInfo.uploadUrl, // 预签名的上传 URL @@ -222,10 +220,9 @@ async function uploadFiles(choosePromise, { onChooseFile, onUploadProgress }) { data: fileBuffer, // 文件的路径,适用于小程序 success: (res) => { // 1.4. 记录文件信息到后端(异步) - createFile(presignedInfo, fileName, file); + createFile(presignedInfo, file); // 1.5. 重新赋值 file.url = presignedInfo.url; - file.name = fileName; console.log('上传成功:', res); }, fail: (err) => { @@ -258,38 +255,16 @@ function chooseAndUploadFile( return uploadFiles(chooseAll(opts), opts); } -/** - * 生成文件名称(使用算法SHA256) - * @param arrayBuffer 二进制文件对象 - * @param fileName 文件名称 - */ -async function generateFileName(arrayBuffer, fileName) { - return new Promise((resolve, reject) => { - try { - // 创建 WordArray - const wordArray = CryptoJS.lib.WordArray.create(new Uint8Array(arrayBuffer)); - // 计算SHA256 - const sha256 = CryptoJS.SHA256(wordArray).toString(); - // 拼接后缀 - const ext = fileName.substring(fileName.lastIndexOf('.')); - resolve(`${sha256}${ext}`); - } catch (error) { - reject(new Error('计算SHA256失败: ' + error.message)); - } - }); -} - /** * 创建文件信息 * @param vo 文件预签名信息 - * @param name 文件名称 * @param file 文件 */ -function createFile(vo, name, file) { +function createFile(vo, file) { const fileVo = { configId: vo.configId, url: vo.url, - path: name, + path: file.name, name: file.name, type: file.fileType, size: file.size,