mirror of
https://gitee.com/dapppp/ruoyi-plus-vben5.git
synced 2026-03-15 13:22:01 +08:00
refactor: 移除已弃用的 commonDownloadExcel 方法
使用 useBlobExport 替代 commonDownloadExcel 方法,并更新相关文档。
This commit is contained in:
@@ -1,149 +1,5 @@
|
||||
import type { VbenFormProps } from '#/adapter/form';
|
||||
|
||||
import { $t } from '@vben/locales';
|
||||
import { cloneDeep, formatDate } from '@vben/utils';
|
||||
|
||||
import { isFunction } from 'lodash-es';
|
||||
|
||||
import { dataURLtoBlob, urlToBase64 } from './base64Conver';
|
||||
|
||||
/**
|
||||
*
|
||||
* @deprecated 无法处理区间选择器数据 请使用commonDownloadExcel
|
||||
*
|
||||
* 下载excel文件
|
||||
* @param [func] axios函数
|
||||
* @param [fileName] 文件名称 不需要带xlsx后缀
|
||||
* @param [requestData] 请求参数
|
||||
* @param [withRandomName] 是否带随机文件名
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
export async function downloadExcel(
|
||||
func: (data?: any) => Promise<Blob>,
|
||||
fileName: string,
|
||||
requestData: any = {},
|
||||
withRandomName = true,
|
||||
) {
|
||||
const hideLoading = window.message.loading(
|
||||
$t('pages.common.downloadLoading'),
|
||||
0,
|
||||
);
|
||||
try {
|
||||
const data = await func(requestData);
|
||||
downloadExcelFile(data, fileName, withRandomName);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
} finally {
|
||||
hideLoading();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 源码同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 (startTimeKey && endTimeKey && values[field] === null) {
|
||||
Reflect.deleteProperty(values, startTimeKey);
|
||||
Reflect.deleteProperty(values, endTimeKey);
|
||||
// delete values[startTimeKey];
|
||||
// delete values[endTimeKey];
|
||||
}
|
||||
|
||||
if (!values[field]) {
|
||||
Reflect.deleteProperty(values, field);
|
||||
// delete values[field];
|
||||
return;
|
||||
}
|
||||
|
||||
const [startTime, endTime] = values[field];
|
||||
if (format === null) {
|
||||
values[startTimeKey] = startTime;
|
||||
values[endTimeKey] = endTime;
|
||||
} else if (isFunction(format)) {
|
||||
values[startTimeKey] = format(startTime, startTimeKey);
|
||||
values[endTimeKey] = format(endTime, endTimeKey);
|
||||
} else {
|
||||
const [startTimeFormat, endTimeFormat] = Array.isArray(format)
|
||||
? format
|
||||
: [format, format];
|
||||
|
||||
values[startTimeKey] = startTime
|
||||
? formatDate(startTime, startTimeFormat)
|
||||
: undefined;
|
||||
values[endTimeKey] = endTime
|
||||
? formatDate(endTime, endTimeFormat)
|
||||
: undefined;
|
||||
}
|
||||
// delete values[field];
|
||||
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 = window.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,
|
||||
withRandomName = true,
|
||||
) {
|
||||
let realFileName = filename;
|
||||
if (withRandomName) {
|
||||
realFileName = `${filename}-${Date.now()}.xlsx`;
|
||||
}
|
||||
downloadByData(data, realFileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Download online pictures
|
||||
* @param url
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
- Popconfirm不再需要 `:get-popup-container="getVxePopupContainer"` antd已经支持滚动跟随 故`getVxePopupContainer`已经移除
|
||||
- 离线(菜单)图标方案重构 在`scripts/generate-offline-icons.js`添加图标名称 在根目录执行`pnpm generate-offline-icons`即可生成离线图标
|
||||
- 表格上方搜索表单(或者需要调用formReset的场景) -> 时间相关组件必须设置`defaultValue`为`null`(区间时间组件需要设置为[null, null]的元组) **否则不会正常重置**
|
||||
- 移除`commonDownloadExcel`方法 使用`useBlobExport`代替
|
||||
|
||||
## 已知问题
|
||||
|
||||
|
||||
Reference in New Issue
Block a user