utils.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // utils.js - 通用工具函数
  2. // 保存数据到localStorage的LabStatistics对象下
  3. function setLocalStorage(name, value) {
  4. try {
  5. // 获取现有的LabStatistics对象
  6. let labStats = localStorage.getItem('LabStatistics');
  7. let labStatsObj = labStats ? JSON.parse(labStats) : {};
  8. // 设置新的值
  9. labStatsObj[name] = value;
  10. // 保存回localStorage
  11. localStorage.setItem('LabStatistics', JSON.stringify(labStatsObj));
  12. } catch (e) {
  13. console.error('保存数据失败:', e);
  14. }
  15. }
  16. // 从localStorage的LabStatistics对象下获取数据
  17. function getLocalStorage(name) {
  18. try {
  19. let labStats = localStorage.getItem('LabStatistics');
  20. if (!labStats) return null;
  21. let labStatsObj = JSON.parse(labStats);
  22. return labStatsObj[name] !== undefined ? labStatsObj[name] : null;
  23. } catch (e) {
  24. console.error('获取数据失败:', e);
  25. return null;
  26. }
  27. }
  28. // 从localStorage的LabStatistics对象下删除数据
  29. function removeLocalStorage(name) {
  30. try {
  31. let labStats = localStorage.getItem('LabStatistics');
  32. if (!labStats) return;
  33. let labStatsObj = JSON.parse(labStats);
  34. delete labStatsObj[name];
  35. // 保存更新后的对象
  36. localStorage.setItem('LabStatistics', JSON.stringify(labStatsObj));
  37. } catch (e) {
  38. console.error('删除数据失败:', e);
  39. }
  40. }
  41. // 可选:清空LabStatistics对象
  42. function clearLocalStorage() {
  43. try {
  44. localStorage.removeItem('LabStatistics');
  45. } catch (e) {
  46. console.error('清空数据失败:', e);
  47. }
  48. }
  49. // 可选:获取整个LabStatistics对象
  50. function getAllLocalStorageData() {
  51. try {
  52. const labStats = localStorage.getItem('LabStatistics');
  53. if (labStats) {
  54. const labStatsObj = JSON.parse(labStats);
  55. // 创建新对象,排除translation字段
  56. const result = {};
  57. for (const [key, value] of Object.entries(labStatsObj)) {
  58. if (key !== 'translation') {
  59. result[key] = value;
  60. }
  61. }
  62. return result;
  63. }
  64. return {};
  65. } catch (e) {
  66. console.error('获取LabStatistics数据失败:', e);
  67. return {};
  68. }
  69. }
  70. // 更新当前时间
  71. function updateCurrentTime() {
  72. const now = new Date();
  73. const hours = now.getHours().toString().padStart(2, '0');
  74. const minutes = now.getMinutes().toString().padStart(2, '0');
  75. const seconds = now.getSeconds().toString().padStart(2, '0');
  76. const timeString = `${hours}:${minutes}:${seconds}`;
  77. document.getElementById('current-time').textContent = timeString;
  78. }
  79. function parseUnixTime(unixTimestamp) {
  80. // 将Unix时间戳转换为毫秒
  81. const timestamp = parseInt(unixTimestamp);
  82. const date = new Date(timestamp);
  83. // 格式化日期和时间
  84. const year = date.getFullYear();
  85. const month = (date.getMonth() + 1).toString().padStart(2, '0');
  86. const day = date.getDate().toString().padStart(2, '0');
  87. const hours = date.getHours().toString().padStart(2, '0');
  88. const minutes = date.getMinutes().toString().padStart(2, '0');
  89. const seconds = date.getSeconds().toString().padStart(2, '0');
  90. // 返回格式化后的时间字符串
  91. return `${hours}:${minutes}:${seconds}`;
  92. // return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  93. }
  94. // 显示提示信息
  95. // 跟踪当前显示的toast数量
  96. let activeToasts = 0;
  97. const MAX_TOASTS = 3;
  98. function showToast(message, type = 'info', duration = 3000) {
  99. // 如果已经达到最大数量,移除最早的toast
  100. const existingToasts = document.querySelectorAll('.toast');
  101. if (existingToasts.length >= MAX_TOASTS) {
  102. // 不移除最早的,而是等待它自动消失
  103. // 只是不再增加新的位置
  104. }
  105. // 创建toast元素
  106. const toast = document.createElement('div');
  107. toast.className = `toast toast-${type}`;
  108. toast.textContent = message;
  109. // 计算当前toast应该的位置
  110. const position = Math.min(existingToasts.length, MAX_TOASTS - 1);
  111. const topPosition = 20 + position * 60; // 每个toast高度约50px,加上间距10px
  112. // 设置样式
  113. toast.style.cssText = `
  114. position: fixed;
  115. top: ${topPosition}px;
  116. left: 50%;
  117. transform: translateX(-50%);
  118. padding: 10px 20px;
  119. border-radius: 4px;
  120. color: #fff;
  121. font-size: 14px;
  122. z-index: 9999;
  123. opacity: 0;
  124. transition: opacity 0.3s ease-in-out;
  125. `;
  126. // 根据类型设置背景色
  127. const colors = {
  128. success: '#67C23A',
  129. error: '#F56C6C',
  130. warning: '#E6A23C',
  131. info: '#909399'
  132. };
  133. toast.style.backgroundColor = colors[type] || colors.info;
  134. // 添加到文档
  135. document.body.appendChild(toast);
  136. activeToasts++;
  137. // 显示动画
  138. setTimeout(() => {
  139. toast.style.opacity = '1';
  140. }, 10);
  141. // 自动移除
  142. setTimeout(() => {
  143. toast.style.opacity = '0';
  144. setTimeout(() => {
  145. document.body.removeChild(toast);
  146. activeToasts--;
  147. // 当一个toast被移除时,重新调整其他toast的位置
  148. repositionToasts();
  149. }, 300);
  150. }, duration);
  151. }
  152. // 重新调整所有toast的位置
  153. function repositionToasts() {
  154. const toasts = document.querySelectorAll('.toast');
  155. toasts.forEach((toast, index) => {
  156. if (index < MAX_TOASTS) {
  157. const topPosition = 20 + index * 60;
  158. toast.style.top = `${topPosition}px`;
  159. }
  160. });
  161. }