From db7817cc22e8395fdadf0454e830b1a5410796ea Mon Sep 17 00:00:00 2001 From: Forrest Date: Tue, 20 Jan 2026 18:25:05 +0800 Subject: [PATCH] =?UTF-8?q?refactor(WcdbCore,=20SettingsPage):=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E6=95=B0=E6=8D=AE=E5=BA=93=E6=B5=8B=E8=AF=95=E7=9A=84?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E5=A4=84=E7=90=86=EF=BC=9A=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E5=90=8E=E6=B4=BB=E8=B7=83=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=EF=BC=8C=E6=8B=86=E5=88=86=E8=AE=BE=E7=BD=AE=E9=A1=B5=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E4=BF=9D=E5=AD=98=E4=B8=8E=E8=BF=9E=E6=8E=A5=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E9=80=BB=E8=BE=91=EF=BC=8C=E9=81=BF=E5=85=8D=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E5=B9=B2=E6=89=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- electron/services/wcdbCore.ts | 19 +++++++++++++++++-- src/pages/SettingsPage.tsx | 13 +++---------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/electron/services/wcdbCore.ts b/electron/services/wcdbCore.ts index 7f8f32c..6144726 100644 --- a/electron/services/wcdbCore.ts +++ b/electron/services/wcdbCore.ts @@ -382,6 +382,12 @@ export class WcdbCore { return { success: true, sessionCount: 0 } } + // 记录当前活动连接,用于在测试结束后恢复(避免影响聊天页等正在使用的连接) + const hadActiveConnection = this.handle !== null + const prevPath = this.currentPath + const prevKey = this.currentKey + const prevWxid = this.currentWxid + if (!this.initialized) { const initOk = await this.initialize() if (!initOk) { @@ -424,8 +430,8 @@ export class WcdbCore { return { success: false, error: '无效的数据库句柄' } } - // 测试成功,使用 shutdown 清理所有资源(包括测试句柄) - // 这会中断当前活动连接,但 testConnection 本应该是独立测试 + // 测试成功:使用 shutdown 清理资源(包括测试句柄) + // 注意:shutdown 会断开当前活动连接,因此需要在测试后尝试恢复之前的连接 try { this.wcdbShutdown() this.handle = null @@ -437,6 +443,15 @@ export class WcdbCore { console.error('关闭测试数据库时出错:', closeErr) } + // 恢复测试前的连接(如果之前有活动连接) + if (hadActiveConnection && prevPath && prevKey && prevWxid) { + try { + await this.open(prevPath, prevKey, prevWxid) + } catch { + // 恢复失败则保持断开,由调用方处理 + } + } + return { success: true, sessionCount: 0 } } catch (e) { console.error('测试连接异常:', e) diff --git a/src/pages/SettingsPage.tsx b/src/pages/SettingsPage.tsx index 3f4e930..6fbe196 100644 --- a/src/pages/SettingsPage.tsx +++ b/src/pages/SettingsPage.tsx @@ -1,4 +1,4 @@ -import { useState, useEffect, useRef } from 'react' +import { useState, useEffect, useRef } from 'react' import { useAppStore } from '../stores/appStore' import { useThemeStore, themes } from '../stores/themeStore' import { useAnalyticsStore } from '../stores/analyticsStore' @@ -484,15 +484,8 @@ function SettingsPage() { await configService.setTranscribeLanguages(transcribeLanguages) await configService.setOnboardingDone(true) - showMessage('配置保存成功,正在测试连接...', true) - const result = await window.electronAPI.wcdb.testConnection(dbPath, decryptKey, wxid) - - if (result.success) { - setDbConnected(true, dbPath) - showMessage('配置保存成功!数据库连接正常', true) - } else { - showMessage(result.error || '数据库连接失败,请检查配置', false) - } + // 保存按钮只负责持久化配置,不做连接测试/重连,避免影响聊天页的活动连接 + showMessage('配置保存成功', true) } catch (e) { showMessage(`保存配置失败: ${e}`, false) } finally {