将身份证号码中间部分替换为星号
export const maskIDCard = (idCard) => {
if (idCard && idCard.length > 7) {
let prefix = idCard.substring(0, 3);
let suffix = idCard.substring(idCard.length - 4);
let maskedPart = "*".repeat(idCard.length - 7);
return prefix + maskedPart + suffix;
} else {
return idCard;
}
}
文件下载
export const downloadFile = (url, fileName) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'blob';
xhr.onload = function () {
if (xhr.status === 200) {
const url = window.URL.createObjectURL(xhr.response);
const a = document.createElement('a');
a.href = url;
a.download = fileName;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
}
};
xhr.send();
}
判断文件地址是否为404并完成文件下载
checkFileExists(fileUrl) {
fetch(fileUrl)
.then((response) => {
if (response.status === 404) {
this.$message.error("文件不存在,请检查文件地址。");
} else {
this.$message.success("文件存在,可以下载。");
const xhr = new XMLHttpRequest();
xhr.open("GET", fileUrl, true);
xhr.responseType = "blob";
xhr.onload = function () {
if (this.status === 200) {
const blob = this.response;
const link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.download = "自定义文件名称.pdf";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(link.href);
}
};
xhr.send();
}
})
.catch((error) => {
console.error("请求出错:", error);
});
},
获取地址栏上的参数
export const getURLParams = () => {
let url = window.location.href
let params = {}
let queryString = url.split('?')[1]
if (queryString) {
let pairs = queryString.split('&')
for (let pair of pairs) {
let [key, value] = pair.split('=')
params[key] = decodeURIComponent(value)
}
}
return params
}