Browse Source

update LabStatistics localstroage logic

JY 4 months ago
parent
commit
7435d72ba4
3 changed files with 97 additions and 55 deletions
  1. 5 16
      LabStatistics/pageData.js
  2. 58 8
      LabStatistics/utils.js
  3. 34 31
      index.html

+ 5 - 16
LabStatistics/pageData.js

@@ -299,7 +299,7 @@ function loadProjectTitle() {
 function newProject() {
     if (confirm('确定要新建工程吗?这将清除所有当前数据!')) {
         // 清除整个localStorage
-        localStorage.clear();
+        clearLocalStorage();
         // 刷新页面
         location.reload();
     }
@@ -309,18 +309,7 @@ function newProject() {
 // 导出数据
 function exportData() {
     // 获取所有localStorage数据
-    const data = {};
-    for (let i = 0; i < localStorage.length; i++) {
-        const key = localStorage.key(i);
-        try {
-            // 尝试解析JSON数据
-            const value = localStorage.getItem(key);
-            data[key] = JSON.parse(value);
-        } catch (e) {
-            // 如果不是JSON格式则直接存储原始值
-            data[key] = localStorage.getItem(key);
-        }
-    }
+    const data = getAllLocalStorageData();
 
     const projectTitle = document.getElementById('project-title').value;
     const jsonString = JSON.stringify(data, null, 2);
@@ -353,7 +342,7 @@ function importData() {
             const data = JSON.parse(e.target.result);
 
             // 清除现有数据
-            localStorage.clear();
+            clearLocalStorage();
 
             // 导入所有数据到localStorage
             // 遍历数据对象的所有键
@@ -362,10 +351,10 @@ function importData() {
                     // 检查数据类型并相应处理
                     if (typeof data[key] === 'object' && data[key] !== null) {
                         // 对象类型数据转换为JSON字符串存储
-                        localStorage.setItem(key, JSON.stringify(data[key]));
+                        setLocalStorage(key, JSON.stringify(data[key]));
                     } else {
                         // 其他类型直接存储
-                        localStorage.setItem(key, data[key]);
+                        setLocalStorage(key, data[key]);
                     }
                 } catch (error) {
                     console.error(`导入键 ${key} 时出错:`, error);

+ 58 - 8
LabStatistics/utils.js

@@ -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);

+ 34 - 31
index.html

@@ -1,5 +1,6 @@
 <!DOCTYPE html>
 <html lang="zh-CN">
+
 <head>
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
@@ -12,42 +13,51 @@
             background: #f0f2f5;
             color: #1f2937;
         }
+
         .container {
             max-width: 1200px;
             margin: 0 auto;
         }
+
         h1 {
             text-align: center;
             color: #2563eb;
             font-size: 2.5rem;
             margin-bottom: 3rem;
         }
+
         .grid {
             display: grid;
             grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
             gap: 2rem;
         }
+
         .card {
             background: white;
             border-radius: 1rem;
             padding: 1.5rem;
             box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
             transition: transform 0.2s, box-shadow 0.2s;
+            cursor: pointer;
         }
+
         .card:hover {
             transform: translateY(-4px);
             box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
         }
+
         .card h2 {
             color: #1f2937;
             margin: 0 0 1rem 0;
             font-size: 1.5rem;
         }
+
         .card p {
             color: #6b7280;
             margin: 0 0 1.5rem 0;
             line-height: 1.5;
         }
+
         .card a {
             display: inline-block;
             text-decoration: none;
@@ -56,75 +66,68 @@
             padding: 0.5rem 0;
             transition: color 0.2s;
         }
+
         .card a:hover {
             color: #1d4ed8;
         }
+
         @media (max-width: 768px) {
             body {
                 padding: 1rem;
             }
+
             .grid {
                 grid-template-columns: 1fr;
             }
         }
+
     </style>
 </head>
+
 <body>
     <div class="container">
         <h1>工具导航</h1>
         <div class="grid">
-            <div class="card">
+            <div class="card" onclick="window.location.href='./QrGen/index.html';return false">
                 <h2>DO_FREEZE APP密码加密生成</h2>
                 <p>牛奶冰点仪、渗透压摩尔浓度检测仪 APP登录明文加密。</p>
-                <a href="./QrGen/index.html">进入工具 →</a>
             </div>
-            <div class="card">
+            <div class="card" onclick="window.location.href='./DIPM_resultStatistics/index.html';return false">
                 <h2>DIPM结果统计</h2>
                 <p>牛奶冰点仪结果评估。</p>
-                <a href="./DIPM_resultStatistics/index.html">进入工具 →</a>
             </div>
-            <div class="card">
-                <h2>OSMO结果统计</h2>
-                <p>渗透压摩尔浓度检测仪结果评估。(暂时与DIPM共用)</p>
-                <a href="./DIPM_resultStatistics/index.html">进入工具 →</a>
-            </div>
-            <div class="card">
+            <div class="card" onclick="window.location.href='./LabStatistics/index.html';return false">
                 <h2>实验结果统计</h2>
                 <p>基本的重复性、稳定性、示值误差计算分析。</p>
-                <a href="./LabStatistics/index.html">进入工具 →</a>
             </div>
-            <div class="card">
+            <div class="card" onclick="window.location.href='./GeoGebra_Self/index.html';return false">
                 <h2>GeoGebra</h2>
                 <p>用于生成和可视化各种数学函数的工具。</p>
-                <a href="./GeoGebra_Self/index.html">进入工具 →</a>
-            </div>
-            <div class="card">
-                <h2>函数统计</h2>
-                <p>用于生成和可视化各种数学函数的工具。</p>
-                <a href="./FuncGen/index.html">进入工具 →</a>
             </div>
-            <!-- <div class="card">
-                <h2>Desmos Desktop</h2>
-                <p>用于生成和可视化各种数学函数的工具。</p>
-                <a href="./Desmos-Desktop-master/index.html">进入工具 →</a>
-            </div> -->
-            <div class="card">
+            <div class="card" onclick="window.location.href='./RT_Table/RT_Table_NTC.html';return false">
                 <h2>RT表格工具</h2>
                 <p>包含NTC、PT100、PT1000和TJ的RT表格查询工具。</p>
-                <a href="./RT_Table/RT_Table_NTC.html">NTC表格 →</a>
             </div>
-
-            <!-- <div class="card">
+            <div class="card" onclick="window.location.href='./NfcCoilDesignTool/index.html';return false">
                 <h2>NFC线圈设计工具</h2>
                 <p>用于设计和计算NFC线圈参数的专业工具,支持多种参数调整和实时预览。</p>
-                <a href="./NfcCoilDesignTool/index.html">进入工具 →</a>
             </div>
-            <div class="card">
+            <div class="card" onclick="window.location.href='./HexFormat/index.html';return false">
                 <h2>十六进制格式化</h2>
                 <p>提供十六进制数据的格式化和转换功能。</p>
-                <a href="./HexFormat/index.html">进入工具 →</a>
+            </div>
+            <div class="card" onclick="window.location.href='./FuncGen/index.html';return false">
+                <h2>函数统计</h2>
+                <p>用于生成和可视化各种数学函数的工具。</p>
+            </div>
+
+            <!-- <div class="card">
+                <h2>Desmos Desktop</h2>
+                <p>用于生成和可视化各种数学函数的工具。</p>
+                <a href="./Desmos-Desktop-master/index.html">进入工具 →</a>
             </div> -->
         </div>
     </div>
 </body>
-</html>
+
+</html>