Browse Source

同步下载

lph 1 year ago
parent
commit
763b086e33
1 changed files with 78 additions and 17 deletions
  1. 78 17
      zkqy-ui/src/views/system/engineeringManage/index.vue

+ 78 - 17
zkqy-ui/src/views/system/engineeringManage/index.vue

@@ -237,8 +237,9 @@ import {
   delEngineering,
   addEngineering,
   updateEngineering,
+  downloadEngineering,
 } from "@/api/system/engineering";
-
+import axios from "axios";
 export default {
   name: "Engineering",
   data() {
@@ -396,29 +397,61 @@ export default {
     },
     /** 下载按钮操作 */
     handleDownload() {
-      this.getDownload(
-        "system/engineering/download",
-        {
-          ...this.queryParams,
-        },
-        `engineering_${new Date().getTime()}.zip`
-      );
+      // this.myGetDownload(
+      //   "engineering/download",
+      //   {
+      //     ...this.queryParams,
+      //   },
+      //   `engineering_${new Date().getTime()}.zip`
+      // );
+      let downloadLoadingInstance;
+      downloadLoadingInstance = Loading.service({
+        text: "正在下载数据,请稍候",
+        spinner: "el-icon-loading",
+        background: "rgba(0, 0, 0, 0.7)",
+      });
+      let filename = `engineering_${new Date().getTime()}.zip`;
+      downloadEngineering().then(async (data) => {
+        const isBlob = blobValidate(data);
+        if (isBlob) {
+          const blob = new Blob([data], { type: "application/zip" });
+          // saveAs(blob, filename);
+          if ("download" in document.createElement("a")) {
+            // 非IE下载
+            const elink = document.createElement("a");
+            elink.download = filename;
+            elink.style.display = "none";
+            elink.href = URL.createObjectURL(blob);
+            document.body.appendChild(elink);
+            elink.click();
+            URL.revokeObjectURL(elink.href); // 释放URL 对象
+            document.body.removeChild(elink);
+            this.loading = false;
+          } else {
+            // IE10+下载
+            navigator.msSaveBlob(blob, filename);
+            this.loading = false;
+          }
+        } else {
+          const resText = await data.text();
+          const rspObj = JSON.parse(resText);
+          const errMsg =
+            errorCode[rspObj.code] || rspObj.msg || errorCode["default"];
+          Message.error(errMsg);
+        }
+        downloadLoadingInstance.close();
+      });
     },
-    getDownload(url, params, filename, config) {
+    myGetDownload(url, params, filename, config) {
+      console.log(filename);
       let downloadLoadingInstance;
       downloadLoadingInstance = Loading.service({
         text: "正在下载数据,请稍候",
         spinner: "el-icon-loading",
         background: "rgba(0, 0, 0, 0.7)",
       });
-      return service
-        .get(url, params, {
-          headers: {
-            "Content-Disposition": `attachment;filename="${filename}"`,
-            "Content-Type": `application/octet-stream`,
-          },
-          responseType: "blob",
-        })
+      return axios
+        .get(url, { responseType: "blob" })
         .then(async (data) => {
           const isBlob = blobValidate(data);
           if (isBlob) {
@@ -438,6 +471,34 @@ export default {
           Message.error("下载文件出现错误,请联系管理员!");
           downloadLoadingInstance.close();
         });
+
+      // return service
+      //   .get(url, params, {
+      //     headers: {
+      //       "Content-Disposition": `attachment;filename="${filename}"`,
+      //       "Content-Type": `application/octet-stream`,
+      //     },
+      //     responseType: "blob",
+      //   })
+      //   .then(async (data) => {
+      //     const isBlob = blobValidate(data);
+      //     if (isBlob) {
+      //       const blob = new Blob([data]);
+      //       saveAs(blob, filename);
+      //     } else {
+      //       const resText = await data.text();
+      //       const rspObj = JSON.parse(resText);
+      //       const errMsg =
+      //         errorCode[rspObj.code] || rspObj.msg || errorCode["default"];
+      //       Message.error(errMsg);
+      //     }
+      //     downloadLoadingInstance.close();
+      //   })
+      //   .catch((r) => {
+      //     console.error(r);
+      //     Message.error("下载文件出现错误,请联系管理员!");
+      //     downloadLoadingInstance.close();
+      //   });
     },
   },
 };