From b5a71b7788be7c8928d865e4c8d0699d4eacbdcb Mon Sep 17 00:00:00 2001 From: feige996 <1020102647@qq.com> Date: Fri, 6 Jun 2025 22:31:10 +0800 Subject: [PATCH] =?UTF-8?q?fix(useUpload):=20=E4=BC=98=E5=8C=96=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E9=80=89=E6=8B=A9=E9=80=BB=E8=BE=91=EF=BC=8C=E6=94=AF?= =?UTF-8?q?=E6=8C=81H5=E5=92=8C=E5=B0=8F=E7=A8=8B=E5=BA=8F=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=EF=BC=8C=E5=A2=9E=E5=BC=BA=E9=94=99=E8=AF=AF=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useUpload.ts | 43 +++++++++++++++++++++++++--------------- src/pages/mine/index.vue | 7 +++++-- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/hooks/useUpload.ts b/src/hooks/useUpload.ts index 8a94ecd..7a45163 100644 --- a/src/hooks/useUpload.ts +++ b/src/hooks/useUpload.ts @@ -36,7 +36,21 @@ export default function useUpload(options: TOptions = {} const chooseFileOptions = { count: 1, success: (res: any) => { - handleFileChoose({ tempFilePath: res.tempFilePaths[0], tempFiles: res.tempFiles[0] }) + console.log('File selected successfully:', res) + // h5中res:{errMsg: "chooseImage:ok", tempFilePaths: "blob:http://localhost:9000/f74ab6b8-a14d-4cb6-a10d-fcf4511a0de5", tempFiles: [File]} + // h5的File有一下字段:{name: "girl.jpeg", size: 48976, type: "image/jpeg"} + // 小程序中res:{errMsg: "chooseImage:ok", tempFiles: [{fileType: "image", size: 48976, tempFilePath: "http://tmp/5iG1WpIxTaJf3ece38692a337dc06df7eb69ecb49c6b.jpeg"}]} + let tempFilePath = '' + let size = 0 + // #ifdef H5 + tempFilePath = res.tempFilePaths[0] + size = res.tempFiles[0].size + // #endif + // #ifdef MP-WEIXIN + tempFilePath = res.tempFiles[0].tempFilePath + size = res.tempFiles[0].size + // #endif + handleFileChoose({ tempFilePath, size }) }, fail: (err: any) => { console.error('File selection failed:', err) @@ -64,11 +78,8 @@ export default function useUpload(options: TOptions = {} } } - const handleFileChoose = (file: { - tempFilePath: string - tempFiles?: { size?: number; name?: string } - }) => { - if (file?.tempFiles?.size && file.tempFiles.size > maxSize) { + const handleFileChoose = ({ tempFilePath, size }: { tempFilePath: string; size: number }) => { + if (size > maxSize) { uni.showToast({ title: `文件大小不能超过 ${maxSize / 1024 / 1024}MB`, icon: 'none', @@ -76,20 +87,20 @@ export default function useUpload(options: TOptions = {} return } - const fileExtension = file?.tempFiles?.name?.split('.').pop()?.toLowerCase() - const isTypeValid = accept.some((type) => type === '*' || type.toLowerCase() === fileExtension) + // const fileExtension = file?.tempFiles?.name?.split('.').pop()?.toLowerCase() + // const isTypeValid = accept.some((type) => type === '*' || type.toLowerCase() === fileExtension) - if (!isTypeValid) { - uni.showToast({ - title: `仅支持 ${accept.join(', ')} 格式的文件`, - icon: 'none', - }) - return - } + // if (!isTypeValid) { + // uni.showToast({ + // title: `仅支持 ${accept.join(', ')} 格式的文件`, + // icon: 'none', + // }) + // return + // } loading.value = true uploadFile({ - tempFilePath: file.tempFilePath, + tempFilePath: tempFilePath, formData, onSuccess: (res) => { data.value = res diff --git a/src/pages/mine/index.vue b/src/pages/mine/index.vue index 714fcff..b9d9e69 100644 --- a/src/pages/mine/index.vue +++ b/src/pages/mine/index.vue @@ -134,10 +134,13 @@ const onChooseAvatar = (e: any) => { console.log('选择头像', e.detail) const { avatarUrl } = e.detail const { run } = useUpload( - uploadFileUrl.USER_AVATAR, + import.meta.env.VITE_UPLOAD_BASEURL, {}, { - onSuccess: (res) => useUserStore().getUserInfo(), + onSuccess: (res) => { + console.log('头像上传成功', res) + // 更新用户信息 + }, }, avatarUrl, )