From ded99502ae2d7824d9584b5c69f4dfcf7bdb6759 Mon Sep 17 00:00:00 2001
From: RuoYi <yzz_ivy@163.com>
Date: 星期一, 27 九月 2021 10:38:29 +0800
Subject: [PATCH] 新增通用方法简化下载使用

---
 ruoyi-ui/src/views/system/dict/data.vue                                      |    2 
 ruoyi-ui/src/views/monitor/job/log.vue                                       |    2 
 ruoyi-ui/src/views/tool/gen/index.vue                                        |    3 
 ruoyi-ui/src/utils/ruoyi.js                                                  |    7 --
 ruoyi-ui/src/views/system/role/index.vue                                     |    2 
 ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUtils.java        |    1 
 ruoyi-generator/src/main/resources/vm/vue/index.vue.vm                       |    2 
 ruoyi-ui/src/views/system/user/index.vue                                     |    4 
 ruoyi-ui/src/views/monitor/job/index.vue                                     |    2 
 ruoyi-ui/src/views/monitor/operlog/index.vue                                 |    2 
 ruoyi-ui/src/views/system/config/index.vue                                   |    2 
 /dev/null                                                                    |   42 --------------
 ruoyi-ui/src/views/system/dict/index.vue                                     |    2 
 ruoyi-ui/src/main.js                                                         |    3 
 ruoyi-ui/src/plugins/index.js                                                |    3 +
 ruoyi-ui/src/views/tool/build/index.vue                                      |   19 +-----
 ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java |    2 
 ruoyi-ui/src/plugins/download.js                                             |   48 ++++++++++++++++
 ruoyi-ui/src/views/monitor/logininfor/index.vue                              |    2 
 ruoyi-ui/src/views/system/post/index.vue                                     |    2 
 20 files changed, 70 insertions(+), 82 deletions(-)

diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUtils.java
index 04ae9b1..8d8f5a6 100644
--- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUtils.java
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUtils.java
@@ -211,6 +211,7 @@
                 .append(percentEncodedFileName);
 
         response.setHeader("Content-disposition", contentDispositionValue.toString());
+        response.setHeader("download-filename", percentEncodedFileName);
     }
 
     /**
diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java
index bd36229..58dd02a 100644
--- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java
+++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java
@@ -107,8 +107,6 @@
                         "/**/*.js",
                         "/profile/**"
                 ).permitAll()
-                .antMatchers("/common/download**").anonymous()
-                .antMatchers("/common/download/resource**").anonymous()
                 .antMatchers("/swagger-ui.html").anonymous()
                 .antMatchers("/swagger-resources/**").anonymous()
                 .antMatchers("/webjars/**").anonymous()
diff --git a/ruoyi-generator/src/main/resources/vm/vue/index.vue.vm b/ruoyi-generator/src/main/resources/vm/vue/index.vue.vm
index a555503..867225a 100644
--- a/ruoyi-generator/src/main/resources/vm/vue/index.vue.vm
+++ b/ruoyi-generator/src/main/resources/vm/vue/index.vue.vm
@@ -567,7 +567,7 @@
         this.exportLoading = true;
         return export${BusinessName}(queryParams);
       }).then(response => {
-        this.download(response.msg);
+        this.#[[$download]]#.name(response.msg);
         this.exportLoading = false;
       }).catch(() => {});
     }
diff --git a/ruoyi-ui/src/main.js b/ruoyi-ui/src/main.js
index 79a2ab4..489600a 100644
--- a/ruoyi-ui/src/main.js
+++ b/ruoyi-ui/src/main.js
@@ -17,7 +17,7 @@
 import './permission' // permission control
 import { getDicts } from "@/api/system/dict/data";
 import { getConfigKey } from "@/api/system/config";
-import { parseTime, resetForm, addDateRange, selectDictLabel, selectDictLabels, download, handleTree } from "@/utils/ruoyi";
+import { parseTime, resetForm, addDateRange, selectDictLabel, selectDictLabels, handleTree } from "@/utils/ruoyi";
 // 鍒嗛〉缁勪欢
 import Pagination from "@/components/Pagination";
 // 鑷畾涔夎〃鏍煎伐鍏风粍浠�
@@ -43,7 +43,6 @@
 Vue.prototype.addDateRange = addDateRange
 Vue.prototype.selectDictLabel = selectDictLabel
 Vue.prototype.selectDictLabels = selectDictLabels
-Vue.prototype.download = download
 Vue.prototype.handleTree = handleTree
 
 // 鍏ㄥ眬缁勪欢鎸傝浇
diff --git a/ruoyi-ui/src/plugins/download.js b/ruoyi-ui/src/plugins/download.js
new file mode 100644
index 0000000..cb10ab0
--- /dev/null
+++ b/ruoyi-ui/src/plugins/download.js
@@ -0,0 +1,48 @@
+import { saveAs } from 'file-saver'
+import axios from 'axios'
+import { getToken } from '@/utils/auth'
+
+const baseURL = process.env.VUE_APP_BASE_API
+
+export default {
+  name(name, isDelete = true) {
+    var url = baseURL + "/common/download?fileName=" + encodeURI(name) + "&delete=" + isDelete
+    axios({
+      method: 'get',
+      url: url,
+      responseType: 'blob',
+      headers: { 'Authorization': 'Bearer ' + getToken() }
+    }).then(res => {
+      const blob = new Blob([res.data])
+      this.saveAs(blob, decodeURI(res.headers['download-filename']))
+    })
+  },
+  resource(resource) {
+    var url = baseURL + "/common/download/resource?resource=" + encodeURI(resource);
+    axios({
+      method: 'get',
+      url: url,
+      responseType: 'blob',
+      headers: { 'Authorization': 'Bearer ' + getToken() }
+    }).then(res => {
+      const blob = new Blob([res.data])
+      this.saveAs(blob, decodeURI(res.headers['download-filename']))
+    })
+  },
+  zip(url, name) {
+    var url = baseURL + url
+    axios({
+      method: 'get',
+      url: url,
+      responseType: 'blob',
+      headers: { 'Authorization': 'Bearer ' + getToken() }
+    }).then(res => {
+      const blob = new Blob([res.data], { type: 'application/zip' })
+      this.saveAs(blob, name)
+    })
+  },
+  saveAs(text, name, opts) {
+    saveAs(text, name, opts);
+  }
+}
+
diff --git a/ruoyi-ui/src/plugins/index.js b/ruoyi-ui/src/plugins/index.js
index 15d829b..a138e6d 100644
--- a/ruoyi-ui/src/plugins/index.js
+++ b/ruoyi-ui/src/plugins/index.js
@@ -1,5 +1,6 @@
 import cache from './cache'
 import modal from './modal'
+import download from './download'
 
 export default {
   install(Vue) {
@@ -7,5 +8,7 @@
     Vue.prototype.$cache = cache
     // 妯℃�佹瀵硅薄
     Vue.prototype.$modal = modal
+    // 涓嬭浇鏂囦欢
+    Vue.prototype.$download = download
   }
 }
diff --git a/ruoyi-ui/src/utils/ruoyi.js b/ruoyi-ui/src/utils/ruoyi.js
index 5669a43..63bd379 100644
--- a/ruoyi-ui/src/utils/ruoyi.js
+++ b/ruoyi-ui/src/utils/ruoyi.js
@@ -3,8 +3,6 @@
  * Copyright (c) 2019 ruoyi
  */
 
-const baseURL = process.env.VUE_APP_BASE_API
-
 // 鏃ユ湡鏍煎紡鍖�
 export function parseTime(time, pattern) {
 	if (arguments.length === 0 || !time) {
@@ -93,11 +91,6 @@
 		})
 	})
 	return actions.join('').substring(0, actions.join('').length - 1);
-}
-
-// 閫氱敤涓嬭浇鏂规硶
-export function download(fileName) {
-	window.location.href = baseURL + "/common/download?fileName=" + encodeURI(fileName) + "&delete=" + true;
 }
 
 // 瀛楃涓叉牸寮忓寲(%s )
diff --git a/ruoyi-ui/src/utils/zipdownload.js b/ruoyi-ui/src/utils/zipdownload.js
deleted file mode 100644
index 8a1b819..0000000
--- a/ruoyi-ui/src/utils/zipdownload.js
+++ /dev/null
@@ -1,42 +0,0 @@
-import axios from 'axios'
-import { getToken } from '@/utils/auth'
-
-const mimeMap = {
-  xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
-  zip: 'application/zip'
-}
-
-const baseUrl = process.env.VUE_APP_BASE_API
-export function downLoadZip(str, filename) {
-  var url = baseUrl + str
-  axios({
-    method: 'get',
-    url: url,
-    responseType: 'blob',
-    headers: { 'Authorization': 'Bearer ' + getToken() }
-  }).then(res => {
-    resolveBlob(res, mimeMap.zip)
-  })
-}
-/**
- * 瑙f瀽blob鍝嶅簲鍐呭骞朵笅杞�
- * @param {*} res blob鍝嶅簲鍐呭
- * @param {String} mimeType MIME绫诲瀷
- */
-export function resolveBlob(res, mimeType) {
-  const aLink = document.createElement('a')
-  var blob = new Blob([res.data], { type: mimeType })
-  // //浠巖esponse鐨刪eaders涓幏鍙杅ilename, 鍚庣response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 璁剧疆鐨勬枃浠跺悕;
-  var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
-  var contentDisposition = decodeURI(res.headers['content-disposition'])
-  var result = patt.exec(contentDisposition)
-  var fileName = result[1]
-  fileName = fileName.replace(/\"/g, '')
-  aLink.style.display = 'none'
-  aLink.href = URL.createObjectURL(blob)
-  aLink.setAttribute('download', fileName) // 璁剧疆涓嬭浇鏂囦欢鍚嶇О
-  document.body.appendChild(aLink)
-  aLink.click()
-  URL.revokeObjectURL(aLink.href);//娓呴櫎寮曠敤
-  document.body.removeChild(aLink);
-}
diff --git a/ruoyi-ui/src/views/monitor/job/index.vue b/ruoyi-ui/src/views/monitor/job/index.vue
index 528c98b..06b939d 100644
--- a/ruoyi-ui/src/views/monitor/job/index.vue
+++ b/ruoyi-ui/src/views/monitor/job/index.vue
@@ -515,7 +515,7 @@
         this.exportLoading = true;
         return exportJob(queryParams);
       }).then(response => {
-        this.download(response.msg);
+        this.$download.name(response.msg);
         this.exportLoading = false;
       }).catch(() => {});
     }
diff --git a/ruoyi-ui/src/views/monitor/job/log.vue b/ruoyi-ui/src/views/monitor/job/log.vue
index f8b3a50..44efe5f 100644
--- a/ruoyi-ui/src/views/monitor/job/log.vue
+++ b/ruoyi-ui/src/views/monitor/job/log.vue
@@ -298,7 +298,7 @@
         this.exportLoading = true;
         return exportJobLog(queryParams);
       }).then(response => {
-        this.download(response.msg);
+        this.$download.name(response.msg);
         this.exportLoading = false;
       }).catch(() => {});
     }
diff --git a/ruoyi-ui/src/views/monitor/logininfor/index.vue b/ruoyi-ui/src/views/monitor/logininfor/index.vue
index b4a498e..6341419 100644
--- a/ruoyi-ui/src/views/monitor/logininfor/index.vue
+++ b/ruoyi-ui/src/views/monitor/logininfor/index.vue
@@ -221,7 +221,7 @@
         this.exportLoading = true;
         return exportLogininfor(queryParams);
       }).then(response => {
-        this.download(response.msg);
+        this.$download.name(response.msg);
         this.exportLoading = false;
       }).catch(() => {});
     }
diff --git a/ruoyi-ui/src/views/monitor/operlog/index.vue b/ruoyi-ui/src/views/monitor/operlog/index.vue
index 243451a..0aee4a6 100644
--- a/ruoyi-ui/src/views/monitor/operlog/index.vue
+++ b/ruoyi-ui/src/views/monitor/operlog/index.vue
@@ -308,7 +308,7 @@
         this.exportLoading = true;
         return exportOperlog(queryParams);
       }).then(response => {
-        this.download(response.msg);
+        this.$download.name(response.msg);
         this.exportLoading = false;
       }).catch(() => {});
     }
diff --git a/ruoyi-ui/src/views/system/config/index.vue b/ruoyi-ui/src/views/system/config/index.vue
index c1bb728..efb6961 100644
--- a/ruoyi-ui/src/views/system/config/index.vue
+++ b/ruoyi-ui/src/views/system/config/index.vue
@@ -339,7 +339,7 @@
         this.exportLoading = true;
         return exportConfig(queryParams);
       }).then(response => {
-        this.download(response.msg);
+        this.$download.name(response.msg);
         this.exportLoading = false;
       }).catch(() => {});
     },
diff --git a/ruoyi-ui/src/views/system/dict/data.vue b/ruoyi-ui/src/views/system/dict/data.vue
index 6ff517b..36eb6af 100644
--- a/ruoyi-ui/src/views/system/dict/data.vue
+++ b/ruoyi-ui/src/views/system/dict/data.vue
@@ -385,7 +385,7 @@
         this.exportLoading = true;
         return exportData(queryParams);
       }).then(response => {
-        this.download(response.msg);
+        this.$download.name(response.msg);
         this.exportLoading = false;
       }).catch(() => {});
     }
diff --git a/ruoyi-ui/src/views/system/dict/index.vue b/ruoyi-ui/src/views/system/dict/index.vue
index a0e5539..37de40f 100644
--- a/ruoyi-ui/src/views/system/dict/index.vue
+++ b/ruoyi-ui/src/views/system/dict/index.vue
@@ -343,7 +343,7 @@
         this.exportLoading = true;
         return exportType(queryParams);
       }).then(response => {
-        this.download(response.msg);
+        this.$download.name(response.msg);
         this.exportLoading = false;
       }).catch(() => {});
     },
diff --git a/ruoyi-ui/src/views/system/post/index.vue b/ruoyi-ui/src/views/system/post/index.vue
index b0039eb..e206968 100644
--- a/ruoyi-ui/src/views/system/post/index.vue
+++ b/ruoyi-ui/src/views/system/post/index.vue
@@ -310,7 +310,7 @@
         this.exportLoading = true;
         return exportPost(queryParams);
       }).then(response => {
-        this.download(response.msg);
+        this.$download.name(response.msg);
         this.exportLoading = false;
       }).catch(() => {});
     }
diff --git a/ruoyi-ui/src/views/system/role/index.vue b/ruoyi-ui/src/views/system/role/index.vue
index b58b26f..6e58558 100644
--- a/ruoyi-ui/src/views/system/role/index.vue
+++ b/ruoyi-ui/src/views/system/role/index.vue
@@ -618,7 +618,7 @@
         this.exportLoading = true;
         return exportRole(queryParams);
       }).then(response => {
-        this.download(response.msg);
+        this.$download.name(response.msg);
         this.exportLoading = false;
       }).catch(() => {});
     }
diff --git a/ruoyi-ui/src/views/system/user/index.vue b/ruoyi-ui/src/views/system/user/index.vue
index fe431f1..4f20b08 100644
--- a/ruoyi-ui/src/views/system/user/index.vue
+++ b/ruoyi-ui/src/views/system/user/index.vue
@@ -648,7 +648,7 @@
         this.exportLoading = true;
         return exportUser(queryParams);
       }).then(response => {
-        this.download(response.msg);
+        this.$download.name(response.msg);
         this.exportLoading = false;
       }).catch(() => {});
     },
@@ -660,7 +660,7 @@
     /** 涓嬭浇妯℃澘鎿嶄綔 */
     importTemplate() {
       importTemplate().then(response => {
-        this.download(response.msg);
+        this.$download.name(response.msg);
       });
     },
     // 鏂囦欢涓婁紶涓鐞�
diff --git a/ruoyi-ui/src/views/tool/build/index.vue b/ruoyi-ui/src/views/tool/build/index.vue
index 0281d18..e511408 100644
--- a/ruoyi-ui/src/views/tool/build/index.vue
+++ b/ruoyi-ui/src/views/tool/build/index.vue
@@ -137,23 +137,13 @@
 
 <script>
 import draggable from 'vuedraggable'
-import { saveAs } from 'file-saver'
 import beautifier from 'js-beautify'
 import ClipboardJS from 'clipboard'
 import render from '@/utils/generator/render'
 import RightPanel from './RightPanel'
-import {
-  inputComponents,
-  selectComponents,
-  layoutComponents,
-  formConf
-} from '@/utils/generator/config'
-import {
-  exportDefault, beautifierConf, isNumberStr, titleCase
-} from '@/utils/index'
-import {
-  makeUpHtml, vueTemplate, vueScript, cssStyle
-} from '@/utils/generator/html'
+import { inputComponents, selectComponents, layoutComponents, formConf } from '@/utils/generator/config'
+import { beautifierConf, titleCase } from '@/utils/index'
+import { makeUpHtml, vueTemplate, vueScript, cssStyle } from '@/utils/generator/html'
 import { makeUpJs } from '@/utils/generator/js'
 import { makeUpCss } from '@/utils/generator/css'
 import drawingDefalut from '@/utils/generator/drawingDefalut'
@@ -161,7 +151,6 @@
 import CodeTypeDialog from './CodeTypeDialog'
 import DraggableItem from './DraggableItem'
 
-const emptyActiveData = { style: {}, autosize: {} }
 let oldActiveId
 let tempActiveData
 
@@ -287,7 +276,7 @@
     execDownload(data) {
       const codeStr = this.generateCode()
       const blob = new Blob([codeStr], { type: 'text/plain;charset=utf-8' })
-      saveAs(blob, data.fileName)
+      this.$download.saveAs(blob, data.fileName)
     },
     execCopy(data) {
       document.getElementById('copyNode').click()
diff --git a/ruoyi-ui/src/views/tool/gen/index.vue b/ruoyi-ui/src/views/tool/gen/index.vue
index 25c4f12..646decf 100644
--- a/ruoyi-ui/src/views/tool/gen/index.vue
+++ b/ruoyi-ui/src/views/tool/gen/index.vue
@@ -180,7 +180,6 @@
 <script>
 import { listTable, previewTable, delTable, genCode, synchDb } from "@/api/tool/gen";
 import importTable from "./importTable";
-import { downLoadZip } from "@/utils/zipdownload";
 import hljs from "highlight.js/lib/highlight";
 import "highlight.js/styles/github-gist.css";
 hljs.registerLanguage("java", require("highlight.js/lib/languages/java"));
@@ -270,7 +269,7 @@
           this.$modal.msgSuccess("鎴愬姛鐢熸垚鍒拌嚜瀹氫箟璺緞锛�" + row.genPath);
         });
       } else {
-        downLoadZip("/tool/gen/batchGenCode?tables=" + tableNames, "ruoyi");
+        this.$download.zip("/tool/gen/batchGenCode?tables=" + tableNames, "ruoyi");
       }
     },
     /** 鍚屾鏁版嵁搴撴搷浣� */

--
Gitblit v1.9.3