diff --git a/CHANGELOG.md b/CHANGELOG.md index e44e1b91..4512dcac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,11 @@ **REFACTOR** - 使用VxeTable重构OAuth账号绑定列表(替代antdv的Table) +- commonDownloadExcel方法 支持处理区间选择器字段导出excel + +**OTHERS** + +- 废弃downloadExcel方法 统一使用commonDownloadExcel方法 # 1.1.0 diff --git a/apps/web-antd/src/utils/file/download.ts b/apps/web-antd/src/utils/file/download.ts index 8bea0c1e..9bcdcc3c 100644 --- a/apps/web-antd/src/utils/file/download.ts +++ b/apps/web-antd/src/utils/file/download.ts @@ -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, + 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, + 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, diff --git a/apps/web-antd/src/views/monitor/logininfor/index.vue b/apps/web-antd/src/views/monitor/logininfor/index.vue index 78201b10..ddcd72fe 100644 --- a/apps/web-antd/src/views/monitor/logininfor/index.vue +++ b/apps/web-antd/src/views/monitor/logininfor/index.vue @@ -7,7 +7,6 @@ import { Page, useVbenModal, type VbenFormProps } from '@vben/common-ui'; import { getVxePopupContainer } from '@vben/utils'; import { Modal, Popconfirm, Space } from 'ant-design-vue'; -import dayjs from 'dayjs'; import { tableCheckboxEvent, @@ -22,7 +21,7 @@ import { loginInfoRemove, userUnlock, } from '#/api/monitor/logininfo'; -import { downloadExcel } from '#/utils/file/download'; +import { commonDownloadExcel } from '#/utils/file/download'; import { confirmDeleteModal } from '#/utils/modal'; import { columns, querySchema } from './data'; @@ -37,6 +36,14 @@ const formOptions: VbenFormProps = { }, schema: querySchema(), wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4', + // 日期选择格式化 + fieldMappingTime: [ + [ + 'dateTime', + ['params[beginTime]', 'params[endTime]'], + ['YYYY-MM-DD 00:00:00', 'YYYY-MM-DD 23:59:59'], + ], + ], }; const gridOptions: VxeGridProps = { @@ -55,20 +62,6 @@ const gridOptions: VxeGridProps = { proxyConfig: { ajax: { query: async ({ page }, formValues = {}) => { - // 区间选择器处理 - if (formValues?.dateTime) { - formValues.params = { - beginTime: dayjs(formValues.dateTime[0]).format( - 'YYYY-MM-DD 00:00:00', - ), - endTime: dayjs(formValues.dateTime[1]).format( - 'YYYY-MM-DD 23:59:59', - ), - }; - Reflect.deleteProperty(formValues, 'dateTime'); - } else { - Reflect.deleteProperty(formValues, 'params'); - } return await loginInfoList({ pageNum: page.currentPage, pageSize: page.pageSize, @@ -149,6 +142,17 @@ async function handleUnlock() { canUnlock.value = false; tableApi.grid.clearCheckboxRow(); } + +function handleDownloadExcel() { + commonDownloadExcel( + loginInfoExport, + '登录日志', + tableApi.formApi.form.values, + { + fieldMappingTime: formOptions.fieldMappingTime, + }, + ); +}