Sfoglia il codice sorgente

批量下载附件功能

SanHQin 1 anno fa
parent
commit
f9c2a05bdf
1 ha cambiato i file con 113 aggiunte e 1 eliminazioni
  1. 113 1
      src/components/pages/test/check/index.vue

+ 113 - 1
src/components/pages/test/check/index.vue

@@ -95,6 +95,7 @@
                     :class="{ test_icon_check: item.type == 1 && item.atype == 2, test_icon_checkO: item.type == 1 && item.atype == 1, test_icon_gap: item.type == 3, test_icon_file: item.type == 5, test_course_file: item.type == 6, test_eva_file: item.type == 7 }"></span>
                 </el-tooltip>
                 <span>{{ item.title }}</span>
+								<el-button v-if="item.type == 5" class="title_downBtn" type="primary" size="small" @click.stop="downloadFileType5(item.array,item.title)">批量下载附件</el-button>
               </div>
               <div class="detail" v-if="item.detail">{{ item.detail }}</div>
               <div class="content1" v-if="item.type == 1">
@@ -338,6 +339,44 @@ import FileSaver from "file-saver";
 
 import XLSX from "xlsx-js-style";
 
+const getFile = (url) => {
+    return new Promise((resolve, reject) => {
+        var credentials = {
+            accessKeyId: "AKIATLPEDU37QV5CHLMH",
+            secretAccessKey: "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",
+        }; //秘钥形式的登录上传
+        window.AWS.config.update(credentials);
+        window.AWS.config.region = "cn-northwest-1"; //设置区域
+        let url2 = url;
+        let _url2 = "";
+        if (
+            url2.indexOf("https://view.officeapps.live.com/op/view.aspx?src=") != -1
+        ) {
+            _url2 = url2.split(
+                "https://view.officeapps.live.com/op/view.aspx?src="
+            )[1];
+        } else {
+            _url2 = url2;
+        }
+        var s3 = new window.AWS.S3({ params: { Bucket: "ccrb" } });
+        let name = decodeURIComponent(_url2.split("https://ccrb.s3.cn-northwest-1.amazonaws.com.cn/")[1])
+        var params = {
+            Bucket: "ccrb",
+            Key: name
+        };
+        s3.getObject(params, function (err, data) {
+            if (err) {
+                console.log(err, err.stack)
+                resolve({ data: 1 });
+            } else {
+                resolve({ data: data.Body });
+                console.log(data);
+            }          // sxuccessful response
+
+        });
+        // axios({
+    });
+};
 
 export default {
   components: {
@@ -667,8 +706,10 @@ export default {
                 }
               } else if (topic.type == 5) {
                 let _answer = el2.array[i].json.file ? el2.array[i].json.file : []
+								console.log(el2)
+								let _user = {userid:el2.userid,username:el2.name}
                 for (var k = 0; k < _answer.length; k++) {
-                  topic.array.push(_answer[k])
+                  topic.array.push({..._answer[k],..._user})
                 }
               } else if (topic.type == 7) {
                 if(el2.array[i].json.answer2){
@@ -1351,6 +1392,70 @@ export default {
         }
         return item.answer2 ? array[item.answer2] : ''
     },
+		downloadFileType5(fileData,title="附件"){
+			let data = fileData;
+			const zip = new JSZip();
+			let downFileArray = [];
+			let uniqueArr = data.filter((v,i,a)=>a.map(mb=>mb.userid).indexOf(v.userid)==i);
+			uniqueArr.forEach(i=>{
+				downFileArray.push({userid:i.userid,username:i.username,urlList:data.filter(d=>d.userid==i.userid).map(m=>m)});
+			})
+			let promises = [];
+			if(downFileArray.length==1){//只有一位老师的情况
+				if(downFileArray[0].urlList.length>1){//多个文件的情况
+					const folder = zip.folder(`${downFileArray[0].username}`);
+					downFileArray[0].urlList.forEach(i=>{
+						const promise = getFile(i.url).then((data) => {
+        	      if (data.data != 1) {
+        	          // 下载文件, 并存成ArrayBuffer对象
+        	          const file_name = i.name; // 获取文件名
+									  folder.file(file_name, data.data, { binary: true }); // 逐个添加文件
+        	      }
+        	  });
+						promises.push(promise)
+					})
+					
+				}else{//只有一个文件的情况
+					promises.push(getFile(downFileArray[0].urlList[0].url).then((data) => {
+						if (data.data != 1) {
+        	      // 下载文件, 并存成ArrayBuffer对象
+        	      const file_name = `${downFileArray[0].username}-${downFileArray[0].urlList[0].name}`; // 获取文件名
+								zip.file(file_name, data.data, { binary: true }); // 逐个添加文件
+        	    }
+					}))
+				}
+			}else if(downFileArray.length>1){//有多位老师的情况
+				downFileArray.forEach(i=>{
+					if(i.urlList.length==1){//只有一个文件的情况
+						promises.push(getFile(i.urlList[0].url).then((data) => {
+						  if (data.data != 1) {
+        	      // 下载文件, 并存成ArrayBuffer对象
+        	      const file_name = `${i.username}-${i.urlList[0].name}`; // 获取文件名
+								zip.file(file_name, data.data, { binary: true }); // 逐个添加文件
+        	    }
+						}))
+					}else if(i.urlList.length>1){//有多个文件的
+						const folder = zip.folder(`${i.username}`);
+						i.urlList.forEach(ui=>{
+						  const promise = getFile(ui.url).then((data) => {
+								if (data.data != 1) {
+        	          // 下载文件, 并存成ArrayBuffer对象
+        	          const file_name = ui.name; // 获取文件名
+									  folder.file(file_name, data.data, { binary: true }); // 逐个添加文件
+        	      }
+						  })
+							promises.push(promise)
+						})
+					}
+				})
+			}
+			Promise.all(promises).then(() => {
+          zip.generateAsync({ type: "blob" }).then((content) => {
+              // 生成二进制流
+              FileSaver.saveAs(content, `${this.testJson.title}-${title}.zip`); // 利用file-saver保存文件  自定义文件名
+          });
+      });
+		},
   },
   beforeDestroy() {
     document.getElementsByTagName('html')[0].style.overflow = ''
@@ -1608,6 +1713,7 @@ export default {
   font-size: 18px;
   display: flex;
   white-space: pre-line;
+	position: relative;
 }
 
 .title_content>.title_box>.title>.test_icon {
@@ -2077,4 +2183,10 @@ export default {
   font-size: 14px;
   color: #8c8c8c;
 }
+
+.title_box>.title>.title_downBtn{
+	position: absolute;
+	right: 10px;
+	top: 0;
+}
 </style>