diff --git a/electron/db/migration.js b/electron/db/migration.js index 1a54bef..44720fc 100644 --- a/electron/db/migration.js +++ b/electron/db/migration.js @@ -12,11 +12,12 @@ export async function migrateDatabases() { } try { - // 确保数据目录存在 + // 获取数据库路径 const systemDbPath = getSystemDbPath(); const userDbPath = getUserDbPath(); const dataDir = path.dirname(systemDbPath); + // 确保数据目录存在 if (!fs.existsSync(dataDir)) { fs.mkdirSync(dataDir, { recursive: true }); console.log(`创建数据目录: ${dataDir}`); @@ -27,7 +28,7 @@ export async function migrateDatabases() { const installedSystemDbPath = path.join(appPath, 'data', 'system.db'); const installedUserDbPath = path.join(appPath, 'data', 'user.db'); - // 检查并复制系统数据库文件 + // 检查并复制系统数据库文件(仅当目标文件不存在时) if (fs.existsSync(installedSystemDbPath) && !fs.existsSync(systemDbPath)) { fs.copyFileSync(installedSystemDbPath, systemDbPath); console.log(`复制系统数据库: ${installedSystemDbPath} -> ${systemDbPath}`); @@ -37,7 +38,7 @@ export async function migrateDatabases() { console.log(`系统数据库不存在: ${systemDbPath}`); } - // 检查并复制用户数据库文件 + // 检查并复制用户数据库文件(仅当目标文件不存在时) if (fs.existsSync(installedUserDbPath) && !fs.existsSync(userDbPath)) { fs.copyFileSync(installedUserDbPath, userDbPath); console.log(`复制用户数据库: ${installedUserDbPath} -> ${userDbPath}`); diff --git a/electron/db/path.js b/electron/db/path.js index cd5d1b0..fdab367 100644 --- a/electron/db/path.js +++ b/electron/db/path.js @@ -1,9 +1,35 @@ import { app } from 'electron'; import path from 'path'; +import fs from 'fs'; -// 获取用户数据目录 -const userDataPath = app.getPath('userData'); -const dataDir = path.join(userDataPath, 'data'); +// 检测是否为便携模式 +let isPortable = false; +const exePath = app.getPath('exe'); +const appDir = path.dirname(exePath); +const portableFlagPath = path.join(appDir, 'portable.txt'); + +// 如果应用目录中存在portable.txt文件,则启用便携模式 +if (fs.existsSync(portableFlagPath)) { + isPortable = true; + console.log('启用便携模式'); +} + +// 根据模式选择数据目录 +let dataDir; +if (isPortable) { + // 便携模式:数据存储在应用目录下的data文件夹 + dataDir = path.join(appDir, 'data'); +} else { + // 非便携模式:数据存储在用户数据目录 + const userDataPath = app.getPath('userData'); + dataDir = path.join(userDataPath, 'data'); +} + +// 确保数据目录存在 +if (!fs.existsSync(dataDir)) { + fs.mkdirSync(dataDir, { recursive: true }); + console.log(`创建数据目录: ${dataDir}`); +} // 系统数据库路径 export function getSystemDbPath() { diff --git a/electron/service/fileService.js b/electron/service/fileService.js index c604d2f..4b7deda 100644 --- a/electron/service/fileService.js +++ b/electron/service/fileService.js @@ -360,14 +360,38 @@ function getAppSaveDir() { // 开发环境:使用项目根目录 return path.join(process.cwd(), 'output'); } else { - // 生产环境:根据操作系统选择不同路径 - if (process.platform === 'darwin') { // macOS - // 使用用户文档目录 - return path.join(app.getPath('documents'), 'ExamApp'); - } else if (process.platform === 'win32') { // Windows - return path.dirname(app.getPath('exe')); - } else { // Linux 等其他系统 - return app.getPath('userData'); + // 检测是否为便携模式 + const exePath = app.getPath('exe'); + const appDir = path.dirname(exePath); + const portableFlagPath = path.join(appDir, 'portable.txt'); + const isPortable = fs.existsSync(portableFlagPath); + + if (isPortable) { + // 便携模式:使用应用目录下的output文件夹 + const outputDir = path.join(appDir, 'output'); + if (!fs.existsSync(outputDir)) { + fs.mkdirSync(outputDir, { recursive: true }); + } + return outputDir; + } else { + // 非便携模式:根据操作系统选择不同路径 + if (process.platform === 'darwin') { // macOS + // 使用用户文档目录 + const docDir = path.join(app.getPath('documents'), 'ExamApp'); + if (!fs.existsSync(docDir)) { + fs.mkdirSync(docDir, { recursive: true }); + } + return docDir; + } else if (process.platform === 'win32') { // Windows + // 使用用户文档目录 + const docDir = path.join(app.getPath('documents'), 'ExamApp'); + if (!fs.existsSync(docDir)) { + fs.mkdirSync(docDir, { recursive: true }); + } + return docDir; + } else { // Linux 等其他系统 + return app.getPath('userData'); + } } } }