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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 container = document.getElementById('loading');
  65. if (container) {
  66. container.parentNode?.removeChild(container);
  67. }
  68. this.isShowing = false;
  69. }
  70. static hide(force: boolean = false) {
  71. this.total -= 1;
  72. if (this.total > 0 && !force) {
  73. return;
  74. }
  75. this.total = 0;
  76. const duration = new Date().getTime() - this.startTime.getTime();
  77. this.removeTimeout = setTimeout(() => {
  78. this.removeLoading();
  79. }, Math.max(300 - duration, 0));
  80. }
  81. }
  82. export default Loading;