|
|
@@ -1,33 +1,83 @@
|
|
|
// utils.js - 通用工具函数
|
|
|
|
|
|
-// 保存数据到localStorage
|
|
|
+// 保存数据到localStorage的LabStatistics对象下
|
|
|
function setLocalStorage(name, value) {
|
|
|
try {
|
|
|
- localStorage.setItem(name, value);
|
|
|
+ // 获取现有的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获取数据
|
|
|
+// 从localStorage的LabStatistics对象下获取数据
|
|
|
function getLocalStorage(name) {
|
|
|
try {
|
|
|
- return localStorage.getItem(name);
|
|
|
+ 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删除数据
|
|
|
+// 从localStorage的LabStatistics对象下删除数据
|
|
|
function removeLocalStorage(name) {
|
|
|
try {
|
|
|
- localStorage.removeItem(name);
|
|
|
+ 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();
|
|
|
@@ -74,7 +124,7 @@ function showToast(message, type = 'info', duration = 3000) {
|
|
|
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
|
|
|
@@ -118,7 +168,7 @@ function showToast(message, type = 'info', duration = 3000) {
|
|
|
setTimeout(() => {
|
|
|
document.body.removeChild(toast);
|
|
|
activeToasts--;
|
|
|
-
|
|
|
+
|
|
|
// 当一个toast被移除时,重新调整其他toast的位置
|
|
|
repositionToasts();
|
|
|
}, 300);
|