You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
298 lines
7.0 KiB
298 lines
7.0 KiB
import request from "@/utils/request";
|
|
import { Message, MessageBox } from "element-ui";
|
|
|
|
/**
|
|
*
|
|
* @param {*} result 结果数组
|
|
* @param {*} predicate 断言函数
|
|
* @param {*} operationName 操作名称
|
|
*/
|
|
export const multiResultShow = (allItems, predicate, operationName) => {
|
|
const successItems = allItems.filter(predicate);
|
|
if (successItems.length == allItems.length) {
|
|
Message.success(`${operationName}成功`);
|
|
} else {
|
|
Message.error(
|
|
`${operationName}成功${successItems.length}个, 失败${
|
|
allItems.length - successItems.length
|
|
}个!`
|
|
);
|
|
}
|
|
};
|
|
|
|
export function toDecimal(x){
|
|
var f = parseFloat(x)
|
|
if(isNaN(f)){
|
|
return 0;
|
|
}
|
|
f = Math.round(x*100)/100;
|
|
return f;
|
|
}
|
|
|
|
export function delay(ms = 240) {
|
|
return new Promise((resolve) => setTimeout(() => resolve(void 0), ms));
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {{ method?: string; url: string; data?: string; type?: string; filename?: string; }} options
|
|
*/
|
|
export function exportImgFile({
|
|
url,
|
|
method = "post",
|
|
data,
|
|
type = "application/vnd.ms-excel",
|
|
filename = "download",
|
|
ext = "xlsx",
|
|
} = {}) {
|
|
if (!url) return;
|
|
const closeMessage = loadingMessage({ message: "文件下载中..." });
|
|
|
|
request({
|
|
url,
|
|
method,
|
|
responseType: "blob",
|
|
data: {
|
|
...data,
|
|
}
|
|
})
|
|
.then((result) => {
|
|
const blob = new Blob([result], { type });
|
|
const downloadElement = document.createElement("a");
|
|
const href = window.URL.createObjectURL(blob);
|
|
downloadElement.href = href;
|
|
downloadElement.download = result.filename || `${filename}.${ext}`;
|
|
document.body.appendChild(downloadElement);
|
|
downloadElement.click();
|
|
document.body.removeChild(downloadElement);
|
|
window.URL.revokeObjectURL(href);
|
|
|
|
Message.success("文件下载成功");
|
|
})
|
|
.catch((err) => {
|
|
Message.error("文件下载失败");
|
|
})
|
|
.finally(() => closeMessage());
|
|
}
|
|
/**
|
|
*
|
|
* @param {{ method?: string; url: string; data?: string; type?: string; filename?: string; }} options
|
|
*/
|
|
export function exportFile({
|
|
url,
|
|
method = "post",
|
|
data,
|
|
type = "application/vnd.ms-excel",
|
|
filename = "download",
|
|
ext = "xlsx",
|
|
} = {}) {
|
|
if (!url) return;
|
|
const closeMessage = loadingMessage({ message: "文件下载中..." });
|
|
|
|
request({
|
|
url,
|
|
method,
|
|
responseType: "blob",
|
|
data: {
|
|
...data,
|
|
},
|
|
params: {
|
|
...data,
|
|
},
|
|
})
|
|
.then((result) => {
|
|
const blob = new Blob([result], { type });
|
|
const downloadElement = document.createElement("a");
|
|
const href = window.URL.createObjectURL(blob);
|
|
downloadElement.href = href;
|
|
downloadElement.download = result.filename || `${filename}.${ext}`;
|
|
document.body.appendChild(downloadElement);
|
|
downloadElement.click();
|
|
document.body.removeChild(downloadElement);
|
|
window.URL.revokeObjectURL(href);
|
|
|
|
Message.success("文件下载成功");
|
|
})
|
|
.catch((err) => {
|
|
Message.error("文件下载失败");
|
|
})
|
|
.finally(() => closeMessage());
|
|
}
|
|
/**
|
|
*
|
|
* @param {{ method?: string; url: string; data?: string; type?: string; filename?: string; }} options
|
|
*/
|
|
export function exportFile2({
|
|
url,
|
|
method = "post",
|
|
data,
|
|
type = "application/vnd.ms-excel",
|
|
filename = "download",
|
|
ext = "xlsx",
|
|
} = {}) {
|
|
if (!url) return;
|
|
const closeMessage = loadingMessage({ message: "文件下载中..." });
|
|
const formData = new FormData();
|
|
for(let i in data){
|
|
formData.append(i, data[i]);
|
|
}
|
|
request({
|
|
url,
|
|
method,
|
|
responseType: "blob",
|
|
data: formData,
|
|
})
|
|
.then((result) => {
|
|
const blob = new Blob([result], { type });
|
|
const downloadElement = document.createElement("a");
|
|
const href = window.URL.createObjectURL(blob);
|
|
downloadElement.href = href;
|
|
downloadElement.download = result.filename || `${filename}.${ext}`;
|
|
document.body.appendChild(downloadElement);
|
|
downloadElement.click();
|
|
document.body.removeChild(downloadElement);
|
|
window.URL.revokeObjectURL(href);
|
|
|
|
Message.success("文件下载成功");
|
|
})
|
|
.catch((err) => {
|
|
Message.error("文件下载失败");
|
|
})
|
|
.finally(() => closeMessage());
|
|
}
|
|
/**
|
|
* Loading 状态的消息
|
|
* @param {ElMessageOptions} options
|
|
* @returns
|
|
*/
|
|
export function loadingMessage({ message, ...args } = {}) {
|
|
if (!message) return;
|
|
|
|
const loadingMessage = Message.info({
|
|
message,
|
|
duration: 0,
|
|
customClass: "loading-message",
|
|
iconClass: "el-icon-loading",
|
|
...args,
|
|
});
|
|
|
|
return () => loadingMessage.close();
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {{ message: string, title: string; [x: string]: any }} param0
|
|
* @returns
|
|
*/
|
|
export function confirm({
|
|
title = "提示",
|
|
message = "确定要执行该操作吗?",
|
|
...args
|
|
} = {}) {
|
|
return new Promise((resolve, reject) => {
|
|
MessageBox.confirm(message, title, {
|
|
confirmButtonText: "确定",
|
|
cancelButtonText: "取消",
|
|
type: "warning",
|
|
...args,
|
|
})
|
|
.then(() => {
|
|
resolve(true);
|
|
})
|
|
.catch(() => {
|
|
reject("取消 Confirm");
|
|
});
|
|
});
|
|
}
|
|
|
|
export function uploadFile(
|
|
{ url, method = "POST", data } = {},
|
|
{
|
|
// 接受的文件类型
|
|
accept = ".xls,.xlsx",
|
|
// 是否多选
|
|
multiple,
|
|
// 最大文件大小 (单位: MB)
|
|
maxSize = 15,
|
|
} = {}
|
|
) {
|
|
return new Promise((resolve, reject) => {
|
|
if (!url) return reject("未配置上传地址");
|
|
|
|
const fileDom = document.createElement("input");
|
|
|
|
fileDom.type = "file";
|
|
fileDom.setAttribute("type", "file");
|
|
accept && fileDom.setAttribute("accept", accept);
|
|
multiple && fileDom.setAttribute("multiple", "");
|
|
|
|
fileDom.click();
|
|
|
|
fileDom.oncancel = function () {
|
|
reject("取消 Upload");
|
|
};
|
|
|
|
fileDom.onchange = function () {
|
|
const [file] = fileDom.files;
|
|
|
|
if (!file) return reject("未选择文件");
|
|
|
|
if (file.size > 1024 * 1024 * maxSize) {
|
|
Message.error(`文件大小不能超过${maxSize}MB`);
|
|
reject(`文件大小不能超过${maxSize}MB`);
|
|
return;
|
|
}
|
|
|
|
const close = loadingMessage({ message: "文件上传中..." });
|
|
|
|
const formData = new FormData();
|
|
|
|
formData.append("file", file);
|
|
|
|
for (const key in data) {
|
|
formData.append(key, data[key]);
|
|
}
|
|
|
|
request({
|
|
url,
|
|
method,
|
|
headers: {
|
|
"Content-Type": "multipart/form-data",
|
|
},
|
|
data: formData,
|
|
})
|
|
.then((res) => {
|
|
if (res.code !== 200) {
|
|
Message.error("上传失败");
|
|
reject(res.msg);
|
|
return;
|
|
}
|
|
|
|
Message.success("上传成功");
|
|
|
|
resolve(res,file);
|
|
})
|
|
.catch((err) => {
|
|
reject(err);
|
|
})
|
|
.finally(() => {
|
|
close();
|
|
});
|
|
};
|
|
});
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {*} number
|
|
* @returns
|
|
*/
|
|
export function returnFileSize(number) {
|
|
if (number < 1024) {
|
|
return `${number} bytes`;
|
|
} else if (number >= 1024 && number < 1048576) {
|
|
return `${(number / 1024).toFixed(1)} KB`;
|
|
} else if (number >= 1048576) {
|
|
return `${(number / 1048576).toFixed(1)} MB`;
|
|
}
|
|
}
|
|
|