2021-09-27 10:38:29 +08:00
|
|
|
import axios from 'axios'
|
2021-10-23 10:23:32 +08:00
|
|
|
import { Message } from 'element-ui'
|
2021-11-17 11:57:17 +08:00
|
|
|
import { saveAs } from 'file-saver'
|
|
|
|
import { getToken } from '@/utils/auth'
|
|
|
|
import { blobValidate } from "@/utils/ruoyi";
|
2021-09-27 10:38:29 +08:00
|
|
|
|
|
|
|
const baseURL = process.env.VUE_APP_BASE_API
|
|
|
|
|
|
|
|
export default {
|
2021-09-27 17:24:37 +08:00
|
|
|
excel(url, params) {
|
|
|
|
// get请求映射params参数
|
|
|
|
if (params) {
|
|
|
|
let urlparams = url + '?';
|
|
|
|
for (const propName of Object.keys(params)) {
|
|
|
|
const value = params[propName];
|
|
|
|
var part = encodeURIComponent(propName) + "=";
|
|
|
|
if (value !== null && typeof(value) !== "undefined") {
|
|
|
|
if (typeof value === 'object') {
|
|
|
|
for (const key of Object.keys(value)) {
|
|
|
|
if (value[key] !== null && typeof (value[key]) !== 'undefined') {
|
|
|
|
let params = propName + '[' + key + ']';
|
|
|
|
let subPart = encodeURIComponent(params) + '=';
|
|
|
|
urlparams += subPart + encodeURIComponent(value[key]) + '&';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
urlparams += part + encodeURIComponent(value) + "&";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
urlparams = urlparams.slice(0, -1);
|
|
|
|
url = urlparams;
|
|
|
|
}
|
|
|
|
url = baseURL + url
|
2021-09-27 10:38:29 +08:00
|
|
|
axios({
|
|
|
|
method: 'get',
|
|
|
|
url: url,
|
|
|
|
responseType: 'blob',
|
|
|
|
headers: { 'Authorization': 'Bearer ' + getToken() }
|
2021-10-23 10:23:32 +08:00
|
|
|
}).then(async (res) => {
|
|
|
|
const isLogin = await this.blobValidate(res.data);
|
|
|
|
if (isLogin) {
|
2021-10-25 18:24:55 +08:00
|
|
|
const blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
|
2021-10-23 10:23:32 +08:00
|
|
|
this.saveAs(blob, decodeURI(res.headers['download-filename']))
|
|
|
|
} else {
|
2021-10-25 18:24:55 +08:00
|
|
|
Message.error('无效的会话,或者会话已过期,请重新登录。');
|
2021-10-23 10:23:32 +08:00
|
|
|
}
|
2021-09-27 10:38:29 +08:00
|
|
|
})
|
|
|
|
},
|
2021-09-27 21:30:07 +08:00
|
|
|
oss(ossId) {
|
2021-09-27 17:24:37 +08:00
|
|
|
var url = baseURL + '/system/oss/download/' + ossId
|
2021-09-27 10:38:29 +08:00
|
|
|
axios({
|
|
|
|
method: 'get',
|
|
|
|
url: url,
|
|
|
|
responseType: 'blob',
|
|
|
|
headers: { 'Authorization': 'Bearer ' + getToken() }
|
2021-10-23 10:23:32 +08:00
|
|
|
}).then(async (res) => {
|
|
|
|
const isLogin = await this.blobValidate(res.data);
|
|
|
|
if (isLogin) {
|
2021-10-25 18:24:55 +08:00
|
|
|
const blob = new Blob([res.data], { type: 'application/octet-stream' })
|
2021-10-23 10:23:32 +08:00
|
|
|
this.saveAs(blob, decodeURI(res.headers['download-filename']))
|
|
|
|
} else {
|
|
|
|
Message.error('无效的会话,或者会话已过期,请重新登录。');
|
|
|
|
}
|
2021-09-27 10:38:29 +08:00
|
|
|
})
|
|
|
|
},
|
|
|
|
zip(url, name) {
|
|
|
|
var url = baseURL + url
|
|
|
|
axios({
|
|
|
|
method: 'get',
|
|
|
|
url: url,
|
|
|
|
responseType: 'blob',
|
|
|
|
headers: { 'Authorization': 'Bearer ' + getToken() }
|
2021-10-23 10:23:32 +08:00
|
|
|
}).then(async (res) => {
|
2021-11-17 11:57:17 +08:00
|
|
|
const isLogin = await blobValidate(res.data);
|
2021-10-23 10:23:32 +08:00
|
|
|
if (isLogin) {
|
|
|
|
const blob = new Blob([res.data], { type: 'application/zip' })
|
|
|
|
this.saveAs(blob, name)
|
|
|
|
} else {
|
|
|
|
Message.error('无效的会话,或者会话已过期,请重新登录。');
|
|
|
|
}
|
2021-09-27 10:38:29 +08:00
|
|
|
})
|
|
|
|
},
|
|
|
|
saveAs(text, name, opts) {
|
|
|
|
saveAs(text, name, opts);
|
2021-11-17 11:57:17 +08:00
|
|
|
}
|
2021-09-27 10:38:29 +08:00
|
|
|
}
|
|
|
|
|