generator-min-app
Version:
hcy system layout and pages
187 lines (170 loc) • 4.19 kB
text/typescript
import { deleteTableData, getTableData } from '@/services/common/components';
import type { ProColumns } from '@ant-design/pro-components';
import { message } from 'antd';
const ExportJsonExcel = require('js-export-excel');
type TableListItem = {
[key: string]: any;
};
// 封装方法 使用react-query请求数据 导出excel
// 传入url和fileName
// 返回导出结果
//exportDate批量导出的数组
export const exportExcelData = async (
url: string,
fileName: string,
columns: ProColumns<TableListItem>[],
params?: {
[key: string]: any;
},
exportDate?: any,
) => {
const saveExcel = (
filename: any,
sheetHeader: any,
sheetFilter: any,
sheetData: any,
) => {
const options = {
fileName: filename,
datas: [
{
sheetHeader,
sheetFilter,
sheetData,
},
],
};
const excel = new ExportJsonExcel(options);
excel.saveExcel();
};
const exportExcel = (
filename: string,
columns: ProColumns<TableListItem>[],
data: TableListItem[],
) => {
const sheetHeader = columns.map((column) => {
if (column.valueType !== 'option' && !column.hideInTable) {
return column.title;
}
return '';
});
const sheetFilter = columns.map((column) => column.dataIndex);
const sheetData = data.map((rowInput: any) => {
const rowOutput: TableListItem = {};
columns.forEach(({ key, renderText }) => {
if (key) {
rowOutput[key] = renderText
? renderText(rowInput[key], {}, 0, rowInput)
: rowInput[key];
}
});
return rowOutput;
});
saveExcel(filename, sheetHeader, sheetFilter, sheetData);
};
if (!exportDate.length) {
const res = await getTableData(url, {
...params,
pageNum: 1,
pageSize: 10000,
});
exportExcel(fileName, columns, res.data.items);
} else {
exportExcel(fileName, columns, exportDate);
}
};
// 删除table数据
// 传入url和ids
// 返回删除结果
export const deleteListData = async (
url: string,
ids: any,
// 成功回调函数
successFunction: any,
) => {
const res = await deleteTableData(url, ids);
if (res.success) {
message.success(res.msg);
successFunction();
}
// return res;
};
// 表格详情展示排序
// 传入表格数据和排序规则
// 返回排序后的表格数据
// 传入参数参考如下
/*
// 排序
const order: Record<string, number> = {
privilegeName: 1,
privilegeAlias: 2,
privilegeNumber: 3,
sort: 4,
type: 5,
isOpenWindow: 6,
parentId: 7,
privilegeIcon: 8,
privilegeUrl: 9,
updateTime: 10,
remark: 11,
};
// 类型规则
const typeRules=(key: string)=>{
return key === 'privilegeIcon' ? 'icon' : 'text';
};
// 标题双语
const titleFunction=(key: string)=>{
return intl.formatMessage({ id: `menu.columns.title.${key}` });
};
// span规则
const spanRules=(key: string)=>{
return key === 'privilegeRemark' || key === 'privilegeIcon' || key === 'parentId' ? 2 : 1;
};
*/
export const sortDataDetails = (
data: any,
order: {
[key: string]: number;
},
titleFunction: any,
typeRules: any,
spanRules: any,
detailsRule: any,
) => {
const dataIndex: {
key: string;
index: number;
}[] = [];
const listData: {
title: string;
description: string;
type: 'text' | 'icon' | 'image' | 'file';
name: string;
span?: number | undefined;
}[] = [];
Object.keys(data).forEach((key: string) => {
dataIndex.push({
key,
index: order[key],
});
});
const newOrder = dataIndex.sort((a, b) => {
const indexA = order[a.key];
const indexB = order[b.key];
return (
(indexA === undefined ? 1 : indexA) - (indexB === undefined ? 1 : indexB)
);
});
newOrder.forEach(({ key, index }) => {
if (index && key !== 'index') {
listData.push({
title: titleFunction(key),
description: detailsRule(key, data[key]),
type: typeRules(key) || 'text',
name: key,
span: spanRules(key) || 1,
});
}
});
return listData;
};