jwyu 1 жил өмнө
parent
commit
d3a15a6336

+ 3 - 0
src/api/http.js

@@ -117,6 +117,9 @@ export default {
       ],
     })
       .then((response) => {
+        if(responseType==='blob'){
+          return response
+        }
         return checkStatus(response);
       })
       .catch((res) => {

+ 7 - 1
src/api/modules/oldApi.js

@@ -326,6 +326,11 @@ const getRealPublicSettings = params => {
 	return http.get('/sysuser/public_config')
 }
 
+// 下载文件公共接口
+const apiDownloadResource=params=>{
+	return http.get('/resource/file/download',params,'blob')
+}
+
 export {
 	userLogin,
 	modifyPwd,
@@ -402,5 +407,6 @@ export {
 	getPublicSettingsApi,
 	recordActiveLogin,
 	getRealPublicSettings,
-	userLogOut
+	userLogOut,
+	apiDownloadResource
 }

+ 35 - 0
src/mixins/index.js

@@ -2,6 +2,8 @@
  * Created by xieli on 2018/7/12 0012.
  */
 import http from "api/http.js";
+import {apiDownloadResource} from '@/api/modules/oldApi'
+import {Message} from "element-ui"
 const mixins = {
 	filters: {
 		// 显示时间为 2021.07.22 
@@ -133,6 +135,39 @@ const mixins = {
 					// });       
 				});
 			})
+		},
+		// 下载文件
+		handleDownloadResource(url,fileName){
+			const b=new http.Base64() 
+			const arr=url.split('/')
+			const _fileName=arr[arr.length-1]
+			apiDownloadResource({
+				FileName:_fileName,
+				FileUrl:b.encode(url) 
+			}).then(res=>{
+				console.log(res);
+				const {status,data}=res
+				if(status!=200){
+					Message.waring('下载失败')
+					return
+				}
+				const content = data
+				const blob = new Blob([content])
+				if ('download' in document.createElement('a')) {
+					const elink = document.createElement('a')
+					elink.download = filename || _fileName
+					elink.style.display = 'none'
+					elink.href = window.URL.createObjectURL(blob)
+					document.body.appendChild(elink)
+					elink.click()
+					window.URL.revokeObjectURL(elink.href)
+					document.body.removeChild(elink)
+				} else {
+					navigator.msSaveBlob(blob, filename)
+				}
+			}).catch(()=>{
+				Message.waring('下载失败')
+			})
 		}
 		
 	}

+ 26 - 25
src/views/smartReport/reportList.vue

@@ -506,31 +506,32 @@ export default {
     methods:{
         // 下载报告图片
         handleDownReportImg(imgUrl,title){
-            let img=new Image()
-            img.setAttribute('crossOrigin', 'anonymous');
-            img.src=imgUrl
-            img.onload=()=>{
-                let canvas = document.createElement("canvas");
-                canvas.width = img.width;
-                canvas.height = img.height;
-                let context = canvas.getContext('2d');
-                context.drawImage(img, 0, 0, img.width, img.height);
-                let dataURL = canvas.toDataURL("image/png", 1);
-                const a=document.createElement('a')
-                a.setAttribute("download",title)
-                a.style.display = "none"
-                a.href=dataURL
-                document.body.appendChild(a);
-                a.click()
-            }
-            img.onerror=(e)=>{
-                console.log(e);
-                this.$message.warning(this.$t('ReportManage.smart_msg.please_manually_save'))
-                if(imgUrl){
-                    this.reportImgUrl=imgUrl
-                    this.showReportImg=true
-                }
-            }
+            this.handleDownloadResource(imgUrl,title)
+            // let img=new Image()
+            // img.setAttribute('crossOrigin', 'anonymous');
+            // img.src=imgUrl
+            // img.onload=()=>{
+            //     let canvas = document.createElement("canvas");
+            //     canvas.width = img.width;
+            //     canvas.height = img.height;
+            //     let context = canvas.getContext('2d');
+            //     context.drawImage(img, 0, 0, img.width, img.height);
+            //     let dataURL = canvas.toDataURL("image/png", 1);
+            //     const a=document.createElement('a')
+            //     a.setAttribute("download",title)
+            //     a.style.display = "none"
+            //     a.href=dataURL
+            //     document.body.appendChild(a);
+            //     a.click()
+            // }
+            // img.onerror=(e)=>{
+            //     console.log(e);
+            //     this.$message.warning(this.$t('ReportManage.smart_msg.please_manually_save'))
+            //     if(imgUrl){
+            //         this.reportImgUrl=imgUrl
+            //         this.showReportImg=true
+            //     }
+            // }
         },
 
         // 删除某个报告