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.6 kB

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