Tang 3 年之前
父节点
当前提交
eab07c421c
共有 2 个文件被更改,包括 75 次插入2 次删除
  1. 2 1
      package.json
  2. 73 1
      src/views/education/certificateManagement/creditHoursList/index.vue

+ 2 - 1
package.json

@@ -43,13 +43,14 @@
     "core-js": "3.8.1",
     "echarts": "4.9.0",
     "element-ui": "2.15.6",
-    "file-saver": "2.0.5",
+    "file-saver": "^2.0.5",
     "fuse.js": "6.4.3",
     "highlight.js": "9.18.5",
     "js-beautify": "1.13.0",
     "js-cookie": "2.2.1",
     "js-md5": "^0.7.3",
     "jsencrypt": "3.0.0-rc.1",
+    "jszip": "^3.7.1",
     "nprogress": "0.2.0",
     "quill": "1.3.7",
     "quill-image-resize-module": "^3.0.0",

+ 73 - 1
src/views/education/certificateManagement/creditHoursList/index.vue

@@ -8,12 +8,18 @@
       @init="init"
     />
     <table-list
+      ref="tableList"
       :tableSets="tableSet"
       :tableData="tableData"
       :navText="navText"
       :loading="loading"
       @editInfo="editInfo"
     >
+      <template slot="customize">
+        <el-button size="small" type="success" @click="moreActive"
+          >批量下载</el-button
+        >
+      </template>
       <template slot="btn" slot-scope="props">
         <el-button type="text" @click="addClick(props.scope.row, 2)"
           >下载</el-button
@@ -31,6 +37,8 @@
 </template>
 
 <script>
+import JSZip from 'jszip'
+import FileSaver from 'file-saver'
 import * as baseUrls from "@/utils/request.js";
 import searchBoxNew from "@/components/searchBoxNew";
 import tableList from "@/components/tableList";
@@ -90,7 +98,7 @@ export default {
           placeholder1: "培训有效期开始时间",
           placeholder2: "培训有效期结束时间",
           scope: "moreDataPicker",
-          Diszing:true,
+          Diszing: true,
         },
         {
           prop: "searchKey",
@@ -178,6 +186,70 @@ export default {
     this.search();
   },
   methods: {
+    /**
+     * 批量下载
+     */
+    moreActive() {
+      if (!this.$refs.tableList.allCheckData.length) {
+        this.$message.warning("请勾选需要下载的数据");
+        return;
+      }
+      let arr = this.$refs.tableList.allCheckData.map((item) => {
+        return {
+          fileUrl: baseUrls.BASE_IMG_URL + "/" + item.certificatePath,
+          renameFileName: item.certificateCode,
+        };
+      });
+      this.filesToRar(arr, "证书批量下载");
+      console.log(arr);
+    },
+    filesToRar(arrImages, filename) {
+      let _this = this;
+      let zip = new JSZip();
+      let cache = {};
+      let promises = [];
+      _this.title = "正在加载压缩文件";
+
+      for (let item of arrImages) {
+        const promise = _this.getImgArrayBuffer(item.fileUrl).then((data) => {
+          // 下载文件, 并存成ArrayBuffer对象(blob)
+          zip.file(item.renameFileName, data, { binary: true }); // 逐个添加文件
+          cache[item.renameFileName] = data;
+        });
+        promises.push(promise);
+      }
+
+      Promise.all(promises)
+        .then(() => {
+          zip.generateAsync({ type: "blob" }).then((content) => {
+            _this.title = "正在压缩";
+            // 生成二进制流
+            FileSaver.saveAs(content, filename); // 利用file-saver保存文件  自定义文件名
+            _this.title = "压缩完成";
+          });
+        })
+        .catch((res) => {
+          _this.$message.error("文件压缩失败");
+        });
+    },
+    //获取文件blob
+    getImgArrayBuffer(url) {
+      let _this = this;
+      return new Promise((resolve, reject) => {
+        //通过请求获取文件blob格式
+        let xmlhttp = new XMLHttpRequest();
+        xmlhttp.open("GET", url, true);
+        xmlhttp.responseType = "blob";
+        xmlhttp.onload = function () {
+          if (this.status == 200) {
+            resolve(this.response);
+          } else {
+            reject(this.status);
+          }
+        };
+        xmlhttp.send();
+      });
+    },
     editInfo(v) {
       this.addClick(v, 2);
     },