|
@@ -2,6 +2,9 @@
|
|
|
* 通用js方法封装处理
|
|
* 通用js方法封装处理
|
|
|
* Copyright (c) 2019 fs
|
|
* Copyright (c) 2019 fs
|
|
|
*/
|
|
*/
|
|
|
|
|
+import axios from 'axios'
|
|
|
|
|
+import { Message } from 'element-ui'
|
|
|
|
|
+import { getToken } from '@/utils/auth'
|
|
|
|
|
|
|
|
const baseURL = process.env.VUE_APP_BASE_API
|
|
const baseURL = process.env.VUE_APP_BASE_API
|
|
|
|
|
|
|
@@ -137,9 +140,38 @@ export function selectDictLabels(datas, value, separator) {
|
|
|
return actions.join('').substring(0, actions.join('').length - 1);
|
|
return actions.join('').substring(0, actions.join('').length - 1);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// 通用下载方法
|
|
|
|
|
|
|
+// 通用下载方法(须携带 Token;window.location 无法带 Authorization,鉴权后会 401)
|
|
|
export function download(fileName) {
|
|
export function download(fileName) {
|
|
|
- window.location.href = baseURL + "/common/download?fileName=" + encodeURI(fileName) + "&delete=" + true;
|
|
|
|
|
|
|
+ axios({
|
|
|
|
|
+ method: 'get',
|
|
|
|
|
+ url: baseURL + '/common/download?fileName=' + encodeURIComponent(fileName) + '&delete=' + true,
|
|
|
|
|
+ responseType: 'blob',
|
|
|
|
|
+ headers: { 'Authorization': 'Bearer ' + getToken() }
|
|
|
|
|
+ }).then(async (res) => {
|
|
|
|
|
+ const data = res.data
|
|
|
|
|
+ // 失败时后端可能返回 JSON(blob 形态),需识别并提示
|
|
|
|
|
+ if (data.type && data.type.indexOf('application/json') !== -1) {
|
|
|
|
|
+ const text = await data.text()
|
|
|
|
|
+ try {
|
|
|
|
|
+ const rsp = JSON.parse(text)
|
|
|
|
|
+ Message.error(rsp.msg || '下载失败')
|
|
|
|
|
+ } catch (e) {
|
|
|
|
|
+ Message.error('下载失败')
|
|
|
|
|
+ }
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ const blob = new Blob([data])
|
|
|
|
|
+ const aLink = document.createElement('a')
|
|
|
|
|
+ const name = decodeURIComponent(fileName)
|
|
|
|
|
+ aLink.href = URL.createObjectURL(blob)
|
|
|
|
|
+ aLink.setAttribute('download', name)
|
|
|
|
|
+ document.body.appendChild(aLink)
|
|
|
|
|
+ aLink.click()
|
|
|
|
|
+ document.body.removeChild(aLink)
|
|
|
|
|
+ URL.revokeObjectURL(aLink.href)
|
|
|
|
|
+ }).catch(() => {
|
|
|
|
|
+ Message.error('下载失败,请重新登录后重试')
|
|
|
|
|
+ })
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 字符串格式化(%s )
|
|
// 字符串格式化(%s )
|