yjwang 1 روز پیش
والد
کامیت
eccbf5b31f
1فایلهای تغییر یافته به همراه33 افزوده شده و 2 حذف شده
  1. 33 2
      src/utils/common.js

+ 33 - 2
src/utils/common.js

@@ -2,6 +2,9 @@
  * 通用js方法封装处理
  * 通用js方法封装处理
  * Copyright (c) 2019 ruoyi
  * Copyright (c) 2019 ruoyi
  */
  */
+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
 
 
@@ -100,9 +103,37 @@ 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
+		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 )