From fc8e1c7c3759cfdc1901b7153f0d925de316ed21 Mon Sep 17 00:00:00 2001 From: personal82555 Date: Tue, 13 Oct 2026 21:05:52 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=B7=A8=E7=94=B5?= =?UTF-8?q?=E8=84=91=E5=AF=B9=E8=AF=9D/=E6=95=B0=E6=8D=AE=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 根因: 上传3秒防抖且失败不重试(关标签/服务重启期间推送丢失不补), 拉取仅登录/加载触发 -> 多设备分叉 - 新增: 登录后每60s周期性双向同步(push+pull自愈) - 新增: visibilitychange/pagehide 立即上送, 关标签不丢改动 - applyLocal 值未变不写, 减少多余上送 验证: 构建通过, 新逻辑入包(index-8f321156.js), 控制台无报错 ✓ CHANGELOG记录 --- CHANGELOG.md | 8 ++++ src/composable/cloud-sync.ts | 90 +++++++++++++++++++++++++----------- 2 files changed, 71 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0887b0a..2426fec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file. See [standa ## Version 2026.10.12-it-tools-pro +### ☁️ 修复跨电脑对话/数据不同步 +- 根因:上传靠 3 秒防抖且**失败不重试**(关标签页/服务重启期间的推送会丢且不补),拉取仅在登录与页面加载时触发 → 多设备数据分叉 +- 新增 **周期性双向同步**:登录后每 60 秒 push+pull 自愈,任一设备的改动(含对话)60 秒内收敛到其他设备 +- 新增 **页面隐藏/关闭立即上送**(`visibilitychange`/`pagehide` 触发 flush,不等防抖),关标签页不丢改动 +- `applyLocal` 值未变则不写,避免每次周期同步触发多余上送 +- 说明:源电脑需处于登录状态,其对话才会推到云端;登录后会自动合并 + + ### 🔏 印章生成器默认网址改为 it.88531.cn - 默认文本元素 `www.shephe.com` → `it.88531.cn` - 验证:工具页已显示 it.88531.cn、旧 shephe 域名消失 ✓ diff --git a/src/composable/cloud-sync.ts b/src/composable/cloud-sync.ts index ecefec2..e9381d4 100644 --- a/src/composable/cloud-sync.ts +++ b/src/composable/cloud-sync.ts @@ -35,11 +35,16 @@ function applyLocal(data: Record) { for (const [k, v] of Object.entries(data)) { if (k === '__gen-history') { for (const [hk, hv] of Object.entries(v as Record)) { - localStorage.setItem(hk, hv); + if (localStorage.getItem(hk) !== hv) { + localStorage.setItem(hk, hv); + } } } else if (typeof v === 'string') { - localStorage.setItem(k, v); + // 值未变则不写:避免每次周期同步都触发自动上送 + if (localStorage.getItem(k) !== v) { + localStorage.setItem(k, v); + } } } } @@ -154,6 +159,17 @@ function scheduleAutoSync() { }, 3000); } +// 页面隐藏/关闭时立即上送(不等 3 秒防抖),避免关标签页丢失未推送的改动 +function flushPushNow() { + if (useUserAuth().isLoggedIn.value) { + if (autoPending) { + clearTimeout(autoPending); + autoPending = undefined; + } + pushUpOnly().catch(() => {}); + } +} + // wrap localStorage so any write to a synced key schedules an upload if (typeof localStorage !== 'undefined') { const origSet = localStorage.setItem.bind(localStorage); @@ -172,32 +188,52 @@ if (typeof localStorage !== 'undefined') { }; } +/** push local data up, pull server data down, merge both ways */ +async function doSyncNow(): Promise<{ ok: boolean; message: string }> { + if (!useUserAuth().isLoggedIn.value) { + return { ok: false, message: '未登录' }; + } + const mine = collectLocal(); + const { status, body } = await apiFetch('/user/data'); + if (status !== 200) { + return { ok: false, message: body.message || '同步失败' }; + } + const server = body.data?.data ?? {}; + const merged = mergeData(server, mine); + const put = await apiFetch('/user/data', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ data: merged }), + }); + if (put.status !== 200) { + return { ok: false, message: put.body.message || '同步失败' }; + } + applyLocal(merged); + return { ok: true, message: '同步完成' }; +} + +// ===== 周期性双向同步:任一设备的改动在 60s 内收敛到其他设备(切换电脑即可看到对话)===== +let periodicTimer: ReturnType | undefined; +function ensurePeriodicSync() { + if (periodicTimer) return; + periodicTimer = setInterval(() => { + if (useUserAuth().isLoggedIn.value) { + doSyncNow().catch(() => {}); + } + }, 60000); +} +ensurePeriodicSync(); + +// 页面隐藏/切走/关闭时立即上送,避免关标签页丢失未推送改动 +if (typeof document !== 'undefined') { + document.addEventListener('visibilitychange', () => { + if (document.visibilityState === 'hidden') flushPushNow(); + }); + window.addEventListener('pagehide', flushPushNow); +} + export function useCloudSync() { const { isLoggedIn } = useUserAuth(); - /** push local data up, pull server data down, merge both ways */ - async function syncNow(): Promise<{ ok: boolean; message: string }> { - if (!isLoggedIn.value) { - return { ok: false, message: '未登录' }; - } - const mine = collectLocal(); - const { status, body } = await apiFetch('/user/data'); - if (status !== 200) { - return { ok: false, message: body.message || '同步失败' }; - } - const server = body.data?.data ?? {}; - const merged = mergeData(server, mine); - const put = await apiFetch('/user/data', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ data: merged }), - }); - if (put.status !== 200) { - return { ok: false, message: put.body.message || '同步失败' }; - } - applyLocal(merged); - return { ok: true, message: '同步完成' }; - } - - return { syncNow, isLoggedIn }; + return { syncNow: doSyncNow, isLoggedIn }; }