fix: 修复跨电脑对话/数据不同步
- 根因: 上传3秒防抖且失败不重试(关标签/服务重启期间推送丢失不补), 拉取仅登录/加载触发 -> 多设备分叉 - 新增: 登录后每60s周期性双向同步(push+pull自愈) - 新增: visibilitychange/pagehide 立即上送, 关标签不丢改动 - applyLocal 值未变不写, 减少多余上送 验证: 构建通过, 新逻辑入包(index-8f321156.js), 控制台无报错 ✓ CHANGELOG记录
This commit is contained in:
1 parent
073028d5d3
commit
fc8e1c7c37
2 files changed
+71
-27
No files matched your search
@@ -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 域名消失 ✓
|
||||
|
||||
@@ -35,11 +35,16 @@ function applyLocal(data: Record<string, any>) {
|
||||
for (const [k, v] of Object.entries(data)) {
|
||||
if (k === '__gen-history') {
|
||||
for (const [hk, hv] of Object.entries(v as Record<string, string>)) {
|
||||
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<typeof setInterval> | 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 };
|
||||
}
|
||||
Reference in new issue
Block a user