utils.js 3.9 KB

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