diff --git a/src/main.js b/src/main.js index d6aa150ef..2d35334db 100644 --- a/src/main.js +++ b/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") || "", diff --git a/src/utils/index.js b/src/utils/index.js index 0b5a858b0..ec1e45d0b 100644 --- a/src/utils/index.js +++ b/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' } \ No newline at end of file diff --git a/src/utils/request.js b/src/utils/request.js index 3696ebe00..e45df4aca 100644 --- a/src/utils/request.js +++ b/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