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.

loading.tsx 2.7 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * @Author: 赵伟
  3. * @Date: 2024-06-26 16:37:39
  4. * @Description: 全局网络请求 Loading
  5. */
  6. import KFSpin from '@/components/KFSpin';
  7. import { ConfigProvider, SpinProps } from 'antd';
  8. import { globalConfig } from 'antd/es/config-provider';
  9. import zhCN from 'antd/locale/zh_CN';
  10. import { createRoot } from 'react-dom/client';
  11. export class Loading {
  12. static total: number = 0;
  13. static isShowing: boolean = false;
  14. static startTime: Date = new Date();
  15. static removeTimeout: ReturnType<typeof setTimeout> | undefined;
  16. static show(props?: SpinProps) {
  17. this.total += 1;
  18. if (this.total > 1) {
  19. return;
  20. }
  21. // 是否有延时未关闭的 loading
  22. if (this.isShowing) {
  23. this.clearRemoveTimeout();
  24. return;
  25. }
  26. const container = document.createElement('div');
  27. container.id = 'loading';
  28. const rootContainer = document.body; //document.getElementsByTagName('main')[0];
  29. rootContainer?.appendChild(container);
  30. const root = createRoot(container);
  31. const global = globalConfig();
  32. let renderTimeoutId: ReturnType<typeof setTimeout>;
  33. const render = (spinProps: SpinProps) => {
  34. clearTimeout(renderTimeoutId);
  35. renderTimeoutId = setTimeout(() => {
  36. const rootPrefixCls = global.getPrefixCls();
  37. const iconPrefixCls = global.getIconPrefixCls();
  38. const theme = global.getTheme();
  39. const dom = <KFSpin {...spinProps} />;
  40. root.render(
  41. <ConfigProvider
  42. prefixCls={rootPrefixCls}
  43. iconPrefixCls={iconPrefixCls}
  44. theme={theme}
  45. locale={zhCN}
  46. >
  47. {global.holderRender ? global.holderRender(dom) : dom}
  48. </ConfigProvider>,
  49. );
  50. });
  51. };
  52. render({ size: 'large', ...props, spinning: true });
  53. this.startTime = new Date();
  54. this.isShowing = true;
  55. }
  56. static clearRemoveTimeout() {
  57. if (this.removeTimeout) {
  58. clearTimeout(this.removeTimeout);
  59. this.removeTimeout = undefined;
  60. }
  61. }
  62. static removeLoading() {
  63. this.clearRemoveTimeout();
  64. const rootContainer = document.body; //document.getElementsByTagName('main')[0];
  65. const container = document.getElementById('loading');
  66. if (container) {
  67. rootContainer?.removeChild(container);
  68. }
  69. this.isShowing = false;
  70. }
  71. static hide(force: boolean = false) {
  72. this.total -= 1;
  73. if (this.total > 0 && !force) {
  74. return;
  75. }
  76. this.total = 0;
  77. const duration = new Date().getTime() - this.startTime.getTime();
  78. this.removeTimeout = setTimeout(() => {
  79. this.removeLoading();
  80. }, Math.max(300 - duration, 0));
  81. }
  82. }
  83. export default Loading;