Browse Source

feat(组件): 修改上传api

tangning 2 năm trước cách đây
mục cha
commit
a4747b77fe

+ 50 - 12
src/utils/http/axios/Axios.ts

@@ -124,6 +124,7 @@ export class VAxios {
    * @description:  File Upload
    */
   uploadFile<T = any>(config: AxiosRequestConfig, params: UploadFileParams) {
+    let conf: CreateAxiosOptions = cloneDeep(config);
     const formData = new window.FormData();
     const customFilename = params.name || 'file';
 
@@ -146,16 +147,50 @@ export class VAxios {
         formData.append(key, params.data![key]);
       });
     }
-    return this.axiosInstance.request<T>({
-      ...config,
-      method: 'POST',
-      data: formData,
-      headers: {
-        'Content-type': ContentTypeEnum.FORM_DATA,
-        // @ts-ignore
-        ignoreCancelToken: true,
-      },
-    })
+    const transform = this.getTransform();
+    const { requestOptions } = this.options;
+    const opt: RequestOptions = Object.assign({}, requestOptions);
+    const { beforeRequestHook, requestCatchHook, transformRequestHook } = transform || {};
+    if (beforeRequestHook && isFunction(beforeRequestHook)) {
+      conf = beforeRequestHook(conf, opt);
+    }
+    conf = this.supportFormData(conf);
+    return new Promise((resolve, reject) => {
+      this.axiosInstance
+        .request<any, AxiosResponse<Result>>({
+          ...conf,
+          method: 'POST',
+          data: formData,
+          requestOptions,
+          headers: {
+            'Content-type': ContentTypeEnum.FORM_DATA,
+            // @ts-ignore
+            ignoreCancelToken: true,
+          },
+        })
+        .then((res: AxiosResponse<Result>) => {
+          if (transformRequestHook && isFunction(transformRequestHook)) {
+            try {
+              const ret = transformRequestHook(res, opt);
+              resolve(ret);
+            } catch (err) {
+              reject(err || new Error('request error!'));
+            }
+            return;
+          }
+          resolve(res as unknown as Promise<T>);
+        })
+        .catch((e: Error | AxiosError) => {
+          if (requestCatchHook && isFunction(requestCatchHook)) {
+            reject(requestCatchHook(e, opt));
+            return;
+          }
+          if (axios.isAxiosError(e)) {
+            // rewrite error message from axios in here
+          }
+          reject(e);
+        });
+    });
   }
 
   
@@ -241,6 +276,7 @@ export class VAxios {
 
   request<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> {
     let conf: CreateAxiosOptions = cloneDeep(config);
+    console.log('conf1',conf)
     const transform = this.getTransform();
 
     const { requestOptions } = this.options;
@@ -249,12 +285,14 @@ export class VAxios {
 
     const { beforeRequestHook, requestCatchHook, transformRequestHook } = transform || {};
     if (beforeRequestHook && isFunction(beforeRequestHook)) {
-      conf = beforeRequestHook(conf, opt);
+    console.log('conf2',conf)
+    conf = beforeRequestHook(conf, opt);
     }
+    console.log('conf3',conf)
     conf.requestOptions = opt;
 
     conf = this.supportFormData(conf);
-
+    console.log('conf4',conf)
     return new Promise((resolve, reject) => {
       this.axiosInstance
         .request<any, AxiosResponse<Result>>(conf)

+ 1 - 1
src/views/device/OutflowModal.vue

@@ -100,7 +100,7 @@
           const apiData = {
             file: fileFlow.file,
             data:{
-              type: 2,
+              type: 1,
             }
           };
           const res = await companyUploadExcel(apiData);

+ 1 - 1
src/views/device/index.vue

@@ -18,7 +18,7 @@
                 onClick: handleDelete.bind(null, record),
               },{
                 label: '出库',
-                ifShow: getCheckPerm('device-out') && !Boolean(record.companyName),
+                ifShow: getCheckPerm('device-out') && !record.outType,
                 onClick: handleCheckout.bind(null, record),
               },{
                 label: '编辑',