mirror of
https://github.com/hicccc77/WeFlow.git
synced 2026-03-25 07:16:51 +00:00
更友好的跳转日期 #256;修复聊天记录显示不完整 #254;修复聊天 tab 对话页面“向下滚动查看更新消息”失效 #253
This commit is contained in:
@@ -903,6 +903,9 @@ function registerIpcHandlers() {
|
||||
ipcMain.handle('chat:getAllVoiceMessages', async (_, sessionId: string) => {
|
||||
return chatService.getAllVoiceMessages(sessionId)
|
||||
})
|
||||
ipcMain.handle('chat:getMessageDates', async (_, sessionId: string) => {
|
||||
return chatService.getMessageDates(sessionId)
|
||||
})
|
||||
ipcMain.handle('chat:resolveVoiceCache', async (_, sessionId: string, msgId: string) => {
|
||||
return chatService.resolveVoiceCache(sessionId, msgId)
|
||||
})
|
||||
|
||||
@@ -142,6 +142,7 @@ contextBridge.exposeInMainWorld('electronAPI', {
|
||||
getVoiceData: (sessionId: string, msgId: string, createTime?: number, serverId?: string | number) =>
|
||||
ipcRenderer.invoke('chat:getVoiceData', sessionId, msgId, createTime, serverId),
|
||||
getAllVoiceMessages: (sessionId: string) => ipcRenderer.invoke('chat:getAllVoiceMessages', sessionId),
|
||||
getMessageDates: (sessionId: string) => ipcRenderer.invoke('chat:getMessageDates', sessionId),
|
||||
resolveVoiceCache: (sessionId: string, msgId: string) => ipcRenderer.invoke('chat:resolveVoiceCache', sessionId, msgId),
|
||||
getVoiceTranscript: (sessionId: string, msgId: string, createTime?: number) => ipcRenderer.invoke('chat:getVoiceTranscript', sessionId, msgId, createTime),
|
||||
onVoiceTranscriptPartial: (callback: (payload: { msgId: string; text: string }) => void) => {
|
||||
|
||||
@@ -72,6 +72,9 @@ export interface Message {
|
||||
// 名片消息
|
||||
cardUsername?: string // 名片的微信ID
|
||||
cardNickname?: string // 名片的昵称
|
||||
// 转账消息
|
||||
transferPayerUsername?: string // 转账付款人
|
||||
transferReceiverUsername?: string // 转账收款人
|
||||
// 聊天记录
|
||||
chatRecordTitle?: string // 聊天记录标题
|
||||
chatRecordList?: Array<{
|
||||
@@ -723,7 +726,7 @@ class ChatService {
|
||||
// 1. 没有游标状态
|
||||
// 2. offset 为 0 (重新加载会话)
|
||||
// 3. batchSize 改变
|
||||
// 4. startTime 改变
|
||||
// 4. startTime/endTime 改变(视为全新查询)
|
||||
// 5. ascending 改变
|
||||
const needNewCursor = !state ||
|
||||
offset === 0 ||
|
||||
@@ -756,27 +759,35 @@ class ChatService {
|
||||
this.messageCursors.set(sessionId, state)
|
||||
|
||||
// 如果需要跳过消息(offset > 0),逐批获取但不返回
|
||||
// 注意:仅在 offset === 0 时重建游标最安全;
|
||||
// 当 startTime/endTime 变化导致重建时,offset 应由前端重置为 0
|
||||
if (offset > 0) {
|
||||
|
||||
console.warn(`[ChatService] 新游标需跳过 ${offset} 条消息(startTime=${startTime}, endTime=${endTime})`)
|
||||
let skipped = 0
|
||||
while (skipped < offset) {
|
||||
const maxSkipAttempts = Math.ceil(offset / batchSize) + 5 // 防止无限循环
|
||||
let attempts = 0
|
||||
while (skipped < offset && attempts < maxSkipAttempts) {
|
||||
attempts++
|
||||
const skipBatch = await wcdbService.fetchMessageBatch(state.cursor)
|
||||
if (!skipBatch.success) {
|
||||
console.error('[ChatService] 跳过消息批次失败:', skipBatch.error)
|
||||
return { success: false, error: skipBatch.error || '跳过消息失败' }
|
||||
}
|
||||
if (!skipBatch.rows || skipBatch.rows.length === 0) {
|
||||
|
||||
console.warn(`[ChatService] 跳过时数据耗尽: skipped=${skipped}/${offset}`)
|
||||
return { success: true, messages: [], hasMore: false }
|
||||
}
|
||||
skipped += skipBatch.rows.length
|
||||
state.fetched += skipBatch.rows.length
|
||||
if (!skipBatch.hasMore) {
|
||||
|
||||
console.warn(`[ChatService] 跳过后无更多数据: skipped=${skipped}/${offset}`)
|
||||
return { success: true, messages: [], hasMore: false }
|
||||
}
|
||||
}
|
||||
|
||||
if (attempts >= maxSkipAttempts) {
|
||||
console.error(`[ChatService] 跳过消息超过最大尝试次数: attempts=${attempts}`)
|
||||
}
|
||||
console.log(`[ChatService] 跳过完成: skipped=${skipped}, fetched=${state.fetched}`)
|
||||
}
|
||||
} else if (state && offset !== state.fetched) {
|
||||
// offset 与 fetched 不匹配,说明状态不一致
|
||||
@@ -3776,6 +3787,32 @@ class ChatService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取某会话中有消息的日期列表
|
||||
* 返回 YYYY-MM-DD 格式的日期字符串数组
|
||||
*/
|
||||
async getMessageDates(sessionId: string): Promise<{ success: boolean; dates?: string[]; error?: string }> {
|
||||
try {
|
||||
const connectResult = await this.ensureConnected()
|
||||
if (!connectResult.success) {
|
||||
return { success: false, error: connectResult.error || '数据库未连接' }
|
||||
}
|
||||
|
||||
const result = await wcdbService.getMessageDates(sessionId)
|
||||
if (!result.success) {
|
||||
throw new Error(result.error || '查询失败')
|
||||
}
|
||||
|
||||
const dates = result.dates || []
|
||||
|
||||
console.log(`[ChatService] 会话 ${sessionId} 共有 ${dates.length} 个有消息的日期`)
|
||||
return { success: true, dates }
|
||||
} catch (e) {
|
||||
console.error('[ChatService] 获取消息日期失败:', e)
|
||||
return { success: false, error: String(e) }
|
||||
}
|
||||
}
|
||||
|
||||
async getMessageById(sessionId: string, localId: number): Promise<{ success: boolean; message?: Message; error?: string }> {
|
||||
try {
|
||||
// 1. 尝试从缓存获取会话表信息
|
||||
|
||||
@@ -46,6 +46,7 @@ export class WcdbCore {
|
||||
private wcdbGetAnnualReportExtras: any = null
|
||||
private wcdbGetDualReportStats: any = null
|
||||
private wcdbGetGroupStats: any = null
|
||||
private wcdbGetMessageDates: any = null
|
||||
private wcdbOpenMessageCursor: any = null
|
||||
private wcdbOpenMessageCursorLite: any = null
|
||||
private wcdbFetchMessageBatch: any = null
|
||||
@@ -299,9 +300,6 @@ export class WcdbCore {
|
||||
return false
|
||||
}
|
||||
|
||||
// 关键修复:显式预加载依赖库 WCDB.dll 和 SDL2.dll
|
||||
// Windows 加载器默认不会查找子目录中的依赖,必须先将其加载到内存
|
||||
// 这可以解决部分用户因为 VC++ 运行时或 DLL 依赖问题导致的闪退
|
||||
const dllDir = dirname(dllPath)
|
||||
const wcdbCorePath = join(dllDir, 'WCDB.dll')
|
||||
if (existsSync(wcdbCorePath)) {
|
||||
@@ -478,6 +476,13 @@ export class WcdbCore {
|
||||
this.wcdbGetGroupStats = null
|
||||
}
|
||||
|
||||
// wcdb_status wcdb_get_message_dates(wcdb_handle handle, const char* session_id, char** out_json)
|
||||
try {
|
||||
this.wcdbGetMessageDates = this.lib.func('int32 wcdb_get_message_dates(int64 handle, const char* sessionId, _Out_ void** outJson)')
|
||||
} catch {
|
||||
this.wcdbGetMessageDates = null
|
||||
}
|
||||
|
||||
// wcdb_status wcdb_open_message_cursor(wcdb_handle handle, const char* session_id, int32_t batch_size, int32_t ascending, int32_t begin_timestamp, int32_t end_timestamp, wcdb_cursor* out_cursor)
|
||||
this.wcdbOpenMessageCursor = this.lib.func('int32 wcdb_open_message_cursor(int64 handle, const char* sessionId, int32 batchSize, int32 ascending, int32 beginTimestamp, int32 endTimestamp, _Out_ int64* outCursor)')
|
||||
|
||||
@@ -1194,6 +1199,29 @@ export class WcdbCore {
|
||||
}
|
||||
}
|
||||
|
||||
async getMessageDates(sessionId: string): Promise<{ success: boolean; dates?: string[]; error?: string }> {
|
||||
if (!this.ensureReady()) {
|
||||
return { success: false, error: 'WCDB 未连接' }
|
||||
}
|
||||
try {
|
||||
if (!this.wcdbGetMessageDates) {
|
||||
return { success: false, error: 'DLL 不支持 getMessageDates' }
|
||||
}
|
||||
const outPtr = [null as any]
|
||||
const result = this.wcdbGetMessageDates(this.handle, sessionId, outPtr)
|
||||
if (result !== 0 || !outPtr[0]) {
|
||||
// 空结果也可能是正常的(无消息)
|
||||
return { success: true, dates: [] }
|
||||
}
|
||||
const jsonStr = this.decodeJsonPtr(outPtr[0])
|
||||
if (!jsonStr) return { success: false, error: '解析日期列表失败' }
|
||||
const dates = JSON.parse(jsonStr)
|
||||
return { success: true, dates }
|
||||
} catch (e) {
|
||||
return { success: false, error: String(e) }
|
||||
}
|
||||
}
|
||||
|
||||
async getMessageTableStats(sessionId: string): Promise<{ success: boolean; tables?: any[]; error?: string }> {
|
||||
if (!this.ensureReady()) {
|
||||
return { success: false, error: 'WCDB 未连接' }
|
||||
|
||||
@@ -273,6 +273,10 @@ export class WcdbService {
|
||||
return this.callWorker('getMessageTableStats', { sessionId })
|
||||
}
|
||||
|
||||
async getMessageDates(sessionId: string): Promise<{ success: boolean; dates?: string[]; error?: string }> {
|
||||
return this.callWorker('getMessageDates', { sessionId })
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取消息元数据
|
||||
*/
|
||||
|
||||
@@ -78,6 +78,9 @@ if (parentPort) {
|
||||
case 'getMessageTableStats':
|
||||
result = await core.getMessageTableStats(payload.sessionId)
|
||||
break
|
||||
case 'getMessageDates':
|
||||
result = await core.getMessageDates(payload.sessionId)
|
||||
break
|
||||
case 'getMessageMeta':
|
||||
result = await core.getMessageMeta(payload.dbPath, payload.tableName, payload.limit, payload.offset)
|
||||
break
|
||||
|
||||
Reference in New Issue
Block a user