From bfcd154a254463d078edefbfdd7ec8e84a46d286 Mon Sep 17 00:00:00 2001 From: xuncha <1658671838@qq.com> Date: Sat, 31 Jan 2026 18:11:55 +0800 Subject: [PATCH] =?UTF-8?q?wxid=E5=8F=AF=E4=BB=A5=E8=87=AA=E5=B7=B1?= =?UTF-8?q?=E9=80=89=E6=8B=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- electron/main.ts | 4 ++ electron/preload.ts | 1 + electron/services/dbPathService.ts | 42 ++++++++++++++++ electron/services/keyService.ts | 13 +++-- src/pages/WelcomePage.scss | 54 ++++++++++++++++++++- src/pages/WelcomePage.tsx | 78 ++++++++++++++++++++++++++---- src/types/electron.d.ts | 1 + 7 files changed, 177 insertions(+), 16 deletions(-) diff --git a/electron/main.ts b/electron/main.ts index 4a219b8..f152dc3 100644 --- a/electron/main.ts +++ b/electron/main.ts @@ -674,6 +674,10 @@ function registerIpcHandlers() { return dbPathService.scanWxids(rootPath) }) + ipcMain.handle('dbpath:scanWxidCandidates', async (_, rootPath: string) => { + return dbPathService.scanWxidCandidates(rootPath) + }) + ipcMain.handle('dbpath:getDefault', async () => { return dbPathService.getDefaultPath() }) diff --git a/electron/preload.ts b/electron/preload.ts index c401232..7d6d5c3 100644 --- a/electron/preload.ts +++ b/electron/preload.ts @@ -71,6 +71,7 @@ contextBridge.exposeInMainWorld('electronAPI', { dbPath: { autoDetect: () => ipcRenderer.invoke('dbpath:autoDetect'), scanWxids: (rootPath: string) => ipcRenderer.invoke('dbpath:scanWxids', rootPath), + scanWxidCandidates: (rootPath: string) => ipcRenderer.invoke('dbpath:scanWxidCandidates', rootPath), getDefault: () => ipcRenderer.invoke('dbpath:getDefault') }, diff --git a/electron/services/dbPathService.ts b/electron/services/dbPathService.ts index 038ee2f..ee15b02 100644 --- a/electron/services/dbPathService.ts +++ b/electron/services/dbPathService.ts @@ -118,6 +118,48 @@ export class DbPathService { } } + /** + * 扫描目录名候选(仅包含下划线的文件夹,排除 all_users) + */ + scanWxidCandidates(rootPath: string): WxidInfo[] { + const wxids: WxidInfo[] = [] + + try { + if (existsSync(rootPath)) { + const entries = readdirSync(rootPath) + for (const entry of entries) { + const entryPath = join(rootPath, entry) + let stat: ReturnType + try { + stat = statSync(entryPath) + } catch { + continue + } + + if (!stat.isDirectory()) continue + const lower = entry.toLowerCase() + if (lower === 'all_users') continue + if (!entry.includes('_')) continue + + wxids.push({ wxid: entry, modifiedTime: stat.mtimeMs }) + } + } + + if (wxids.length === 0) { + const rootName = basename(rootPath) + if (rootName.includes('_') && rootName.toLowerCase() !== 'all_users') { + const rootStat = statSync(rootPath) + wxids.push({ wxid: rootName, modifiedTime: rootStat.mtimeMs }) + } + } + } catch { } + + return wxids.sort((a, b) => { + if (b.modifiedTime !== a.modifiedTime) return b.modifiedTime - a.modifiedTime + return a.wxid.localeCompare(b.wxid) + }) + } + /** * 扫描 wxid 列表 */ diff --git a/electron/services/keyService.ts b/electron/services/keyService.ts index e17c285..bdec7de 100644 --- a/electron/services/keyService.ts +++ b/electron/services/keyService.ts @@ -451,7 +451,7 @@ export class KeyService { return false } - private async closeWeChatWindows(timeoutMs = 8000): Promise { + private async closeWeChatWindows(): Promise { if (!this.ensureUser32()) return false let requested = false @@ -473,13 +473,15 @@ export class KeyService { this.EnumWindows(enumWindowsCallback, 0) this.koffi.unregister(enumWindowsCallback) - if (!requested) return true - return await this.waitForWeChatExit(timeoutMs) + return requested } private async killWeChatProcesses(): Promise { - const gracefulOk = await this.closeWeChatWindows(8000) - if (gracefulOk) return true + const requested = await this.closeWeChatWindows() + if (requested) { + const gracefulOk = await this.waitForWeChatExit(1500) + if (gracefulOk) return true + } try { await execFileAsync('taskkill', ['/F', '/T', '/IM', 'Weixin.exe']) @@ -491,6 +493,7 @@ export class KeyService { return await this.waitForWeChatExit(5000) } + // --- Window Detection --- private getWindowTitle(hWnd: any): string { diff --git a/src/pages/WelcomePage.scss b/src/pages/WelcomePage.scss index c115f75..395b24e 100644 --- a/src/pages/WelcomePage.scss +++ b/src/pages/WelcomePage.scss @@ -435,6 +435,58 @@ } } +.wxid-select { + position: relative; +} + +.wxid-dropdown { + position: absolute; + top: calc(100% + 6px); + left: 0; + right: 0; + background: var(--bg-primary); + border: 1px solid var(--border-color); + border-radius: 12px; + padding: 6px; + max-height: 220px; + overflow: auto; + z-index: 20; + box-shadow: 0 12px 24px rgba(0, 0, 0, 0.08); +} + +.wxid-option { + width: 100%; + border: none; + background: transparent; + display: flex; + align-items: center; + justify-content: space-between; + gap: 12px; + padding: 10px 12px; + border-radius: 8px; + cursor: pointer; + color: var(--text-primary); + font-size: 13px; + + &:hover { + background: var(--bg-hover); + } + + &.active { + background: var(--primary-light); + } +} + +.wxid-name { + font-weight: 600; +} + +.wxid-time { + color: var(--text-tertiary); + font-size: 12px; + white-space: nowrap; +} + .field-with-toggle { position: relative; } @@ -750,4 +802,4 @@ transform: scale(1); opacity: 1; } -} \ No newline at end of file +} diff --git a/src/pages/WelcomePage.tsx b/src/pages/WelcomePage.tsx index c68c64f..a1e556b 100644 --- a/src/pages/WelcomePage.tsx +++ b/src/pages/WelcomePage.tsx @@ -1,4 +1,4 @@ -import { useState, useEffect } from 'react' +import { useState, useEffect, useRef } from 'react' import { useNavigate } from 'react-router-dom' import { useAppStore } from '../stores/appStore' import { dialog } from '../services/ipc' @@ -35,6 +35,8 @@ function WelcomePage({ standalone = false }: WelcomePageProps) { const [cachePath, setCachePath] = useState('') const [wxid, setWxid] = useState('') const [wxidOptions, setWxidOptions] = useState>([]) + const [showWxidSelect, setShowWxidSelect] = useState(false) + const wxidSelectRef = useRef(null) const [error, setError] = useState('') const [isConnecting, setIsConnecting] = useState(false) const [isDetectingPath, setIsDetectingPath] = useState(false) @@ -127,8 +129,22 @@ function WelcomePage({ standalone = false }: WelcomePageProps) { useEffect(() => { setWxidOptions([]) setWxid('') + setShowWxidSelect(false) }, [dbPath]) + useEffect(() => { + const handleClickOutside = (event: MouseEvent) => { + if (!showWxidSelect) return + const target = event.target as Node + if (wxidSelectRef.current && !wxidSelectRef.current.contains(target)) { + setShowWxidSelect(false) + } + } + + document.addEventListener('mousedown', handleClickOutside) + return () => document.removeEventListener('mousedown', handleClickOutside) + }, [showWxidSelect]) + const currentStep = steps[stepIndex] const rootClassName = `welcome-page${isClosing ? ' is-closing' : ''}${standalone ? ' is-standalone' : ''}` const showWindowControls = standalone @@ -217,6 +233,28 @@ function WelcomePage({ standalone = false }: WelcomePageProps) { } } + const handleScanWxidCandidates = async () => { + if (!dbPath) { + setError('请先选择数据库目录') + return + } + if (isScanningWxid) return + setIsScanningWxid(true) + setError('') + try { + const wxids = await window.electronAPI.dbPath.scanWxidCandidates(dbPath) + setWxidOptions(wxids) + setShowWxidSelect(true) + if (!wxids.length) { + setError('未检测到可用的账号目录,请检查路径') + } + } catch (e) { + setError(`扫描失败: ${e}`) + } finally { + setIsScanningWxid(false) + } + } + const handleAutoGetDbKey = async () => { if (isFetchingDbKey) return setIsFetchingDbKey(true) @@ -556,14 +594,35 @@ function WelcomePage({ standalone = false }: WelcomePageProps) { {currentStep.id === 'key' && (
- setWxid(e.target.value)} - /> +
+ setWxid(e.target.value)} + /> + {showWxidSelect && wxidOptions.length > 0 && ( +
+ {wxidOptions.map((opt) => ( + + ))} +
+ )} +
@@ -733,4 +792,3 @@ function WelcomePage({ standalone = false }: WelcomePageProps) { } export default WelcomePage - diff --git a/src/types/electron.d.ts b/src/types/electron.d.ts index 2e424ea..892f430 100644 --- a/src/types/electron.d.ts +++ b/src/types/electron.d.ts @@ -42,6 +42,7 @@ export interface ElectronAPI { dbPath: { autoDetect: () => Promise<{ success: boolean; path?: string; error?: string }> scanWxids: (rootPath: string) => Promise + scanWxidCandidates: (rootPath: string) => Promise getDefault: () => Promise } wcdb: {