Browse Source

fix: 添加下载通用方法

featrue-lingshan
LL 4 months ago
parent
commit
bafc832c8b
  1. 3
      src/main.js
  2. 30
      src/utils/index.js
  3. 44
      src/utils/request.js

3
src/main.js

@ -50,6 +50,8 @@ import NameSplit from "@/components/NameSplit/index.vue";
import RelationGraph from "relation-graph";
import dayjs from 'dayjs'
import { globalDownload } from "@/utils/request";
// import AddNodeJw from "@/components/JwTree/addNode.vue";
//按钮
Vue.component("nodeWrap", NodeWrap);
@ -105,6 +107,7 @@ Vue.directive("fixed", {
Vue.prototype.$http = http;
Vue.prototype.$sensitive = desensitization;
Vue.prototype.$dayjs = dayjs
Vue.prototype.globalDownload = globalDownload
// el-uploader的header配置
Vue.prototype.$getElUploadHeaders = () => ({
Authorization: localStorage.getItem("token") || "",

30
src/utils/index.js

@ -208,4 +208,34 @@ export function encryptedData(key, data) {
encryptor.setPublicKey(key);
// 加密数据
return encryptor.encrypt(data);
}
/**
* 参数处理
* @param {*} params 参数
*/
export function tansParams(params) {
let result = ''
for (const propName of Object.keys(params)) {
const value = params[propName]
var part = encodeURIComponent(propName) + "="
if (value !== null && value !== "" && typeof (value) !== "undefined") {
if (typeof value === 'object') {
for (const key of Object.keys(value)) {
if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
let params = propName + '[' + key + ']'
var subPart = encodeURIComponent(params) + "="
result += subPart + encodeURIComponent(value[key]) + "&"
}
}
} else {
result += part + encodeURIComponent(value) + "&"
}
}
}
return result
}
// 验证是否为blob格式
export function blobValidate(data) {
return data.type !== 'application/json'
}

44
src/utils/request.js

@ -8,13 +8,16 @@
*
*/
import {message} from "@/utils/message.js";
import axios from 'axios'
import Cookies from 'js-cookie'
import router from '@/router'
import qs from 'qs'
import { clearLoginInfo } from '@/utils'
import isPlainObject from 'lodash/isPlainObject'
import { Loading, Message } from "element-ui";
import { tansParams, blobValidate } from "@/utils/index";
import { saveAs } from "file-saver";
let downloadLoadingInstance;
axios.defaults.withCredentials=true
axios.defaults.crossDomain=true
@ -90,4 +93,43 @@ http.interceptors.response.use(response => {
return Promise.reject(error)
})
// 通用下载方法
export function globalDownload(url, params, filename, config) {
downloadLoadingInstance = Loading.service({
text: "正在下载数据,请稍候",
spinner: "el-icon-loading",
background: "rgba(0, 0, 0, 0.7)",
});
return http
.post(url, params, {
transformRequest: [
(params) => {
return tansParams(params);
},
],
headers: { "Content-Type": "application/x-www-form-urlencoded" },
responseType: "blob",
...config,
})
.then(async (data) => {
const isBlob = blobValidate(data);
if (isBlob) {
const blob = new Blob([data]);
saveAs(blob, filename);
} else {
const resText = await data.text();
const rspObj = JSON.parse(resText);
const errMsg =
errorCode[rspObj.code] || rspObj.msg || errorCode["default"];
Message.error(errMsg);
}
downloadLoadingInstance.close();
})
.catch((r) => {
console.error(r);
Message.error("下载文件出现错误,请联系管理员!");
downloadLoadingInstance.close();
});
}
export default http

Loading…
Cancel
Save