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 4.2 kB

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