| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- // utils.js - 通用工具函数
- // 保存数据到localStorage的LabStatistics对象下
- function setLocalStorage(name, value) {
- try {
- // 获取现有的LabStatistics对象
- let labStats = localStorage.getItem('LabStatistics');
- let labStatsObj = labStats ? JSON.parse(labStats) : {};
- // 设置新的值
- labStatsObj[name] = value;
- // 保存回localStorage
- localStorage.setItem('LabStatistics', JSON.stringify(labStatsObj));
- } catch (e) {
- console.error('保存数据失败:', e);
- }
- }
- // 从localStorage的LabStatistics对象下获取数据
- function getLocalStorage(name) {
- try {
- let labStats = localStorage.getItem('LabStatistics');
- if (!labStats) return null;
- let labStatsObj = JSON.parse(labStats);
- return labStatsObj[name] !== undefined ? labStatsObj[name] : null;
- } catch (e) {
- console.error('获取数据失败:', e);
- return null;
- }
- }
- // 从localStorage的LabStatistics对象下删除数据
- function removeLocalStorage(name) {
- try {
- let labStats = localStorage.getItem('LabStatistics');
- if (!labStats) return;
- let labStatsObj = JSON.parse(labStats);
- delete labStatsObj[name];
- // 保存更新后的对象
- localStorage.setItem('LabStatistics', JSON.stringify(labStatsObj));
- } catch (e) {
- console.error('删除数据失败:', e);
- }
- }
- // 可选:清空LabStatistics对象
- function clearLocalStorage() {
- try {
- localStorage.removeItem('LabStatistics');
- } catch (e) {
- console.error('清空数据失败:', e);
- }
- }
- // 可选:获取整个LabStatistics对象
- function getAllLocalStorageData() {
- try {
- const labStats = localStorage.getItem('LabStatistics');
- if (labStats) {
- const labStatsObj = JSON.parse(labStats);
- // 创建新对象,排除translation字段
- const result = {};
- for (const [key, value] of Object.entries(labStatsObj)) {
- if (key !== 'translation') {
- result[key] = value;
- }
- }
- return result;
- }
- return {};
- } catch (e) {
- console.error('获取LabStatistics数据失败:', e);
- return {};
- }
- }
- // 更新当前时间
- function updateCurrentTime() {
- const now = new Date();
- const hours = now.getHours().toString().padStart(2, '0');
- const minutes = now.getMinutes().toString().padStart(2, '0');
- const seconds = now.getSeconds().toString().padStart(2, '0');
- const timeString = `${hours}:${minutes}:${seconds}`;
- document.getElementById('current-time').textContent = timeString;
- }
- function parseUnixTime(unixTimestamp) {
- // 将Unix时间戳转换为毫秒
- const timestamp = parseInt(unixTimestamp);
- const date = new Date(timestamp);
- // 格式化日期和时间
- const year = date.getFullYear();
- const month = (date.getMonth() + 1).toString().padStart(2, '0');
- const day = date.getDate().toString().padStart(2, '0');
- const hours = date.getHours().toString().padStart(2, '0');
- const minutes = date.getMinutes().toString().padStart(2, '0');
- const seconds = date.getSeconds().toString().padStart(2, '0');
- // 返回格式化后的时间字符串
- return `${hours}:${minutes}:${seconds}`;
- // return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
- }
- // 显示提示信息
- // 跟踪当前显示的toast数量
- let activeToasts = 0;
- const MAX_TOASTS = 3;
- function showToast(message, type = 'info', duration = 3000) {
- // 如果已经达到最大数量,移除最早的toast
- const existingToasts = document.querySelectorAll('.toast');
- if (existingToasts.length >= MAX_TOASTS) {
- // 不移除最早的,而是等待它自动消失
- // 只是不再增加新的位置
- }
- // 创建toast元素
- const toast = document.createElement('div');
- toast.className = `toast toast-${type}`;
- toast.textContent = message;
- // 计算当前toast应该的位置
- const position = Math.min(existingToasts.length, MAX_TOASTS - 1);
- const topPosition = 20 + position * 60; // 每个toast高度约50px,加上间距10px
- // 设置样式
- toast.style.cssText = `
- position: fixed;
- top: ${topPosition}px;
- left: 50%;
- transform: translateX(-50%);
- padding: 10px 20px;
- border-radius: 4px;
- color: #fff;
- font-size: 14px;
- z-index: 9999;
- opacity: 0;
- transition: opacity 0.3s ease-in-out;
- `;
- // 根据类型设置背景色
- const colors = {
- success: '#67C23A',
- error: '#F56C6C',
- warning: '#E6A23C',
- info: '#909399'
- };
- toast.style.backgroundColor = colors[type] || colors.info;
- // 添加到文档
- document.body.appendChild(toast);
- activeToasts++;
- // 显示动画
- setTimeout(() => {
- toast.style.opacity = '1';
- }, 10);
- // 自动移除
- setTimeout(() => {
- toast.style.opacity = '0';
- setTimeout(() => {
- document.body.removeChild(toast);
- activeToasts--;
- // 当一个toast被移除时,重新调整其他toast的位置
- repositionToasts();
- }, 300);
- }, duration);
- }
- // 重新调整所有toast的位置
- function repositionToasts() {
- const toasts = document.querySelectorAll('.toast');
- toasts.forEach((toast, index) => {
- if (index < MAX_TOASTS) {
- const topPosition = 20 + index * 60;
- toast.style.top = `${topPosition}px`;
- }
- });
- }
|