Pārlūkot izejas kodu

修改返回错误

lex 1 gadu atpakaļ
vecāks
revīzija
98613319d3
2 mainītis faili ar 37 papildinājumiem un 33 dzēšanām
  1. 21 30
      src/components/upload-file/index.tsx
  2. 16 3
      src/helpers/oss-file-upload.ts

+ 21 - 30
src/components/upload-file/index.tsx

@@ -10,7 +10,11 @@ import { defineComponent, watch, PropType, reactive, ref } from 'vue';
 import { policy } from './api';
 import Copper from './copper';
 import axios from 'axios';
-import { getUploadSign, onFileUpload } from '/src/helpers/oss-file-upload';
+import {
+  getUploadSign,
+  onFileUpload,
+  onOnlyFileUpload
+} from '/src/helpers/oss-file-upload';
 
 export default defineComponent({
   name: 'upload-file',
@@ -317,38 +321,25 @@ export default defineComponent({
             unknowValueField: []
           }
         };
-        const { data } = await policy(obj);
-
-        state.policy = data.policy;
-        state.signature = data.signature;
-        state.key = fileName;
-        state.KSSAccessKeyId = data.kssAccessKeyId;
-        state.name = fileName;
-
-        const formData = new FormData();
-        for (const key in state) {
-          formData.append(key, state[key]);
-        }
-        formData.append('file', blob);
+        // const { data } = await policy(obj);
+        const { data } = await getUploadSign(obj);
+        const formData = {
+          policy: data.policy,
+          signature: data.signature,
+          acl: 'public-read',
+          key: fileName,
+          KSSAccessKeyId: data.kssAccessKeyId,
+          name: fileName,
+          file: blob
+        };
 
-        await axios.post(ossUploadUrl, formData).then(() => {
-          const url = ossUploadUrl + state.key;
-          console.log(url, 'url');
-          const splitName = url.split('/');
-          fileListRef.value = [
-            {
-              id: new Date().getTime().toString(),
-              name: splitName[splitName.length - 1],
-              status: 'finished',
-              url: url
-            }
-          ];
-          emit('update:fileList', url);
-          visiable.value = false;
-        });
+        const res = await onOnlyFileUpload(ossUploadUrl, formData);
+        console.log(res, 'upload');
+        emit('update:fileList', res);
+        visiable.value = false;
       } catch {
         //
-        // message.error('上传失败')
+        // message.error('上传失败');
         return false;
       }
     };

+ 16 - 3
src/helpers/oss-file-upload.ts

@@ -143,6 +143,7 @@ export const onOnlyFileUpload = async (action: string, params: any) => {
     }
     formData.append('file', params.file as File);
     let file = '';
+    let errorObj: any = null;
     await axios
       .post(action as string, formData, {
         // onUploadProgress: ({ progress }) => {
@@ -155,11 +156,18 @@ export const onOnlyFileUpload = async (action: string, params: any) => {
       })
       .catch(error => {
         // onError(error);
-        throw new Error(error);
+        errorObj = error;
+        // throw new Error(error);
       });
+    if (file) {
+      return file;
+    } else {
+      throw new Error(errorObj);
+    }
     return file;
   } else {
     let file = '';
+    let errorObj: any = null;
     const cos = new COS({
       // getAuthorization 必选参数
       getAuthorization: async (options, callback: any) => {
@@ -189,8 +197,13 @@ export const onOnlyFileUpload = async (action: string, params: any) => {
       .catch(error => {
         // console.log(error, 'error');
         // onError();
-        throw new Error(error);
+        // throw new Error(error);
+        errorObj = error;
       });
-    return file;
+    if (file) {
+      return file;
+    } else {
+      throw new Error(errorObj);
+    }
   }
 };