This commit is contained in:
RuoYi
2021-11-30 09:55:37 +08:00
parent aa35fd00da
commit a4656c6154
236 changed files with 16070 additions and 1 deletions

65
src/plugins/download.js Normal file
View File

@ -0,0 +1,65 @@
import axios from 'axios'
import { ElMessage } from 'element-plus'
import { saveAs } from 'file-saver'
import { getToken } from '@/utils/auth'
import { blobValidate } from '@/utils/ruoyi'
const baseURL = import.meta.env.VITE_APP_BASE_API
export default {
name(name, isDelete = true) {
var url = baseURL + "/common/download?fileName=" + encodeURI(name) + "&delete=" + isDelete
axios({
method: 'get',
url: url,
responseType: 'blob',
headers: { 'Authorization': 'Bearer ' + getToken() }
}).then(async (res) => {
const isLogin = await this.blobValidate(res.data);
if (isLogin) {
const blob = new Blob([res.data])
this.saveAs(blob, decodeURI(res.headers['download-filename']))
} else {
ElMessage.error('无效的会话,或者会话已过期,请重新登录。');
}
})
},
resource(resource) {
var url = baseURL + "/common/download/resource?resource=" + encodeURI(resource);
axios({
method: 'get',
url: url,
responseType: 'blob',
headers: { 'Authorization': 'Bearer ' + getToken() }
}).then(async (res) => {
const isLogin = await this.blobValidate(res.data);
if (isLogin) {
const blob = new Blob([res.data])
this.saveAs(blob, decodeURI(res.headers['download-filename']))
} else {
ElMessage.error('无效的会话,或者会话已过期,请重新登录。');
}
})
},
zip(url, name) {
var url = baseURL + url
axios({
method: 'get',
url: url,
responseType: 'blob',
headers: { 'Authorization': 'Bearer ' + getToken() }
}).then(async (res) => {
const isLogin = await blobValidate(res.data);
if (isLogin) {
const blob = new Blob([res.data], { type: 'application/zip' })
this.saveAs(blob, name)
} else {
ElMessage.error('无效的会话,或者会话已过期,请重新登录。');
}
})
},
saveAs(text, name, opts) {
saveAs(text, name, opts);
}
}