|
@@ -1,14 +1,13 @@
|
|
import { ElMessage } from "element-plus";
|
|
import { ElMessage } from "element-plus";
|
|
import { utils, writeFile } from "xlsx";
|
|
import { utils, writeFile } from "xlsx";
|
|
import { httpExport } from "/@/api/export";
|
|
import { httpExport } from "/@/api/export";
|
|
-import { responseHandle } from "./responseHandle";
|
|
|
|
|
|
|
|
type Options<T> = {
|
|
type Options<T> = {
|
|
//导出方式 http(网络请求) front(纯前端导出)
|
|
//导出方式 http(网络请求) front(纯前端导出)
|
|
type: "front" | "http";
|
|
type: "front" | "http";
|
|
//type为http时需要url作为请求url
|
|
//type为http时需要url作为请求url
|
|
url?: string;
|
|
url?: string;
|
|
- //type未http时请求的参数
|
|
|
|
|
|
+ //type为http时请求的参数
|
|
params?: any;
|
|
params?: any;
|
|
//type为front时 需要传入导出的数据
|
|
//type为front时 需要传入导出的数据
|
|
data?: T[];
|
|
data?: T[];
|
|
@@ -16,14 +15,27 @@ type Options<T> = {
|
|
columns?: any[];
|
|
columns?: any[];
|
|
//导出文件名
|
|
//导出文件名
|
|
name?: string;
|
|
name?: string;
|
|
|
|
+ //导出文件类型
|
|
|
|
+ httpType?: "zip" | "excel";
|
|
};
|
|
};
|
|
-
|
|
|
|
|
|
+const httpTypeList = [
|
|
|
|
+ {
|
|
|
|
+ key: "zip",
|
|
|
|
+ value: "aplication/zip",
|
|
|
|
+ fileName: "zip"
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ key: "excel",
|
|
|
|
+ value: "application/vnd.ms-excel",
|
|
|
|
+ fileName: "xlsx"
|
|
|
|
+ }
|
|
|
|
+];
|
|
//要排除的列
|
|
//要排除的列
|
|
const EXCLUDE_COLUMNS = ["序号", "操作"];
|
|
const EXCLUDE_COLUMNS = ["序号", "操作"];
|
|
|
|
|
|
//纯前端导出
|
|
//纯前端导出
|
|
function frontEndExport<T>(
|
|
function frontEndExport<T>(
|
|
- _options: Pick<Options<T>, "data" | "columns" | "name">
|
|
|
|
|
|
+ _options: Pick<Options<T>, "data" | "columns" | "name" | "httpType">
|
|
) {
|
|
) {
|
|
const { data, columns, name } = _options;
|
|
const { data, columns, name } = _options;
|
|
|
|
|
|
@@ -44,48 +56,37 @@ function frontEndExport<T>(
|
|
utils.book_append_sheet(workBook, workSheet, "sheet1");
|
|
utils.book_append_sheet(workBook, workSheet, "sheet1");
|
|
|
|
|
|
//导出
|
|
//导出
|
|
- writeFile(workBook, name);
|
|
|
|
|
|
+ writeFile(workBook, name + ".xlsx");
|
|
}
|
|
}
|
|
|
|
|
|
//配合后端文件流导出
|
|
//配合后端文件流导出
|
|
async function httpRequsetExport<T>(
|
|
async function httpRequsetExport<T>(
|
|
- _options: Pick<Options<T>, "url" | "name" | "params">
|
|
|
|
|
|
+ _options: Pick<Options<T>, "url" | "name" | "params" | "httpType">
|
|
) {
|
|
) {
|
|
if (!_options.url) return;
|
|
if (!_options.url) return;
|
|
|
|
+ if (!_options.httpType) _options.httpType = "zip";
|
|
|
|
+ const { value: httpHeader, fileName } =
|
|
|
|
+ httpTypeList.find(item => item.key === _options.httpType) || {};
|
|
|
|
|
|
- const { code, message, data } = await httpExport(_options.url, {
|
|
|
|
|
|
+ const res = await httpExport(_options.url, {
|
|
data: _options.params
|
|
data: _options.params
|
|
});
|
|
});
|
|
-
|
|
|
|
- function handler() {
|
|
|
|
- const url = window.URL.createObjectURL(
|
|
|
|
- //todo:blob对象类型
|
|
|
|
- new Blob([data], {
|
|
|
|
- type: ""
|
|
|
|
- })
|
|
|
|
- );
|
|
|
|
-
|
|
|
|
- //创建a标签 执行下载
|
|
|
|
- const link = document.createElement("a");
|
|
|
|
- link.style.display = "none";
|
|
|
|
- link.href = url;
|
|
|
|
- link.setAttribute("download", _options.name);
|
|
|
|
-
|
|
|
|
- //删除a标签 释放url
|
|
|
|
- document.appendChild(link);
|
|
|
|
- link.click();
|
|
|
|
- link.remove();
|
|
|
|
- window.URL.revokeObjectURL(url);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- responseHandle({
|
|
|
|
- code,
|
|
|
|
- handler,
|
|
|
|
- message,
|
|
|
|
- logout: () => {
|
|
|
|
- console.log("todo:登出");
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
|
|
+ const url = window.URL.createObjectURL(
|
|
|
|
+ //todo:blob对象类型
|
|
|
|
+ new Blob([res], {
|
|
|
|
+ type: httpHeader
|
|
|
|
+ })
|
|
|
|
+ ); //创建a标签 执行下载
|
|
|
|
+ const link = document.createElement("a");
|
|
|
|
+ link.style.display = "none";
|
|
|
|
+ link.href = url;
|
|
|
|
+ link.setAttribute("download", _options.name + "." + fileName);
|
|
|
|
+
|
|
|
|
+ //删除a标签 释放url
|
|
|
|
+ document.body.appendChild(link);
|
|
|
|
+ link.click();
|
|
|
|
+ link.remove();
|
|
|
|
+ window.URL.revokeObjectURL(url);
|
|
}
|
|
}
|
|
|
|
|
|
function generatorResult<T>({
|
|
function generatorResult<T>({
|
|
@@ -114,15 +115,16 @@ export function exportPageContent<T = any>({
|
|
url,
|
|
url,
|
|
columns,
|
|
columns,
|
|
data,
|
|
data,
|
|
- name: _name
|
|
|
|
|
|
+ name: _name,
|
|
|
|
+ httpType
|
|
}: Options<T>) {
|
|
}: Options<T>) {
|
|
- const name = _name ? _name : "导出数据.xlsx";
|
|
|
|
|
|
+ const name = _name ? _name : "导出数据";
|
|
switch (type) {
|
|
switch (type) {
|
|
case "front":
|
|
case "front":
|
|
frontEndExport({ data, columns, name });
|
|
frontEndExport({ data, columns, name });
|
|
break;
|
|
break;
|
|
case "http":
|
|
case "http":
|
|
- httpRequsetExport({ url, name });
|
|
|
|
|
|
+ httpRequsetExport({ url, name, httpType });
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|