feat: AI问答对话列表云同步 — 登录用户对话按id跨设备合并(上限30), localStorage变化3s防抖自动上传, 打开页面已登录自动拉取一次, 本机3s轮询感知云端回写实时刷新
This commit is contained in:
1 parent
c68ad09c65
commit
d16fbb1667
3 files changed
+46
-2
No files matched your search
@@ -6,6 +6,7 @@ const SYNC_KEYS = [
|
||||
'it-tools-recent-tools',
|
||||
'it-tools-bookmarks:sites',
|
||||
'menu-tool-option:collapsed-categories',
|
||||
'it-tools-ai-chat-convs',
|
||||
];
|
||||
|
||||
function collectLocal(): Record<string, any> {
|
||||
@@ -66,6 +67,21 @@ function mergeJsonArrays(a: string, b: string, keyField: string | null): string
|
||||
}
|
||||
}
|
||||
|
||||
/** 会话列表按 updatedAt 降序截断到 30 条 */
|
||||
function trimConvs(jsonStr: string): string {
|
||||
try {
|
||||
const arr = JSON.parse(jsonStr);
|
||||
if (!Array.isArray(arr)) {
|
||||
return jsonStr;
|
||||
}
|
||||
arr.sort((a: any, b: any) => (b?.updatedAt ?? 0) - (a?.updatedAt ?? 0));
|
||||
return JSON.stringify(arr.slice(0, 30));
|
||||
}
|
||||
catch {
|
||||
return jsonStr;
|
||||
}
|
||||
}
|
||||
|
||||
function mergeData(server: Record<string, any>, mine: Record<string, any>): Record<string, any> {
|
||||
const out: Record<string, any> = { ...server };
|
||||
for (const [k, v] of Object.entries(mine)) {
|
||||
@@ -80,6 +96,10 @@ function mergeData(server: Record<string, any>, mine: Record<string, any>): Reco
|
||||
if (k === 'favoriteToolsName' || k === 'it-tools-recent-tools' || k === 'it-tools-bookmarks:sites') {
|
||||
out[k] = mergeJsonArrays(out[k], v, k === 'it-tools-recent-tools' ? 'path' : null);
|
||||
}
|
||||
else if (k === 'it-tools-ai-chat-convs') {
|
||||
// 对话列表按 id 合并(多设备各自的会话都保留),上限 30 条
|
||||
out[k] = trimConvs(mergeJsonArrays(out[k], v, 'id'));
|
||||
}
|
||||
else {
|
||||
out[k] = v; // scalar configs: local wins
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import HeroGradient from '../assets/hero-gradient.svg?component';
|
||||
import MenuLayout from '../components/MenuLayout.vue';
|
||||
import NavbarButtons from '../components/NavbarButtons.vue';
|
||||
import { useStyleStore } from '@/stores/style.store';
|
||||
import { useCloudSync } from '@/composable/cloud-sync';
|
||||
import { config } from '@/config';
|
||||
import type { ToolCategory } from '@/tools/tools.types';
|
||||
import { useToolStore } from '@/tools/tools.store';
|
||||
@@ -18,6 +19,12 @@ import UserMenu from '@/components/UserMenu.vue';
|
||||
|
||||
const themeVars = useThemeVars();
|
||||
const styleStore = useStyleStore();
|
||||
|
||||
// 已登录用户每次打开页面:自动拉取一次云端数据(收藏/记录/对话等跨设备恢复)
|
||||
const { syncNow, isLoggedIn } = useCloudSync();
|
||||
if (isLoggedIn.value) {
|
||||
setTimeout(() => syncNow(), 1500);
|
||||
}
|
||||
const version = config.app.version;
|
||||
const commitSha = config.app.lastCommitSha.slice(0, 7);
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, nextTick, ref } from 'vue';
|
||||
import { computed, nextTick, onUnmounted, ref } from 'vue';
|
||||
import { useHead } from '@vueuse/head';
|
||||
import { getMachineId, licenseHeaders } from '@/tools/license/license';
|
||||
import { useGenHistory } from '@/tools/gen-history/gen-history';
|
||||
@@ -31,13 +31,30 @@ function loadConvs(): Conversation[] {
|
||||
}
|
||||
}
|
||||
|
||||
let lastSaved = localStorage.getItem(STORAGE_KEY) || '';
|
||||
|
||||
function saveConvs() {
|
||||
conversations.value = [...conversations.value]
|
||||
.sort((a, b) => b.updatedAt - a.updatedAt)
|
||||
.slice(0, 30);
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(conversations.value));
|
||||
lastSaved = JSON.stringify(conversations.value);
|
||||
localStorage.setItem(STORAGE_KEY, lastSaved);
|
||||
}
|
||||
|
||||
// 云端同步回写检测:登录用户在其他设备的对话同步到本机时,自动刷新列表(3 秒轮询)
|
||||
const syncTimer = setInterval(() => {
|
||||
const remote = localStorage.getItem(STORAGE_KEY);
|
||||
if (remote && remote !== lastSaved) {
|
||||
conversations.value = loadConvs();
|
||||
lastSaved = remote;
|
||||
if (!conversations.value.some(c => c.id === curId.value)) {
|
||||
curId.value = conversations.value[0]?.id || '';
|
||||
}
|
||||
}
|
||||
}, 3000);
|
||||
|
||||
onUnmounted(() => clearInterval(syncTimer));
|
||||
|
||||
const conversations = ref<Conversation[]>(loadConvs());
|
||||
const curId = ref(conversations.value[0]?.id || '');
|
||||
const search = ref('');
|
||||
|
||||
Reference in new issue
Block a user