refactor: 重构下载excel及区间选择器字段逻辑处理
This commit is contained in:
@@ -1,10 +1,16 @@
|
||||
import type { VbenFormProps } from '#/adapter/form';
|
||||
|
||||
import { $t } from '@vben/locales';
|
||||
import { cloneDeep, formatDate } from '@vben/utils';
|
||||
|
||||
import { message } from 'ant-design-vue';
|
||||
|
||||
import { dataURLtoBlob, urlToBase64 } from './base64Conver';
|
||||
|
||||
/**
|
||||
*
|
||||
* @deprecated 无法处理区间选择器数据 请使用commonDownloadExcel
|
||||
*
|
||||
* 下载excel文件
|
||||
* @param [func] axios函数
|
||||
* @param [fileName] 文件名称 不需要带xlsx后缀
|
||||
@@ -30,6 +36,81 @@ export async function downloadExcel(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 源码同packages\@core\ui-kit\form-ui\src\components\form-actions.vue
|
||||
* @param values 表单值
|
||||
* @param fieldMappingTime 区间选择器 字段映射
|
||||
* @returns 格式化后的值
|
||||
*/
|
||||
function handleRangeTimeValue(
|
||||
values: Record<string, any>,
|
||||
fieldMappingTime: VbenFormProps['fieldMappingTime'],
|
||||
) {
|
||||
// 需要深拷贝 可能是readonly的
|
||||
values = cloneDeep(values);
|
||||
if (!fieldMappingTime || !Array.isArray(fieldMappingTime)) {
|
||||
return values;
|
||||
}
|
||||
|
||||
fieldMappingTime.forEach(
|
||||
([field, [startTimeKey, endTimeKey], format = 'YYYY-MM-DD']) => {
|
||||
if (!values[field]) {
|
||||
Reflect.deleteProperty(values, field);
|
||||
return;
|
||||
}
|
||||
|
||||
const [startTime, endTime] = values[field];
|
||||
const [startTimeFormat, endTimeFormat] = Array.isArray(format)
|
||||
? format
|
||||
: [format, format];
|
||||
|
||||
values[startTimeKey] = startTime
|
||||
? formatDate(startTime, startTimeFormat)
|
||||
: undefined;
|
||||
values[endTimeKey] = endTime
|
||||
? formatDate(endTime, endTimeFormat)
|
||||
: undefined;
|
||||
|
||||
Reflect.deleteProperty(values, field);
|
||||
},
|
||||
);
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
export interface DownloadExcelOptions {
|
||||
// 是否随机文件名(带时间戳)
|
||||
withRandomName?: boolean;
|
||||
// 区间选择器 字段映射
|
||||
fieldMappingTime?: VbenFormProps['fieldMappingTime'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 通用下载excel方法
|
||||
* @param api 后端下载接口
|
||||
* @param fileName 文件名 不带拓展名
|
||||
* @param requestData 请求参数
|
||||
* @param options 下载选项
|
||||
*/
|
||||
export async function commonDownloadExcel(
|
||||
api: (data?: any) => Promise<Blob>,
|
||||
fileName: string,
|
||||
requestData: any = {},
|
||||
options: DownloadExcelOptions = {},
|
||||
) {
|
||||
const hideLoading = message.loading($t('pages.common.downloadLoading'), 0);
|
||||
try {
|
||||
const { withRandomName = true, fieldMappingTime } = options;
|
||||
// 需要处理时间字段映射
|
||||
const data = await api(handleRangeTimeValue(requestData, fieldMappingTime));
|
||||
downloadExcelFile(data, fileName, withRandomName);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
} finally {
|
||||
hideLoading();
|
||||
}
|
||||
}
|
||||
|
||||
export function downloadExcelFile(
|
||||
data: BlobPart,
|
||||
filename: string,
|
||||
|
Reference in New Issue
Block a user