|
@@ -1,13 +1,15 @@
|
|
import { ElMessage } from "element-plus";
|
|
import { ElMessage } from "element-plus";
|
|
import { utils, writeFile } from "xlsx";
|
|
import { utils, writeFile } from "xlsx";
|
|
-import { responseHandle } from "./responseHandle";
|
|
|
|
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时请求的参数
|
|
|
|
+ params?: any;
|
|
//type为front时 需要传入导出的数据
|
|
//type为front时 需要传入导出的数据
|
|
data?: T[]
|
|
data?: T[]
|
|
//ttpe为front时 需要传入导出的列
|
|
//ttpe为front时 需要传入导出的列
|
|
@@ -19,8 +21,10 @@ type Options<T> = {
|
|
//要排除的列
|
|
//要排除的列
|
|
const EXCLUDE_COLUMNS = ['序号', '操作']
|
|
const EXCLUDE_COLUMNS = ['序号', '操作']
|
|
|
|
|
|
|
|
+//纯前端导出
|
|
|
|
+function frontEndExport<T>(_options: Pick<Options<T>, 'data' | 'columns' | 'name'>) {
|
|
|
|
+ const { data, columns, name } = _options;
|
|
|
|
|
|
-function frontEndExport<T>({ data, columns, name }: Pick<Options<T>, 'data' | 'columns' | 'name'>) {
|
|
|
|
if (!data || !columns) return
|
|
if (!data || !columns) return
|
|
if (data.length === 0) return ElMessage.warning('请打开勾选列并勾选导出数据')
|
|
if (data.length === 0) return ElMessage.warning('请打开勾选列并勾选导出数据')
|
|
|
|
|
|
@@ -38,23 +42,30 @@ function frontEndExport<T>({ data, columns, name }: Pick<Options<T>, 'data' | 'c
|
|
writeFile(workBook, name)
|
|
writeFile(workBook, name)
|
|
}
|
|
}
|
|
|
|
|
|
-async function httpRequsetExport<T>(_options: Pick<Options<T>, 'url' | 'name'>) {
|
|
|
|
|
|
+//配合后端文件流导出
|
|
|
|
+async function httpRequsetExport<T>(_options: Pick<Options<T>, 'url' | 'name' | 'params'>) {
|
|
if (!_options.url) return
|
|
if (!_options.url) return
|
|
|
|
|
|
- const { code, message, data } = await httpExport(_options.url);
|
|
|
|
|
|
+
|
|
|
|
+ const { code, message, data } = await httpExport(_options.url, {
|
|
|
|
+ data: _options.params
|
|
|
|
+ });
|
|
|
|
|
|
function handler() {
|
|
function handler() {
|
|
const url = window.URL.createObjectURL(
|
|
const url = window.URL.createObjectURL(
|
|
|
|
+ //todo:blob对象类型
|
|
new Blob([data], {
|
|
new Blob([data], {
|
|
type: ''
|
|
type: ''
|
|
})
|
|
})
|
|
)
|
|
)
|
|
|
|
|
|
|
|
+ //创建a标签 执行下载
|
|
const link = document.createElement('a');
|
|
const link = document.createElement('a');
|
|
link.style.display = 'none'
|
|
link.style.display = 'none'
|
|
link.href = url
|
|
link.href = url
|
|
link.setAttribute('download', _options.name)
|
|
link.setAttribute('download', _options.name)
|
|
|
|
|
|
|
|
+ //删除a标签 释放url
|
|
document.appendChild(link)
|
|
document.appendChild(link)
|
|
link.click();
|
|
link.click();
|
|
link.remove();
|
|
link.remove();
|
|
@@ -97,6 +108,8 @@ export function exportPageContent<T = any>({ type, url, columns, data, name: _na
|
|
case 'http':
|
|
case 'http':
|
|
httpRequsetExport({ url, name })
|
|
httpRequsetExport({ url, name })
|
|
break;
|
|
break;
|
|
|
|
+ default:
|
|
|
|
+ break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|