alert.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. (() => {
  2. /**
  3. * 全局提示遮罩层管理
  4. * 用于服务器断开连接时阻止 UI 交互并显示重连状态
  5. */
  6. class AlertView {
  7. constructor(options = {}) {
  8. this.overlay = null;
  9. this.message = null;
  10. this.checkInterval = options.checkInterval || 3000; // 检测间隔
  11. this.pingUrl = options.pingUrl || '/api/disk/list?path='; // 用于检测连接的接口
  12. this.isConnected = true;
  13. this.reconnectTimer = null;
  14. this.init();
  15. }
  16. async init() {
  17. await this.ensureElements();
  18. this.overlay = document.getElementById('alertOverlay');
  19. this.message = document.getElementById('alertMessage');
  20. this.startConnectionCheck();
  21. }
  22. async ensureElements() {
  23. if (document.getElementById('alertOverlay')) {
  24. return;
  25. }
  26. try {
  27. const response = await fetch('/page/alert-overlay.html', { cache: 'no-cache' });
  28. if (!response.ok) {
  29. throw new Error('加载 Alert Overlay 组件失败');
  30. }
  31. const html = await response.text();
  32. document.body.insertAdjacentHTML('beforeend', html);
  33. } catch (error) {
  34. console.error('加载 Alert Overlay 组件失败:', error);
  35. }
  36. }
  37. show(msg) {
  38. if (this.overlay) {
  39. if (msg && this.message) {
  40. this.message.textContent = msg;
  41. }
  42. this.overlay.classList.add('show');
  43. }
  44. }
  45. hide() {
  46. if (this.overlay) {
  47. this.overlay.classList.remove('show');
  48. }
  49. }
  50. startConnectionCheck() {
  51. // 定期检测服务器连接
  52. setInterval(() => this.checkConnection(), this.checkInterval);
  53. }
  54. async checkConnection() {
  55. try {
  56. const controller = new AbortController();
  57. const timeoutId = setTimeout(() => controller.abort(), 5000);
  58. const response = await fetch(this.pingUrl, {
  59. method: 'GET',
  60. signal: controller.signal
  61. });
  62. clearTimeout(timeoutId);
  63. if (response.ok) {
  64. if (!this.isConnected) {
  65. // 重连成功
  66. this.isConnected = true;
  67. this.hide();
  68. this.onReconnected();
  69. }
  70. } else {
  71. this.handleDisconnect();
  72. }
  73. } catch (error) {
  74. this.handleDisconnect();
  75. }
  76. }
  77. handleDisconnect() {
  78. if (this.isConnected) {
  79. this.isConnected = false;
  80. this.show('服务器断开连接,正在重连...');
  81. this.startReconnect();
  82. }
  83. }
  84. startReconnect() {
  85. if (this.reconnectTimer) {
  86. clearInterval(this.reconnectTimer);
  87. }
  88. // 每 2 秒尝试重连一次
  89. this.reconnectTimer = setInterval(() => {
  90. this.tryReconnect();
  91. }, 2000);
  92. }
  93. async tryReconnect() {
  94. try {
  95. const controller = new AbortController();
  96. const timeoutId = setTimeout(() => controller.abort(), 5000);
  97. const response = await fetch(this.pingUrl, {
  98. method: 'GET',
  99. signal: controller.signal
  100. });
  101. clearTimeout(timeoutId);
  102. if (response.ok) {
  103. // 重连成功
  104. clearInterval(this.reconnectTimer);
  105. this.reconnectTimer = null;
  106. this.isConnected = true;
  107. this.hide();
  108. this.onReconnected();
  109. }
  110. } catch (error) {
  111. // 重连失败,继续尝试
  112. }
  113. }
  114. onReconnected() {
  115. // 重连成功后刷新页面数据
  116. if (window.diskManager && typeof window.diskManager.loadFiles === 'function') {
  117. window.diskManager.loadFiles();
  118. }
  119. }
  120. }
  121. // 导出到全局
  122. window.AlertView = AlertView;
  123. // 页面加载完成后自动初始化(已禁用)
  124. // document.addEventListener('DOMContentLoaded', () => {
  125. // window.alertView = new AlertView();
  126. // });
  127. })();