|
- /*
- * @Author: 赵伟
- * @Date: 2024-04-19 14:42:51
- * @Description: UI 公共方法
- */
- import { PageEnum } from '@/enums/pagesEnums';
- import themes from '@/styles/theme.less';
- import { history } from '@umijs/max';
- import { Modal, message, type ModalFuncProps, type UploadFile } from 'antd';
-
- // 自定义 Confirm 弹框
- export function modalConfirm({ title, content, onOk, ...rest }: ModalFuncProps) {
- Modal.confirm({
- ...rest,
- width: 600,
- centered: true,
- title: (
- <div>
- <img
- src="/assets/images/delete-icon.png"
- style={{ width: '120px', marginBottom: '24px' }}
- alt=""
- />
- <div style={{ color: themes.textColor, fontSize: '16px', fontWeight: 500 }}>{title}</div>
- </div>
- ),
- content: content && <div style={{ color: themes.textColor, fontSize: '15px' }}>{content}</div>,
- okText: '确认',
- 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;
- });
- };
-
- // 去登录页面
- export const gotoLoginPage = (toHome: boolean = true) => {
- const { pathname, search } = window.location;
- const urlParams = new URLSearchParams();
- urlParams.append('redirect', pathname + search);
- const newSearch =
- toHome && pathname !== PageEnum.LOGIN && pathname !== '/' ? '' : urlParams.toString();
- console.log('pathname', pathname);
- console.log('search', search);
- if (window.location.pathname !== PageEnum.LOGIN) {
- history.replace({
- pathname: PageEnum.LOGIN,
- search: newSearch,
- });
- }
- };
-
- // 上传文件校验
- 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;
- };
|