From 184e9a2ed7d6880e97c074eb6e5b09f1a0702e1d Mon Sep 17 00:00:00 2001
From: zjk <852185829@qq.com>
Date: 星期四, 23 七月 2026 10:44:59 +0800
Subject: [PATCH] feat: 优化多模块图片处理、离线同步与页面业务逻辑,修复已知问题

---
 utils/syncEngine.js |   66 ++------------------------------
 1 files changed, 5 insertions(+), 61 deletions(-)

diff --git a/utils/syncEngine.js b/utils/syncEngine.js
index c6024bb..69ecaa6 100644
--- a/utils/syncEngine.js
+++ b/utils/syncEngine.js
@@ -16,8 +16,6 @@
  */
 export const startSyncQueue = async () => {
   if (isSyncing) {
-    console.log('[Sync] 当前正在同步中,跳过本次触发');
-    // uni.showModal({ title: '同步诊断', content: '当前已有同步任务正在进行中,跳过本次触发', showCancel: false });
     return;
   }
 
@@ -30,26 +28,20 @@
   });
 
   if (networkType === 'none') {
-    console.log('[Sync] 当前无网络,取消同步');
-    // uni.showModal({ title: '同步诊断', content: '检测到当前无网络 (networkType: none),取消同步', showCancel: false });
     return;
   }
 
   isSyncing = true;
-  console.log('[Sync] 开始执行同步队列...');
 
   try {
     let pendingRecords = [];
     try {
       pendingRecords = await getPendingSyncRecords();
     } catch (dbErr) {
-    //   uni.showModal({ title: '获取待同步数据失败', content: JSON.stringify(dbErr), showCancel: false });
       return;
     }
 
     if (pendingRecords.length === 0) {
-      console.log('[Sync] 无待同步记录');
-    //   uni.showModal({ title: '同步诊断', content: '数据库中查询不到待同步记录', showCancel: false });
       isSyncing = false;
       return;
     }
@@ -59,25 +51,19 @@
     const signoutDataToPull = [];
     const recordsToSync = [];
 
-    // uni.showModal({
-    //   title: '调试日志 (同步前)',
-    //   content: `共查询到 ${pendingRecords.length} 条待同步记录`,
-    //   showCancel: false
-    // });
-
     for (let record of pendingRecords) {
       // 如果创建时间距离当前超过 5 分钟,认为是离线堆积的旧数据,走批量 pull 接口
       const isOfflineOldData = Date.now() - record.create_time > 5 * 60 * 1000;
 
       try {
-        let payload = record.payload;
+        let payload = {
+          ...(record.payload || {}),
+        };
 
-        // [暂时跳过图片上传测试]
-        // 1. 如果存在本地图片,先上传图片
-        /*
+        // 1. 如果存在本地图片,先上传图片,再把云端地址回填到业务 payload
         if (record.local_img_path) {
           const uploadRes = await uploadImage(record.local_img_path);
-          if (uploadRes && uploadRes.code === 200) {
+          if (uploadRes && uploadRes.code === 200 && uploadRes.url) {
             // 根据记录类型,将云端 URL 赋值给不同的业务 payload 字段
             if (record.type === 'signout') {
               payload.signImageUrl = uploadRes.url;
@@ -88,7 +74,6 @@
             throw new Error('图片上传失败');
           }
         }
-        */
 
         if (isOfflineOldData) {
           // 离线旧数据收集到数组中,准备批量提交
@@ -103,54 +88,27 @@
         }
       } catch (innerErr) {
         console.error(`[Sync] 处理记录 ${record.id} 的图片或数据时失败:`, innerErr);
-        // 通过弹窗暴露底层失败原因
-        // uni.showModal({
-        //   title: '离线同步诊断 (图片/前置)',
-        //   content: `同步记录被阻断:\n${innerErr.message || JSON.stringify(innerErr)}`,
-        //   showCancel: false
-        // });
         // 如果处理单条前置(如传图)失败,跳过该条,等待下次轮询
       }
     }
-
-    // uni.showModal({
-    //   title: '调试日志 (分流结果)',
-    //   content: `单条实时: ${recordsToSync.length}条\n批量签到: ${signinDataToPull.length}条\n批量签离: ${signoutDataToPull.length}条`,
-    //   showCancel: false
-    // });
 
     // 2. 提交实时单条业务数据
     for (let item of recordsToSync) {
       try {
         let submitRes;
         if (item.type === 'signout') {
-        //   uni.showModal({
-        //     title: '调试日志 (单条同步)',
-        //     content: `准备调用签离接口: /api/visitorRecord/idCardSingOut\n参数: ${JSON.stringify(item.payload)}`,
-        //     showCancel: false
-        //   });
           submitRes = await idCardSingOut(item.payload);
         } else {
           // type 为 appointment, online, 或它们加上 _signedout 后缀时,都作为进场记录同步
-        //   uni.showModal({
-        //     title: '调试日志 (单条同步)',
-        //     content: `准备调用签到接口: /api/visitorRecord/add\n参数: ${JSON.stringify(item.payload)}`,
-        //     showCancel: false
-        //   });
           submitRes = await addVisitorRecord(item.payload);
         }
 
         if (submitRes && submitRes.code === 200) {
           await markRecordSynced(item.recordId);
           if (item.localImgPath) await deleteLocalImage(item.localImgPath);
-          console.log(`[Sync] 记录 ${item.recordId} 同步成功 (单条实时)`);
-        //   uni.showModal({ title: '单条同步成功', content: `记录 ${item.recordId} 同步成功`, showCancel: false });
-        } else {
-        //   uni.showModal({ title: '单条同步失败', content: `接口返回: ${JSON.stringify(submitRes)}`, showCancel: false });
         }
       } catch (err) {
         console.error(`[Sync] 单条同步 ${item.recordId} 失败:`, err);
-        // uni.showModal({ title: '单条同步异常', content: `错误信息: ${err.message || JSON.stringify(err)}`, showCancel: false });
       }
     }
 
@@ -164,14 +122,9 @@
                await markRecordSynced(item.recordId);
                if (item.localImgPath) await deleteLocalImage(item.localImgPath);
             }
-            console.log(`[Sync] 批量补发签到数据 ${signinDataToPull.length} 条成功`);
-            // uni.showModal({ title: '批量签到同步成功', content: `成功同步 ${signinDataToPull.length} 条`, showCancel: false });
-         } else {
-            // uni.showModal({ title: '批量签到同步失败', content: `接口返回: ${JSON.stringify(pullRes)}`, showCancel: false });
          }
        } catch (err) {
          console.error('[Sync] 批量补发签到数据失败:', err);
-        //  uni.showModal({ title: '批量签到同步异常', content: `错误信息: ${err.message || JSON.stringify(err)}`, showCancel: false });
        }
     }
 
@@ -185,20 +138,14 @@
                await markRecordSynced(item.recordId);
                if (item.localImgPath) await deleteLocalImage(item.localImgPath);
             }
-            console.log(`[Sync] 批量补发签离数据 ${signoutDataToPull.length} 条成功`);
-            // uni.showModal({ title: '批量签离同步成功', content: `成功同步 ${signoutDataToPull.length} 条`, showCancel: false });
-         } else {
-            // uni.showModal({ title: '批量签离同步失败', content: `接口返回: ${JSON.stringify(pullRes)}`, showCancel: false });
          }
        } catch (err) {
          console.error('[Sync] 批量补发签离数据失败:', err);
-        //  uni.showModal({ title: '批量签离同步异常', content: `错误信息: ${err.message || JSON.stringify(err)}`, showCancel: false });
        }
     }
 
   } catch (error) {
     console.error('[Sync] 队列执行异常:', error);
-    // uni.showModal({ title: '队列执行异常', content: `错误信息: ${error.message || JSON.stringify(error)}`, showCancel: false });
   } finally {
     isSyncing = false;
   }
@@ -208,8 +155,6 @@
  * 每日凌晨或空闲时清理超期废弃数据并同步最新配置字典
  */
 export const runDailyCleanup = async () => {
-  console.log('[Sync] 执行超期数据清理与基础数据同步...');
-
   // 1. 清理过期业务记录
   cleanOldRecords().catch(err => {
     console.error('[Sync] 清理过期记录失败:', err);
@@ -226,7 +171,6 @@
       const res = await getVisitAreaTree();
       if (res && res.code === 200 && Array.isArray(res.data)) {
         await saveOfflineConfig('visit_area_tree', res.data);
-        console.log('[Sync] 每日同步:访问区域数据已更新至本地缓存');
       }
     }
   } catch (err) {

--
Gitblit v1.7.1