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

1 year ago
1 year ago
2 years ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. if (pathname !== PageEnum.LOGIN) {
  78. closeAllModals();
  79. removeAllPageCacheState();
  80. history.replace({
  81. pathname: PageEnum.LOGIN,
  82. search: newSearch,
  83. });
  84. }
  85. };
  86. export const gotoOAuth2 = () => {
  87. const clientInfo = SessionStorage.getItem(SessionStorage.clientInfoKey, true) as ClientInfo;
  88. if (clientInfo) {
  89. const { clientId, userAuthorizationUri } = clientInfo;
  90. const url = `${userAuthorizationUri}?client_id=${clientId}&response_type=code&grant_type=authorization_code`;
  91. location.replace(url);
  92. }
  93. };
  94. /**
  95. * 验证文件上传
  96. *
  97. * @param {UploadFile[]} files - The array of uploaded files.
  98. * @param {boolean} [required=true] - Flag indicating if files are required.
  99. * @return {boolean} Returns true if all files are valid, false otherwise.
  100. */
  101. export const validateUploadFiles = (files: UploadFile[], required: boolean = true): boolean => {
  102. if (required && files.length === 0) {
  103. message.error('请上传文件');
  104. return false;
  105. }
  106. const hasError = files.some((file) => {
  107. if (file.status === 'uploading') {
  108. message.error('请等待文件上传完成');
  109. return true;
  110. }
  111. if (file.status === 'error') {
  112. message.error('存在上传失败的文件,请删除后重新上传');
  113. return true;
  114. }
  115. if (!file.response || file.response.code !== 200 || !file.response.data) {
  116. message.error('存在上传失败的文件,请删除后重新上传');
  117. return true;
  118. }
  119. return false;
  120. });
  121. return !hasError;
  122. };
  123. /**
  124. * 滚动到底部
  125. *
  126. * @param {boolean} smooth - Determines if the scroll should be smooth
  127. */
  128. export const scrollToBottom = (element: HTMLElement | null, smooth: boolean = true) => {
  129. if (element) {
  130. const optons: ScrollToOptions = {
  131. top: element.scrollHeight,
  132. behavior: smooth ? 'smooth' : 'instant',
  133. };
  134. element.scrollTo(optons);
  135. }
  136. };
  137. // 退出火石平台
  138. export const logoutHuoShi = () => {
  139. const iframe = document.createElement('iframe');
  140. iframe.style.display = 'none';
  141. iframe.src = 'http://10.43.107.27:24078/uap/nudt/sso/logout';
  142. document.body.appendChild(iframe);
  143. setTimeout(() => {
  144. document.body.removeChild(iframe);
  145. }, 2000);
  146. };