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
@@ -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