|
|
@@ -1,11 +1,14 @@
|
|
1
|
1
|
import axios from 'axios'
|
|
2
|
|
-import { Notification, MessageBox, Message } from 'element-ui'
|
|
|
2
|
+import { Notification, MessageBox, Message, Loading } from 'element-ui'
|
|
3
|
3
|
import store from '@/store'
|
|
4
|
4
|
import { getToken } from '@/utils/auth'
|
|
5
|
5
|
import errorCode from '@/utils/errorCode'
|
|
6
|
6
|
import { tansParams } from "@/utils/ruoyi";
|
|
|
7
|
+import { saveAs } from 'file-saver'
|
|
7
|
8
|
|
|
8
|
|
-axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
|
|
|
9
|
+let downloadLoadingInstance;
|
|
|
10
|
+
|
|
|
11
|
+axios.defaults.headers['Conntent-Type'] = 'application/json;charset=utf-8'
|
|
9
|
12
|
// 创建axios实例
|
|
10
|
13
|
const service = axios.create({
|
|
11
|
14
|
// axios中请求配置有baseURL选项,表示请求URL公共部分
|
|
|
@@ -90,31 +93,20 @@ service.interceptors.response.use(res => {
|
|
90
|
93
|
|
|
91
|
94
|
// 通用下载方法
|
|
92
|
95
|
export function download(url, params, filename) {
|
|
|
96
|
+ downloadLoadingInstance = Loading.service({ text: "正在下载数据,请稍后", spinner: "el-icon-loading", background: "rgba(0, 0, 0, 0.7)", })
|
|
93
|
97
|
return service.post(url, params, {
|
|
94
|
|
- transformRequest: [(params) => {
|
|
95
|
|
- return tansParams(params)
|
|
96
|
|
- }],
|
|
97
|
|
- headers: {
|
|
98
|
|
- 'Content-Type': 'application/x-www-form-urlencoded'
|
|
99
|
|
- },
|
|
|
98
|
+ transformRequest: [(params) => { return tansParams(params) }],
|
|
|
99
|
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
100
|
100
|
responseType: 'blob'
|
|
101
|
101
|
}).then((data) => {
|
|
102
|
102
|
const content = data
|
|
103
|
103
|
const blob = new Blob([content])
|
|
104
|
|
- if ('download' in document.createElement('a')) {
|
|
105
|
|
- const elink = document.createElement('a')
|
|
106
|
|
- elink.download = filename
|
|
107
|
|
- elink.style.display = 'none'
|
|
108
|
|
- elink.href = URL.createObjectURL(blob)
|
|
109
|
|
- document.body.appendChild(elink)
|
|
110
|
|
- elink.click()
|
|
111
|
|
- URL.revokeObjectURL(elink.href)
|
|
112
|
|
- document.body.removeChild(elink)
|
|
113
|
|
- } else {
|
|
114
|
|
- navigator.msSaveBlob(blob, filename)
|
|
115
|
|
- }
|
|
|
104
|
+ saveAs(blob, filename)
|
|
|
105
|
+ downloadLoadingInstance.close();
|
|
116
|
106
|
}).catch((r) => {
|
|
117
|
107
|
console.error(r)
|
|
|
108
|
+ Message.error('下载文件出现错误,请联系管理员!')
|
|
|
109
|
+ downloadLoadingInstance.close();
|
|
118
|
110
|
})
|
|
119
|
111
|
}
|
|
120
|
112
|
|