完善用户登录态的感知

This commit is contained in:
2025-11-12 15:07:24 +08:00
parent 7b8d31f899
commit 787906c566
4 changed files with 477 additions and 43 deletions
+62
View File
@@ -1,6 +1,7 @@
import { app, BrowserWindow, ipcMain, screen, globalShortcut, clipboard } from 'electron'
import { join } from 'path'
import { chromium, BrowserContext } from 'playwright'
import { existsSync, rmSync } from 'fs'
import { ScraperFactory } from './scrapers'
import { XiaoheiheScrap } from './scrapers/xiaoheihe'
import { GenericScraper } from './scrapers/generic'
@@ -237,6 +238,33 @@ async function waitForQrCodeLogin(): Promise<{
}
}
// Check login status for a platform (fast - cookie-based only)
async function checkPlatformLoginFast(url: string): Promise<{
success: boolean
isLoggedIn: boolean
username?: string
error?: string
}> {
try {
const service = platformServiceFactory.getService(url)
if (!service) {
return { success: false, isLoggedIn: false, error: '不支持的平台' }
}
const context = await getPersistentContext()
const loginStatus = await service.checkLoginStatusFast(context)
return { success: true, ...loginStatus }
} catch (error) {
console.error('Check platform login fast error:', error)
return {
success: false,
isLoggedIn: false,
error: error instanceof Error ? error.message : '检查登录状态失败'
}
}
}
// Check login status for a platform
async function checkPlatformLogin(url: string): Promise<{
success: boolean
@@ -387,6 +415,11 @@ app.whenReady().then(() => {
}
})
// Handle check platform login status (fast - cookie-based only)
ipcMain.handle('check-platform-login-fast', async (_, url: string) => {
return await checkPlatformLoginFast(url)
})
// Handle check platform login status
ipcMain.handle('check-platform-login', async (_, url: string) => {
return await checkPlatformLogin(url)
@@ -407,6 +440,35 @@ app.whenReady().then(() => {
return await waitForQrCodeLogin()
})
// Handle logout
ipcMain.handle('logout-platform', async (_, platform: string) => {
try {
if (platform === 'xiaoheihe') {
// Close the persistent context to clear cookies and session
if (persistentContext) {
await persistentContext.close()
persistentContext = null
}
// Delete the user data directory to completely clear all browser data
if (existsSync(userDataDir)) {
console.log('Deleting user data directory:', userDataDir)
rmSync(userDataDir, { recursive: true, force: true })
console.log('User data directory deleted successfully')
}
return { success: true }
}
return { success: false, error: '不支持的平台' }
} catch (error) {
console.error('Logout error:', error)
return {
success: false,
error: error instanceof Error ? error.message : '退出登录失败'
}
}
})
createFloatingWindow()
registerGlobalShortcuts()