提交 | 用户 | 时间
|
722af2
|
1 |
import axios from 'axios' |
X |
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/dl"; |
|
7 |
|
|
8 |
const baseURL = process.env.VUE_APP_BASE_API |
|
9 |
let downloadLoadingInstance; |
|
10 |
|
|
11 |
export default { |
|
12 |
oss(ossId) { |
|
13 |
var url = baseURL + '/system/oss/download/' + ossId |
|
14 |
downloadLoadingInstance = Loading.service({ text: "正在下载数据,请稍候", spinner: "el-icon-loading", background: "rgba(0, 0, 0, 0.7)", }) |
|
15 |
axios({ |
|
16 |
method: 'get', |
|
17 |
url: url, |
|
18 |
responseType: 'blob', |
|
19 |
headers: { 'Authorization': 'Bearer ' + getToken() } |
|
20 |
}).then((res) => { |
|
21 |
const isBlob = blobValidate(res.data); |
|
22 |
if (isBlob) { |
|
23 |
const blob = new Blob([res.data], { type: 'application/octet-stream' }) |
|
24 |
this.saveAs(blob, decodeURIComponent(res.headers['download-filename'])) |
|
25 |
} else { |
|
26 |
this.printErrMsg(res.data); |
|
27 |
} |
|
28 |
downloadLoadingInstance.close(); |
|
29 |
}).catch((r) => { |
|
30 |
console.error(r) |
|
31 |
Message.error('下载文件出现错误,请联系管理员!') |
|
32 |
downloadLoadingInstance.close(); |
|
33 |
}) |
|
34 |
}, |
|
35 |
zip(url, name) { |
|
36 |
var url = baseURL + url |
|
37 |
axios({ |
|
38 |
method: 'get', |
|
39 |
url: url, |
|
40 |
responseType: 'blob', |
|
41 |
headers: { |
|
42 |
'Authorization': 'Bearer ' + getToken(), |
|
43 |
'datasource': localStorage.getItem("dataName") |
|
44 |
} |
|
45 |
}).then((res) => { |
|
46 |
const isBlob = blobValidate(res.data); |
|
47 |
if (isBlob) { |
|
48 |
const blob = new Blob([res.data], { type: 'application/zip' }) |
|
49 |
this.saveAs(blob, name) |
|
50 |
} else { |
|
51 |
this.printErrMsg(res.data); |
|
52 |
} |
|
53 |
}) |
|
54 |
}, |
|
55 |
saveAs(text, name, opts) { |
|
56 |
saveAs(text, name, opts); |
|
57 |
}, |
|
58 |
async printErrMsg(data) { |
|
59 |
const resText = await data.text(); |
|
60 |
const rspObj = JSON.parse(resText); |
|
61 |
const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default'] |
|
62 |
Message.error(errMsg); |
|
63 |
} |
|
64 |
} |
|
65 |
|