diff --git a/background/db/path.js b/background/db/path.js index b25293a..85d6c18 100644 --- a/background/db/path.js +++ b/background/db/path.js @@ -1,5 +1,5 @@ const fs = require('fs'); -const path = require('path'); // 添加缺失的path模块导入 +const path = require('path'); const { app } = require('electron'); // 判断是否为开发环境 diff --git a/background/main.js b/background/main.js index d9183b6..ab077e9 100644 --- a/background/main.js +++ b/background/main.js @@ -41,28 +41,57 @@ protocol.registerSchemesAsPrivileged([ let mainWindow = null; async function createWindow() { - // Create the browser window. + console.log('开始创建应用窗口...'); + + // 创建浏览器窗口 mainWindow = new BrowserWindow({ - width: 800, // 默认宽度(实际会被最大化覆盖) - height: 600, // 默认高度(实际会被最大化覆盖) - show: false, // 先隐藏窗口,避免闪烁 + width: 800, + height: 600, + // 确保在Windows 7上能正常显示 + show: false, webPreferences: { - // 改为使用绝对路径解析 - preload: require("path").join(process.cwd(), "src/preload.js"), + // 使用Node.js 12兼容的路径解析方式 + preload: path.join(__dirname, '../src/preload.js'), nodeIntegration: false, contextIsolation: true, - }, + } }); // 设置隐藏菜单栏 mainWindow.setMenu(null); - // 在窗口显示前设置最大化 - mainWindow.maximize(); - // 然后显示窗口 - mainWindow.show(); + // 添加窗口准备好显示时的事件 + mainWindow.once('ready-to-show', () => { + console.log('窗口准备就绪,开始显示...'); + try { + // 在Windows 7上使用try-catch确保最大化操作不失败 + try { + mainWindow.maximize(); + console.log('窗口已最大化'); + } catch (e) { + console.error('窗口最大化失败:', e); + } + // 显示窗口 + mainWindow.show(); + console.log('窗口已显示'); + } catch (error) { + console.error('显示窗口时出错:', error); + // 作为备用方案,再次尝试显示窗口 + mainWindow.show(); + } + }); - // 添加窗口关闭事件监听 + // 添加窗口显示事件 + mainWindow.on('show', () => { + console.log('窗口已成功显示'); + }); + + // 添加webContents加载完成事件 + mainWindow.webContents.on('did-finish-load', () => { + console.log('页面加载完成'); + }); + + // 添加窗口关闭事件监听 - 保持原有逻辑 mainWindow.on("close", (event) => { console.log("检测到窗口关闭事件"); @@ -104,13 +133,14 @@ async function createWindow() { } }); + // 加载应用内容 if (process.env.WEBPACK_DEV_SERVER_URL) { - // Load the url of the dev server if in development mode + // 开发模式加载开发服务器URL await mainWindow.loadURL(process.env.WEBPACK_DEV_SERVER_URL); if (!process.env.IS_TEST) mainWindow.webContents.openDevTools(); } else { createProtocol("app"); - // Load the index.html when not in development + // 生产模式加载本地HTML文件 mainWindow.loadURL("app://./index.html"); } } @@ -212,54 +242,7 @@ app.on("ready", async () => { console.error("数据库初始化失败:", error); } - // 添加数据库初始化相关的IPC处理 - ipcMain.handle('initialize-database', async (event) => { - try { - console.log('收到初始化数据库请求'); - await initializeDatabase(); - console.log('数据库初始化成功'); - return { success: true }; - } catch (error) { - console.error('数据库初始化失败:', error); - return { success: false, error: error.message }; - } - }); - - ipcMain.handle('check-database-initialized', async (event) => { - try { - return await checkDatabaseInitialized(); - } catch (error) { - console.error('检查数据库初始化状态失败:', error); - return false; - } - }); - - // 检查user.db是否存在的IPC处理 - ipcMain.handle('checkUserDbExists', async (event) => { - try { - const userDbPath = getUserDbPath(); - const exists = fs.existsSync(userDbPath); - console.log(`用户数据库检查结果: ${exists ? '存在' : '不存在'}`); - return exists; - } catch (error) { - console.error('检查用户数据库失败:', error); - return false; - } - }); - - // 静默初始化用户数据库的IPC处理 - ipcMain.handle('initializeUserDatabaseSilently', async (event) => { - try { - await initializeUserDatabase(); - console.log('用户数据库静默初始化成功'); - return { success: true }; - } catch (error) { - console.error('用户数据库静默初始化失败:', error); - return { success: false, error: error.message }; - } - }); - - // 检测是否为便携模式运行 + // 创建窗口 createWindow(); }); @@ -291,48 +274,8 @@ ipcMain.handle("hashTest", async (event, inputString) => { } }); -// 数据库相关IPC接口 -ipcMain.handle("check-database-initialized", async () => { - try { - return await checkDatabaseInitialized(); - } catch (error) { - console.error("Failed to check database initialization:", error); - return false; - } -}); - -ipcMain.handle("initialize-database", async () => { - try { - return await initializeDatabase(); - } catch (error) { - console.error("Failed to initialize database:", error); - return false; - } -}); - -// 检查user.db是否存在 -ipcMain.handle("checkUserDbExists", async () => { - try { - const userDbPath = getUserDbPath(); - return fs.existsSync(userDbPath); - } catch (error) { - console.error("检查user.db文件是否存在失败:", error); - return false; - } -}); - -// 静默初始化用户数据库 -ipcMain.handle("initializeUserDatabaseSilently", async () => { - try { - console.log("开始静默初始化用户数据库..."); - const result = await initializeUserDatabase(); - console.log("静默初始化用户数据库完成:", result); - return { success: true, result }; - } catch (error) { - console.error("静默初始化用户数据库失败:", error); - return { success: false, error: error.message }; - } -}); +// 移除所有重复的IPC处理器 +// 这些处理器应该已经在各个service的init函数中被注册了 // 检测是否为便携模式运行 let isPortableMode = false;