diff --git a/src/main/index.ts b/src/main/index.ts index 2e572f2..74a44d3 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -297,7 +297,12 @@ app.whenReady().then(async () => { const nextWechatDb = await WechatDb.create(key, settings.dbRoot) const resolvedRoot = nextWechatDb.getWcdb4Client().getAccountRoot() if (resolvedRoot && resolvedRoot !== settings.dbRoot) { - saveSettings({ ...settings, dbRoot: resolvedRoot }) + // 同步更新 imageKeyRoot,避免自动获取图片密钥时扫描到错误目录 + saveSettings({ + ...settings, + dbRoot: resolvedRoot, + imageKeyRoot: resolvedRoot + }) } chat.setChatDb(nextWechatDb) const wcdb4Client = nextWechatDb.getWcdb4Client() @@ -411,7 +416,13 @@ app.whenReady().then(async () => { ipcMain.handle('key:autoGetImageKey', async (event, options?: { save?: boolean }) => { const settings = loadSettings() const self = chat.getSelfAccountInfo() - const accountRoot = settings.imageKeyRoot || self?.accountRoot || settings.dbRoot + // 优先级:chat 真实识别到的根 → self.accountRoot → settings.imageKeyRoot → settings.dbRoot + // 必须先看 chat.getCurrentAccountRoot(),否则 settings 缓存漂移会导致扫错目录。 + const accountRoot = + chat.getCurrentAccountRoot() || + self?.accountRoot || + settings.imageKeyRoot || + settings.dbRoot const wxid = self?.wxid const onStatus = (message: string): void => { if (!event.sender.isDestroyed()) event.sender.send('key:imageKeyStatus', { message }) @@ -788,6 +799,11 @@ app.whenReady().then(async () => { ipcMain.handle('db:reopenWithRoot', (_, accountRoot: string) => { const ok = chat.reopenWithRoot(accountRoot) if (!ok) return { success: false, error: '数据库未初始化或重新打开失败' } + // 同步 imageKeyRoot,避免自动获取扫描到旧目录 + const settings = loadSettings() + if (accountRoot && accountRoot !== settings.imageKeyRoot) { + saveSettings({ ...settings, imageKeyRoot: accountRoot }) + } const info = chat.getSelfAccountInfo() return { success: true, info } }) diff --git a/src/main/services/settings-store.ts b/src/main/services/settings-store.ts index 5d6a61e..fc135c5 100644 --- a/src/main/services/settings-store.ts +++ b/src/main/services/settings-store.ts @@ -4,6 +4,22 @@ import path from 'path' import os from 'os' import { discoverWindowsDbRoots } from '../windows-db-root-discovery' +/** + * 把 V3 时代的 "...\\Documents\\WeChat Files" 路径重定向到 + * "...\\Documents\\xwechat_files"(V4)。如果 xwechat_files 不存在则保留原值。 + * 仅支持 WeChat 4.0:自动纠正用户机器上残留的旧路径。 + */ +function redirectLegacyWeChatFilesToXwechat(candidate: string): string { + if (!candidate) return candidate + const normalized = candidate.replace(/[\\/]+$/, '') + const lowered = normalized.toLowerCase() + const legacyMarker = `${path.sep}wechat files` + if (!lowered.endsWith(legacyMarker)) return candidate + const redirected = `${normalized.slice(0, -legacyMarker.length)}${path.sep}xwechat_files` + if (fs.existsSync(redirected)) return redirected + return candidate +} + export interface AppSettings { dbRoot: string apiEnabled: boolean @@ -25,16 +41,17 @@ function getDefaultDbRoot(): string { function getDefaultDbRootCandidates(home: string): string[] { if (process.platform !== 'win32') { + // macOS 仅支持 WeChat 4.0 路径(xwechat_files) return [ path.join(home, 'Library/Containers/com.tencent.xinWeChat/Data/Documents/xwechat_files') ] } + // 仅支持 WeChat 4.0:剔除 V3 时代的 "WeChat Files" 目录, + // 只认 xwechat_files(含 Documents\ 和 AppData\Roaming\Tencent\ 两种合法位置)。 const candidates = [ ...getWeflowDbPathCandidates(home), - path.join(home, 'Documents', 'WeChat Files'), path.join(home, 'Documents', 'xwechat_files'), - path.join(home, 'WeChat Files'), path.join(os.homedir(), 'AppData', 'Roaming', 'Tencent', 'xwechat_files') ] @@ -123,9 +140,19 @@ export function loadSettings(): AppSettings { if (process.platform === 'win32' && !isUsableDbRoot(cache.dbRoot)) { cache.dbRoot = getDefaultDbRoot() } - if (!cache.imageKeyRoot) { + // 同步:imageKeyRoot 必须跟随 dbRoot 更新, + // 否则自动获取会扫错目录(旧 bug:状态面板显示 D 盘,自动获取扫 C 盘)。 + if (!cache.imageKeyRoot || !isUsableDbRoot(cache.imageKeyRoot)) { cache.imageKeyRoot = cache.dbRoot } + // V4-only 兜底:如果 imageKeyRoot 指向旧的 "WeChat Files"(V3 路径), + // 重定向到同一父目录下的 xwechat_files(V4)。 + if (cache.imageKeyRoot) { + cache.imageKeyRoot = redirectLegacyWeChatFilesToXwechat(cache.imageKeyRoot) + } + if (cache.dbRoot) { + cache.dbRoot = redirectLegacyWeChatFilesToXwechat(cache.dbRoot) + } return cache } } catch (error) { diff --git a/src/main/wcdb4-client.ts b/src/main/wcdb4-client.ts index dcf065f..e10504d 100644 --- a/src/main/wcdb4-client.ts +++ b/src/main/wcdb4-client.ts @@ -311,11 +311,10 @@ export class Wcdb4Client { private static getDefaultRootCandidates(): string[] { const home = os.homedir() if (process.platform === 'win32') { + // 仅支持 WeChat 4.0:只认 xwechat_files,V3 时代的 "WeChat Files" 不再加入候选 const candidates = [ ...Wcdb4Client.getWeflowDbPathCandidates(home), - path.join(home, 'Documents', 'WeChat Files'), path.join(home, 'Documents', 'xwechat_files'), - path.join(home, 'WeChat Files'), path.join(home, 'AppData', 'Roaming', 'Tencent', 'xwechat_files') ] candidates.push(...discoverWindowsDbRoots()) diff --git a/src/main/windows-db-root-discovery.ts b/src/main/windows-db-root-discovery.ts index aa74fb8..5da052e 100644 --- a/src/main/windows-db-root-discovery.ts +++ b/src/main/windows-db-root-discovery.ts @@ -1,7 +1,8 @@ import fs from 'fs-extra' import path from 'path' -const DB_ROOT_NAMES = new Set(['xwechat_files', 'wechat files']) +// 仅支持 WeChat 4.0:只扫描 xwechat_files;旧 V3 时代的 "WeChat Files" 不再纳入候选 +const DB_ROOT_NAMES = new Set(['xwechat_files']) const SKIPPED_DIRECTORY_NAMES = new Set([ '$recycle.bin', 'system volume information',