/* * @Author: 赵伟 * @Date: 2024-04-19 14:42:51 * @Description: UI 公共方法 */ import { PageEnum } from '@/enums/pagesEnums'; import { removeAllPageCacheState } from '@/hooks/pageCacheState'; import themes from '@/styles/theme.less'; import { history } from '@umijs/max'; import { Modal, message, type ModalFuncProps, type UploadFile } from 'antd'; import { closeAllModals } from './modal'; type ModalConfirmProps = ModalFuncProps & { isDelete?: boolean; }; // 自定义删除 Confirm 弹框 export function modalConfirm({ title, content, okText = '确认', cancelText = '取消', isDelete = true, onOk, ...rest }: ModalConfirmProps) { Modal.confirm({ ...rest, width: 600, centered: true, title: (
{title}
), content: content &&
{content}
, okText: okText, cancelText: cancelText, onOk: onOk, }); } // 从事件中获取上传文件列表,用于 Upload + Form 中 export const getFileListFromEvent = (e: any) => { const fileList: UploadFile[] = (Array.isArray(e) ? e : e?.fileList) || []; return fileList.map((item) => { if (item.status === 'done') { const { response } = item; if (response?.code !== 200) { return { ...item, status: 'error', }; } } return item; }); }; /** * 跳转到登录页 * @param toHome 是否跳转到首页 */ export const gotoLoginPage = (toHome: boolean = true) => { const { pathname, search } = location; const urlParams = new URLSearchParams(); urlParams.append('redirect', pathname + search); const newSearch = toHome || pathname === '/' ? '' : urlParams.toString(); if (pathname !== PageEnum.LOGIN) { closeAllModals(); removeAllPageCacheState(); history.replace({ pathname: PageEnum.LOGIN, search: newSearch, }); } }; /** * 验证文件上传 * * @param {UploadFile[]} files - The array of uploaded files. * @param {boolean} [required=true] - Flag indicating if files are required. * @return {boolean} Returns true if all files are valid, false otherwise. */ export const validateUploadFiles = (files: UploadFile[], required: boolean = true): boolean => { if (required && files.length === 0) { message.error('请上传文件'); return false; } const hasError = files.some((file) => { if (file.status === 'uploading') { message.error('请等待文件上传完成'); return true; } if (file.status === 'error') { message.error('存在上传失败的文件,请删除后重新上传'); return true; } if (!file.response || file.response.code !== 200 || !file.response.data) { message.error('存在上传失败的文件,请删除后重新上传'); return true; } return false; }); return !hasError; }; /** * 滚动到底部 * * @param {boolean} smooth - Determines if the scroll should be smooth */ export const scrollToBottom = (element: HTMLElement | null, smooth: boolean = true) => { if (element) { const optons: ScrollToOptions = { top: element.scrollHeight, behavior: smooth ? 'smooth' : 'instant', }; element.scrollTo(optons); } };