diff --git a/background/db/index.js b/background/db/index.js index b2c3cf3..fdfa7b6 100644 --- a/background/db/index.js +++ b/background/db/index.js @@ -1,4 +1,4 @@ -const { getSystemDbPath, getUserDbPath } = require('./path.js'); +const { getSystemDbPath, getUserDbPath } = require('./path.js'); // 添加路径函数导入 const { systemSchema, userSchema, defaultData } = require('./schema.js'); const { openDatabase, batchInsert } = require('./utils.js'); const bcrypt = require('bcryptjs'); diff --git a/background/db/path.js b/background/db/path.js index 85d6c18..af9b47b 100644 --- a/background/db/path.js +++ b/background/db/path.js @@ -1,4 +1,4 @@ -const fs = require('fs'); +const fs = require('fs'); // 添加缺失的fs模块导入 const path = require('path'); const { app } = require('electron'); @@ -13,7 +13,8 @@ let portableFlagPath; // 确保app已经初始化后再获取路径 if (app && app.getPath) { try { - const exePath = app.getPath('exe'); + // 在Windows上,使用process.execPath获取可执行文件路径 + const exePath = process.execPath; appDir = path.dirname(exePath); portableFlagPath = path.join(appDir, 'portable.txt'); @@ -30,6 +31,7 @@ if (app && app.getPath) { appDir = process.cwd(); } +// 在第35行左右,更新数据目录选择逻辑 // 根据模式选择数据目录 let dataDir; if (isDev) { @@ -43,12 +45,32 @@ if (isDev) { dataDir = path.join(path.dirname(appDir), 'data'); } +// 增强路径处理:添加备选路径检测 +if (!isDev && !fs.existsSync(dataDir) && fs.existsSync(path.join(appDir, 'data'))) { + console.log('检测到应用目录下存在data目录,自动切换到便携模式'); + dataDir = path.join(appDir, 'data'); + isPortable = true; +} + // 确保数据目录存在 if (!fs.existsSync(dataDir)) { - fs.mkdirSync(dataDir, { recursive: true }); - console.log(`创建数据目录: ${dataDir}`); + try { + fs.mkdirSync(dataDir, { recursive: true }); + console.log(`创建数据目录: ${dataDir}`); + } catch (mkdirError) { + console.error(`创建数据目录失败: ${dataDir}`, mkdirError); + // 作为最后的备选方案,使用当前工作目录 + dataDir = process.cwd(); + console.log(`备选数据目录: ${dataDir}`); + } } +// 添加更多调试日志 +console.log(`当前运行模式: ${isDev ? '开发环境' : (isPortable ? '便携模式' : '安装模式')}`); +console.log(`数据目录路径: ${dataDir}`); +console.log(`系统数据库路径: ${path.join(dataDir, 'system.db')}`); +console.log(`用户数据库路径: ${path.join(dataDir, 'user.db')}`); + // 系统数据库路径 function getSystemDbPath() { return path.join(dataDir, 'system.db');