提交 | 用户 | 时间
|
0ca254
|
1 |
import axios from 'axios' |
A |
2 |
import {Loading, Message} from 'element-ui' |
|
3 |
import { saveAs } from 'file-saver' |
|
4 |
import { getToken } from '@/utils/auth' |
|
5 |
import errorCode from '@/utils/errorCode' |
|
6 |
import { blobValidate } from "@/utils/ruoyi"; |
|
7 |
|
|
8 |
const baseURL = process.env.VUE_APP_BASE_API |
|
9 |
let downloadLoadingInstance; |
|
10 |
|
|
11 |
export default { |
|
12 |
name(name, isDelete = true) { |
|
13 |
var url = baseURL + "/common/download?fileName=" + encodeURIComponent(name) + "&delete=" + isDelete |
|
14 |
axios({ |
|
15 |
method: 'get', |
|
16 |
url: url, |
|
17 |
responseType: 'blob', |
|
18 |
headers: { 'Authorization': 'Bearer ' + getToken() } |
|
19 |
}).then((res) => { |
|
20 |
const isBlob = blobValidate(res.data); |
|
21 |
if (isBlob) { |
|
22 |
const blob = new Blob([res.data]) |
|
23 |
this.saveAs(blob, decodeURIComponent(res.headers['download-filename'])) |
|
24 |
} else { |
|
25 |
this.printErrMsg(res.data); |
|
26 |
} |
|
27 |
}) |
|
28 |
}, |
|
29 |
resource(resource) { |
|
30 |
var url = baseURL + "/common/download/resource?resource=" + encodeURIComponent(resource); |
|
31 |
axios({ |
|
32 |
method: 'get', |
|
33 |
url: url, |
|
34 |
responseType: 'blob', |
|
35 |
headers: { 'Authorization': 'Bearer ' + getToken() } |
|
36 |
}).then((res) => { |
|
37 |
const isBlob = blobValidate(res.data); |
|
38 |
if (isBlob) { |
|
39 |
const blob = new Blob([res.data]) |
|
40 |
this.saveAs(blob, decodeURIComponent(res.headers['download-filename'])) |
|
41 |
} else { |
|
42 |
this.printErrMsg(res.data); |
|
43 |
} |
|
44 |
}) |
|
45 |
}, |
|
46 |
zip(url, name) { |
|
47 |
var url = baseURL + url |
|
48 |
downloadLoadingInstance = Loading.service({ text: "正在下载数据,请稍候", spinner: "el-icon-loading", background: "rgba(0, 0, 0, 0.7)", }) |
|
49 |
axios({ |
|
50 |
method: 'get', |
|
51 |
url: url, |
|
52 |
responseType: 'blob', |
|
53 |
headers: { 'Authorization': 'Bearer ' + getToken() } |
|
54 |
}).then((res) => { |
|
55 |
const isBlob = blobValidate(res.data); |
|
56 |
if (isBlob) { |
|
57 |
const blob = new Blob([res.data], { type: 'application/zip' }) |
|
58 |
this.saveAs(blob, name) |
|
59 |
} else { |
|
60 |
this.printErrMsg(res.data); |
|
61 |
} |
|
62 |
downloadLoadingInstance.close(); |
|
63 |
}).catch((r) => { |
|
64 |
console.error(r) |
|
65 |
Message.error('下载文件出现错误,请联系管理员!') |
|
66 |
downloadLoadingInstance.close(); |
|
67 |
}) |
|
68 |
}, |
|
69 |
saveAs(text, name, opts) { |
|
70 |
saveAs(text, name, opts); |
|
71 |
}, |
|
72 |
async printErrMsg(data) { |
|
73 |
const resText = await data.text(); |
|
74 |
const rspObj = JSON.parse(resText); |
|
75 |
const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default'] |
|
76 |
Message.error(errMsg); |
|
77 |
} |
|
78 |
} |
|
79 |
|