From 09fe18856a3cb8df6aca51467e65f3776e09f600 Mon Sep 17 00:00:00 2001 From: chenqiang Date: Wed, 10 Sep 2025 12:18:40 +0800 Subject: [PATCH] path --- background/db/path.js | 92 ++++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 59 deletions(-) diff --git a/background/db/path.js b/background/db/path.js index 96237b7..0f49b2b 100644 --- a/background/db/path.js +++ b/background/db/path.js @@ -14,75 +14,50 @@ if (isDev) { dataDir = path.join(process.cwd(), 'data'); } else { try { - // 关键修改:对于打包后的应用,尤其是便携模式,我们需要更智能地确定数据目录位置 - // 1. 获取进程可执行文件路径 - const execPath = process.execPath; - // 2. 获取可执行文件所在目录 - let appDir = path.dirname(execPath); + // 关键修改:使用process.argv[0]获取实际启动路径 + const exePath = process.argv[0]; + let appDir = path.dirname(exePath); - console.log('进程可执行文件路径:', execPath); - console.log('进程目录:', appDir); + console.log('实际可执行文件路径:', exePath); + console.log('实际应用目录:', appDir); - // 3. 检测是否存在portable.txt文件(标记文件) + // 检测是否存在portable.txt标记文件 const portableFlagPath = path.join(appDir, 'portable.txt'); - if (fs.existsSync(portableFlagPath)) { - isPortable = true; - console.log('检测到便携模式标记文件'); - } + isPortable = fs.existsSync(portableFlagPath); - // 4. 检查是否在临时目录中运行(常见于便携应用解压运行时) + // 如果在临时目录中运行且未找到标记文件 const isTempDir = appDir.includes('Temp') || appDir.includes('tmp'); - console.log('是否在临时目录中运行:', isTempDir); - - // 5. 特殊处理便携模式下的临时目录问题 - if (isTempDir) { - // 尝试通过其他方式获取实际的应用目录 - // - 方法1:检查可执行文件名 - const exeName = path.basename(execPath); - console.log('可执行文件名:', exeName); - - // - 方法2:使用process.argv[0](启动路径) - const startupPath = process.argv[0]; - console.log('启动路径:', startupPath); - - // 关键点:对于便携应用,我们需要确保数据目录与实际的可执行文件(用户看到的那个)在同一目录 - // 假设可执行文件名为StatExamPortable_*.exe,我们需要找到这个文件的实际位置 - // 由于在临时目录中,我们需要向上查找或使用相对路径策略 - - // 这里使用一种通用策略:在Windows系统上,便携应用的临时目录通常在用户临时文件夹下 - // 我们将数据目录放在与临时目录不同的位置,确保数据持久性 - - // 尝试获取用户主目录 - const userHome = app.getPath('home'); - console.log('用户主目录:', userHome); - - // 便携式应用的数据目录应该放在一个稳定的位置,而不是临时目录 - // 这里我们选择放在用户主目录下的特定文件夹 - const portableDataDir = path.join(userHome, 'StatExamData'); - dataDir = portableDataDir; - - // 创建便携模式标记文件 - const stablePortableFlagPath = path.join(portableDataDir, '..', 'portable.txt'); - if (!fs.existsSync(stablePortableFlagPath)) { - try { - fs.writeFileSync(stablePortableFlagPath, ''); - console.log('创建便携模式标记文件:', stablePortableFlagPath); - } catch (error) { - console.error('创建便携模式标记文件失败:', error); + if (isTempDir && !isPortable) { + // 尝试通过遍历上级目录寻找portable.txt来确定实际应用目录 + let currentDir = appDir; + // 最多向上查找5级目录 + for (let i = 0; i < 5; i++) { + currentDir = path.dirname(currentDir); + const checkFlagPath = path.join(currentDir, 'portable.txt'); + if (fs.existsSync(checkFlagPath)) { + appDir = currentDir; + isPortable = true; + console.log('在上级目录找到便携模式标记:', appDir); + break; } } - - } else { - // 正常情况下,数据目录与可执行文件同级 - dataDir = path.join(appDir, 'data'); } + // 设置数据目录为应用目录下的data文件夹 + dataDir = path.join(appDir, 'data'); + console.log('最终数据目录:', dataDir); + + // 确保便携模式标记文件存在 + if (isPortable) { + const flagPath = path.join(appDir, 'portable.txt'); + if (!fs.existsSync(flagPath)) { + fs.writeFileSync(flagPath, ''); + } + } } catch (error) { console.error('确定数据目录时出错:', error); - // 发生错误时使用默认策略 - const fallbackDir = path.join(app.getPath('userData'), 'data'); - dataDir = fallbackDir; - console.log('使用备用数据目录:', fallbackDir); + // 出错时使用应用数据目录作为后备 + dataDir = path.join(app.getPath('userData'), 'data'); } } @@ -102,7 +77,6 @@ function getUserDbPath() { return path.join(dataDir, 'user.db'); } -// 导出函数供其他模块使用 module.exports = { getSystemDbPath, getUserDbPath