You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ui.tsx 3.4 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * @Author: 赵伟
  3. * @Date: 2024-04-19 14:42:51
  4. * @Description: UI 公共方法
  5. */
  6. import { PageEnum } from '@/enums/pagesEnums';
  7. import { removeAllPageCacheState } from '@/hooks/pageCacheState';
  8. import themes from '@/styles/theme.less';
  9. import { history } from '@umijs/max';
  10. import { Modal, message, type ModalFuncProps, type UploadFile } from 'antd';
  11. import { closeAllModals } from './modal';
  12. // 自定义 Confirm 弹框
  13. export function modalConfirm({ title, content, onOk, ...rest }: ModalFuncProps) {
  14. Modal.confirm({
  15. ...rest,
  16. width: 600,
  17. centered: true,
  18. title: (
  19. <div>
  20. <img
  21. src={require('@/assets/img/delete-icon.png')}
  22. style={{ width: '120px', marginBottom: '24px' }}
  23. alt=""
  24. />
  25. <div style={{ color: themes.textColor, fontSize: '16px', fontWeight: 500 }}>{title}</div>
  26. </div>
  27. ),
  28. content: content && <div style={{ color: themes.textColor, fontSize: '15px' }}>{content}</div>,
  29. okText: '确认',
  30. cancelText: '取消',
  31. onOk: onOk,
  32. });
  33. }
  34. // 从事件中获取上传文件列表,用于 Upload + Form 中
  35. export const getFileListFromEvent = (e: any) => {
  36. const fileList: UploadFile[] = (Array.isArray(e) ? e : e?.fileList) || [];
  37. return fileList.map((item) => {
  38. if (item.status === 'done') {
  39. const { response } = item;
  40. if (response?.code !== 200) {
  41. return {
  42. ...item,
  43. status: 'error',
  44. };
  45. }
  46. }
  47. return item;
  48. });
  49. };
  50. /**
  51. * 跳转到登录页
  52. * @param toHome 是否跳转到首页
  53. */
  54. export const gotoLoginPage = (toHome: boolean = true) => {
  55. const { pathname, search } = location;
  56. const urlParams = new URLSearchParams();
  57. urlParams.append('redirect', pathname + search);
  58. const newSearch = toHome && pathname !== '/' ? '' : urlParams.toString();
  59. // console.log('pathname', pathname);
  60. // console.log('search', search);
  61. if (pathname !== PageEnum.LOGIN) {
  62. closeAllModals();
  63. removeAllPageCacheState();
  64. history.replace({
  65. pathname: PageEnum.LOGIN,
  66. search: newSearch,
  67. });
  68. }
  69. };
  70. /**
  71. * 验证文件上传
  72. *
  73. * @param {UploadFile[]} files - The array of uploaded files.
  74. * @param {boolean} [required=true] - Flag indicating if files are required.
  75. * @return {boolean} Returns true if all files are valid, false otherwise.
  76. */
  77. export const validateUploadFiles = (files: UploadFile[], required: boolean = true): boolean => {
  78. if (required && files.length === 0) {
  79. message.error('请上传文件');
  80. return false;
  81. }
  82. const hasError = files.some((file) => {
  83. if (file.status === 'uploading') {
  84. message.error('请等待文件上传完成');
  85. return true;
  86. }
  87. if (file.status === 'error') {
  88. message.error('存在上传失败的文件,请删除后重新上传');
  89. return true;
  90. }
  91. if (!file.response || file.response.code !== 200 || !file.response.data) {
  92. message.error('存在上传失败的文件,请删除后重新上传');
  93. return true;
  94. }
  95. return false;
  96. });
  97. return !hasError;
  98. };
  99. /**
  100. * 滚动到底部
  101. *
  102. * @param {boolean} smooth - Determines if the scroll should be smooth
  103. */
  104. export const scrollToBottom = (element: HTMLElement | null, smooth: boolean = true) => {
  105. if (element) {
  106. const optons: ScrollToOptions = {
  107. top: element.scrollHeight,
  108. behavior: smooth ? 'smooth' : 'instant',
  109. };
  110. element.scrollTo(optons);
  111. }
  112. };