fix: 修复跨电脑对话/数据不同步

- 根因: 上传3秒防抖且失败不重试(关标签/服务重启期间推送丢失不补), 拉取仅登录/加载触发 -> 多设备分叉
- 新增: 登录后每60s周期性双向同步(push+pull自愈)
- 新增: visibilitychange/pagehide 立即上送, 关标签不丢改动
- applyLocal 值未变不写, 减少多余上送

验证: 构建通过, 新逻辑入包(index-8f321156.js), 控制台无报错 ✓

CHANGELOG记录
This commit is contained in:
personal82555 committed 2026-10-13 21:05:52 +08:00
1 parent 073028d5d3
commit fc8e1c7c37
2 files changed
+71 -27

No files matched your search

+63 -27
View File
@@ -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 };
}