|
@@ -142,8 +142,6 @@ import printJs from 'print-js';
|
|
|
//import { EmptyObjectType } from "/@/types/global";
|
|
//import { EmptyObjectType } from "/@/types/global";
|
|
|
import formatter from '/@/components/table/formatter.vue';
|
|
import formatter from '/@/components/table/formatter.vue';
|
|
|
import { useThemeConfig } from '/@/stores/themeConfig';
|
|
import { useThemeConfig } from '/@/stores/themeConfig';
|
|
|
-import { exportExcel } from '/@/utils/exportExcel'; //TODO: 此包会引起浏览器控制台报 Module "stream" has been externalized for browser compatibility. Cannot access "stream.Readable" in client code. 警告,建议替换
|
|
|
|
|
-
|
|
|
|
|
// 定义父组件传过来的值
|
|
// 定义父组件传过来的值
|
|
|
const props = defineProps({
|
|
const props = defineProps({
|
|
|
// 获取数据的方法,由父组件传递
|
|
// 获取数据的方法,由父组件传递
|
|
@@ -227,6 +225,15 @@ const getProperty = (obj: any, property: any) => {
|
|
|
return value;
|
|
return value;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
+/** 分页结果可能为 items/total(camelCase)或 Items/Total(PascalCase),统一读取 */
|
|
|
|
|
+const readPagedList = (raw: any): { items: EmptyObjectType[]; total: number } | null => {
|
|
|
|
|
+ if (!raw || typeof raw !== 'object') return null;
|
|
|
|
|
+ const items = raw.items ?? raw.Items;
|
|
|
|
|
+ if (!Array.isArray(items)) return null;
|
|
|
|
|
+ const total = raw.total ?? raw.Total ?? 0;
|
|
|
|
|
+ return { items, total: Number(total) || 0 };
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
// 设置边框显示/隐藏
|
|
// 设置边框显示/隐藏
|
|
|
const setBorder = computed(() => {
|
|
const setBorder = computed(() => {
|
|
|
return props.config.isBorder ? true : false;
|
|
return props.config.isBorder ? true : false;
|
|
@@ -287,35 +294,40 @@ const pageReset = () => {
|
|
|
// 导出当前页
|
|
// 导出当前页
|
|
|
const onExportTable = () => {
|
|
const onExportTable = () => {
|
|
|
if (setHeader.value.length <= 0) return ElMessage.error('没有勾选要导出的列');
|
|
if (setHeader.value.length <= 0) return ElMessage.error('没有勾选要导出的列');
|
|
|
- exportData(state.data);
|
|
|
|
|
|
|
+ void exportData(state.data);
|
|
|
};
|
|
};
|
|
|
// 全部导出
|
|
// 全部导出
|
|
|
const onExportTableAll = async () => {
|
|
const onExportTableAll = async () => {
|
|
|
if (setHeader.value.length <= 0) return ElMessage.error('没有勾选要导出的列');
|
|
if (setHeader.value.length <= 0) return ElMessage.error('没有勾选要导出的列');
|
|
|
- state.exportLoading = true;
|
|
|
|
|
const param = Object.assign({}, props.param, { page: 1, pageSize: 9999999 });
|
|
const param = Object.assign({}, props.param, { page: 1, pageSize: 9999999 });
|
|
|
const res = await props.getData(param);
|
|
const res = await props.getData(param);
|
|
|
- state.exportLoading = false;
|
|
|
|
|
- const data = res.result?.items ?? [];
|
|
|
|
|
- exportData(data);
|
|
|
|
|
|
|
+ const data = readPagedList(res?.result)?.items ?? [];
|
|
|
|
|
+ await exportData(data);
|
|
|
};
|
|
};
|
|
|
-// 导出方法
|
|
|
|
|
-const exportData = (data: Array<EmptyObjectType>) => {
|
|
|
|
|
|
|
+// 导出方法(动态加载 xlsx-js-style,避免表格页首屏拉取 Node stream 相关代码导致运行时报错)
|
|
|
|
|
+const exportData = async (data: Array<EmptyObjectType>) => {
|
|
|
if (data.length <= 0) return ElMessage.error('没有数据可以导出');
|
|
if (data.length <= 0) return ElMessage.error('没有数据可以导出');
|
|
|
state.exportLoading = true;
|
|
state.exportLoading = true;
|
|
|
- let exportData = JSON.parse(JSON.stringify(data));
|
|
|
|
|
- if (props.exportChangeData) {
|
|
|
|
|
- exportData = props.exportChangeData(exportData);
|
|
|
|
|
- }
|
|
|
|
|
- exportExcel(
|
|
|
|
|
- exportData,
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ let rows = JSON.parse(JSON.stringify(data));
|
|
|
|
|
+ if (props.exportChangeData) {
|
|
|
|
|
+ rows = props.exportChangeData(rows);
|
|
|
|
|
+ }
|
|
|
|
|
+ const { exportExcel } = await import('/@/utils/exportExcel');
|
|
|
|
|
+ await exportExcel(
|
|
|
|
|
+ rows,
|
|
|
`${props.config.exportFileName ? props.config.exportFileName : themeConfig.value.globalTitle}_${new Date().toLocaleString()}`,
|
|
`${props.config.exportFileName ? props.config.exportFileName : themeConfig.value.globalTitle}_${new Date().toLocaleString()}`,
|
|
|
setHeader.value.filter((item) => {
|
|
setHeader.value.filter((item) => {
|
|
|
return item.type != 'action';
|
|
return item.type != 'action';
|
|
|
}),
|
|
}),
|
|
|
'导出数据'
|
|
'导出数据'
|
|
|
- );
|
|
|
|
|
- state.exportLoading = false;
|
|
|
|
|
|
|
+ );
|
|
|
|
|
+ } catch (e) {
|
|
|
|
|
+ console.error(e);
|
|
|
|
|
+ ElMessage.error('导出失败');
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ state.exportLoading = false;
|
|
|
|
|
+ }
|
|
|
};
|
|
};
|
|
|
// 打印
|
|
// 打印
|
|
|
const onPrintTable = () => {
|
|
const onPrintTable = () => {
|
|
@@ -441,13 +453,17 @@ const onRefreshTable = async () => {
|
|
|
Object.keys(param).forEach((key) => param[key] === undefined && delete param[key]);
|
|
Object.keys(param).forEach((key) => param[key] === undefined && delete param[key]);
|
|
|
const res = await props.getData(param);
|
|
const res = await props.getData(param);
|
|
|
state.loading = false;
|
|
state.loading = false;
|
|
|
- if (res && res.result && res.result.items) {
|
|
|
|
|
|
|
+ const paged = readPagedList(res?.result);
|
|
|
|
|
+ if (paged) {
|
|
|
state.showPagination = true;
|
|
state.showPagination = true;
|
|
|
- state.data = res.result?.items ?? [];
|
|
|
|
|
- state.total = res.result?.total ?? 0;
|
|
|
|
|
|
|
+ state.data = paged.items;
|
|
|
|
|
+ state.total = paged.total;
|
|
|
|
|
+ } else if (Array.isArray(res?.result)) {
|
|
|
|
|
+ state.showPagination = false;
|
|
|
|
|
+ state.data = res.result;
|
|
|
} else {
|
|
} else {
|
|
|
state.showPagination = false;
|
|
state.showPagination = false;
|
|
|
- state.data = res && res.result ? res.result : [];
|
|
|
|
|
|
|
+ state.data = [];
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
|
|
|