加密方法
@/utils/aseEncrypt.js文件
import CryptoJS from "crypto-js";
const AES_KEY = "c19fb27e0cf61482a38819fbe5e29e17";
const AES_IV = "0102030405060708";
const key = CryptoJS.enc.Utf8.parse(AES_KEY);
const iv = CryptoJS.enc.Utf8.parse(AES_IV);
export function aesEncrypt(word) {
const srcs = CryptoJS.enc.Utf8.parse(word);
const encrypted = CryptoJS.AES.encrypt(srcs, key, {
iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.toString();
}
export function aseDecrypt(word) {
const decrypted = CryptoJS.AES.decrypt(word, key, {
iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return CryptoJS.enc.Utf8.stringify(decrypted).toString();
}
文件下载
fileUpload.js
import axios from "axios";
import { Message } from "element-ui";
import { getToken } from "@/utils/auth";
import store from "../store";
import Cookies from "js-cookie";
import { aesEncrypt } from "@/utils/aseEncrypt";
export function downloadByData(data, filename, mime, bom) {
const blobData = typeof bom !== "undefined" ? [bom, data] : [data];
const blob = new Blob(blobData, { type: mime || "application/octet-stream" });
const blobURL = window.URL.createObjectURL(blob);
const tempLink = document.createElement("a");
tempLink.style.display = "none";
tempLink.href = blobURL;
tempLink.setAttribute("download", filename);
if (typeof tempLink.download === "undefined") {
tempLink.setAttribute("target", "_blank");
}
document.body.appendChild(tempLink);
tempLink.click();
document.body.removeChild(tempLink);
window.URL.revokeObjectURL(blobURL);
}
export function downloadByUrl({ url, target = "_blank", fileName }) {
const isChrome =
window.navigator.userAgent.toLowerCase().indexOf("chrome") > -1;
const isSafari =
window.navigator.userAgent.toLowerCase().indexOf("safari") > -1;
if (/(iP)/g.test(window.navigator.userAgent)) {
console.error("Your browser does not support download!");
return false;
}
if (isChrome || isSafari) {
const link = document.createElement("a");
link.href = url;
link.target = target;
if (link.download !== undefined) {
console.log("url", url);
link.download =
fileName || url.substring(url.lastIndexOf("/") + 1, url.length);
}
if (document.createEvent) {
const e = document.createEvent("MouseEvents");
e.initEvent("click", true, true);
link.dispatchEvent(e);
return true;
}
}
if (url.indexOf("?") === -1) {
url += "?download";
}
openWindow(url, { target });
return true;
}
export function downloadByAxios(
data,
url = "/common/downloadFile",
method = "POST"
) {
if (
typeof data == "object" &&
Object.prototype.toString.call(data).toLowerCase() == "[object object]" &&
!data.length &&
Object.keys(data).length
) {
console.log("下载入参 data", data);
let rsaData = aesEncrypt(JSON.stringify(data.data));
let params = { ...data, buryingPoint: rsaData };
if (params.hasOwnProperty("data")) {
delete params.data;
}
const service = axios.create({
baseURL: process.env.VUE_APP_BASE_API
});
return service
.request({
url,
method,
headers: {
"Content-Type": "application/x-download;charset=utf-8",
Authorization: getToken()
},
responseType: "blob",
params
})
.then(res => {
const data = res.data;
let fileName = "";
if (
res.headers["content-disposition"] ||
res.headers["Content-Disposition"]
) {
try {
fileName = res.headers["content-disposition"].replace(
/\w+;filename=(.*)/,
"$1"
);
} catch (error) {
fileName = res.headers["Content-Disposition"].replace(
/\w+;filename=(.*)/,
"$1"
);
}
} else {
fileName = res.data.fileName;
}
const isBlob = res.data.type !== "application/json";
if (isBlob) {
const blob = new Blob([res.data]);
downloadByData(blob, decodeURIComponent(fileName));
} else {
Message.error("下载文件出现错误");
}
})
.catch(r => {
console.log("r", r.response.status);
if (r.response.status === 401) {
store.dispatch("LogOut").then(() => {
Cookies.set("point", 401);
location.reload();
});
} else {
Message.error("下载文件出现错误");
}
});
} else {
console.error("下载文件: 入参为空或错误");
}
}