浏览代码

智能研报导出图片改为自动保存图片

jwyu 1 年之前
父节点
当前提交
a845d4aa93
共有 1 个文件被更改,包括 36 次插入6 次删除
  1. 36 6
      src/views/smartReport/reportDetail.vue

+ 36 - 6
src/views/smartReport/reportDetail.vue

@@ -112,22 +112,52 @@ export default {
 		},
 
         handleGetReportImg(){
-            if(this.reportImgUrl){
-                this.showReportImg=true
-                return
-            }
+            // if(this.reportImgUrl){
+            //     this.showReportImg=true
+            //     return
+            // }
             apiSmartReport.getReportImg({
                 SmartReportId:Number(this.$route.query.id)
             }).then(res=>{
                 if(res.Ret===200){
-                    this.reportImgUrl=res.Data
-                    this.showReportImg=true
+                    // this.reportImgUrl=res.Data
+                    // this.showReportImg=true
+                    // 改为直接下载图片不是展示
+                    this.saveImg(res.Data)
                 }else{
                     if(res.Ret!==403){
                         this.$message.warning(res.Msg)
                     }
                 }
             })
+        },
+
+        saveImg(imgUrl){
+            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",this.reportInfo.Title)
+                a.style.display = "none"
+                a.href=dataURL
+                document.body.appendChild(a);
+                a.click()
+            }
+            img.onerror=(e)=>{
+                console.log(e);
+                this.$message.warning("自动下载图片失败,请手动保存")
+                if(imgUrl){
+                    this.reportImgUrl=imgUrl
+                    this.showReportImg=true
+                }
+            }
         }
     },
 }