ソースを参照

fix(useUpload): 优化文件选择逻辑,支持H5和小程序环境,增强错误处理

feige996 11 ヶ月 前
コミット
b5a71b7788
2 ファイル変更32 行追加18 行削除
  1. 27 16
      src/hooks/useUpload.ts
  2. 5 2
      src/pages/mine/index.vue

+ 27 - 16
src/hooks/useUpload.ts

@@ -36,7 +36,21 @@ export default function useUpload<T extends TfileType>(options: TOptions<T> = {}
     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<T extends TfileType>(options: TOptions<T> = {}
     }
   }
 
-  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<T extends TfileType>(options: TOptions<T> = {}
       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

+ 5 - 2
src/pages/mine/index.vue

@@ -134,10 +134,13 @@ const onChooseAvatar = (e: any) => {
   console.log('选择头像', e.detail)
   const { avatarUrl } = e.detail
   const { run } = useUpload<IUploadSuccessInfo>(
-    uploadFileUrl.USER_AVATAR,
+    import.meta.env.VITE_UPLOAD_BASEURL,
     {},
     {
-      onSuccess: (res) => useUserStore().getUserInfo(),
+      onSuccess: (res) => {
+        console.log('头像上传成功', res)
+        // 更新用户信息
+      },
     },
     avatarUrl,
   )