diff --git a/package-windows-portable.js b/package-windows-portable.js new file mode 100644 index 0000000..8260c78 --- /dev/null +++ b/package-windows-portable.js @@ -0,0 +1,87 @@ +const { execSync } = require('child_process'); +const fs = require('fs'); +const path = require('path'); + +// 创建一个兼容Node.js 12的删除文件夹函数 +function deleteFolderRecursive(dir) { + if (fs.existsSync(dir)) { + const files = fs.readdirSync(dir); + for (let i = 0; i < files.length; i++) { + const file = files[i]; + const curPath = path.join(dir, file); + if (fs.lstatSync(curPath).isDirectory()) { + // 递归删除子文件夹 + deleteFolderRecursive(curPath); + } else { + // 删除文件 + fs.unlinkSync(curPath); + } + } + // 删除空文件夹 + fs.rmdirSync(dir); + } +} + +// 创建打包前的准备工作 +function prepareForBuild() { + console.log('开始准备Windows 7便携应用打包...'); + + try { + // 清理之前的构建文件 - 使用兼容Node.js 12的方法 + const buildDir = path.join(__dirname, 'dist_electron'); + if (fs.existsSync(buildDir)) { + console.log('清理之前的构建文件...'); + deleteFolderRecursive(buildDir); + } + + // 确保data目录存在(便携应用需要) + const dataDir = path.join(__dirname, 'data'); + if (!fs.existsSync(dataDir)) { + fs.mkdirSync(dataDir); + fs.writeFileSync(path.join(dataDir, '.gitignore'), '*\n!.gitignore'); + console.log('创建data目录完成'); + } + + console.log('准备工作完成'); + } catch (error) { + console.error('准备工作失败:', error); + process.exit(1); + } +} + +// 执行打包命令 +function buildPortableApp() { + console.log('开始构建Windows 7便携应用...'); + + try { + // 设置构建环境变量 + process.env.WIN_CSC_LINK = ''; + process.env.WIN_CSC_KEY_PASSWORD = ''; + + // 执行构建命令 - 特别针对Windows 7优化,兼容Electron 11 + execSync( + 'npm run electron:build -- --win portable --ia32 --x64 --no-compress --publish never', + { stdio: 'inherit' } + ); + + console.log('Windows 7便携应用构建完成!'); + console.log('构建产物位于 dist_electron 目录'); + } catch (error) { + console.error('构建失败:', error); + process.exit(1); + } +} + +// 主函数 +function main() { + console.log('==== Windows 7便携应用打包工具 ===='); + console.log('Node.js版本:', process.version); + console.log('Electron版本: 11.5.0'); + console.log('Vue版本: 2.6.11'); + + prepareForBuild(); + buildPortableApp(); +} + +// 执行主函数 +main(); \ No newline at end of file diff --git a/package.json b/package.json index d510f9d..7aedb49 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "start": "vue-cli-service electron:serve", "serve": "vue-cli-service serve", "build": "vue-cli-service build", + "build:win7-portable": "node package-windows-portable.js", "electron:build": "vue-cli-service electron:build", "electron:serve": "vue-cli-service electron:serve", "postinstall": "electron-builder install-app-deps", diff --git a/src/background/main.js b/src/background/main.js index fff3e7f..72fbd83 100644 --- a/src/background/main.js +++ b/src/background/main.js @@ -7,6 +7,7 @@ const bcrypt = require('bcryptjs') const isDevelopment = process.env.NODE_ENV !== 'production' // 导入fs模块用于文件操作 const fs = require('fs') +const path = require('path') // 导入数据库相关函数 const { checkDatabaseInitialized, initializeDatabase, initializeUserDatabase } = require('./db/index.js'); @@ -280,3 +281,43 @@ ipcMain.handle('initializeUserDatabaseSilently', async () => { return { success: false, error: error.message } } }) + + +// 检测是否为便携模式运行 +let isPortableMode = false +let appDataPath = '' + +// 初始化应用路径 - 兼容Node.js 12 +function initializeAppPaths() { + // 检查应用根目录是否存在data文件夹,如果存在则认为是便携模式 + const portableDataPath = path.join(process.cwd(), 'data') + const defaultDataPath = path.join(app.getPath('userData'), 'data') + + // 检查是否为便携模式 + if (fs.existsSync(portableDataPath)) { + isPortableMode = true + appDataPath = portableDataPath + console.log('检测到便携模式,使用当前目录的data文件夹:', appDataPath) + } else { + isPortableMode = false + appDataPath = defaultDataPath + console.log('使用默认数据目录:', appDataPath) + + // 确保默认数据目录存在 + if (!fs.existsSync(appDataPath)) { + fs.mkdirSync(appDataPath, { recursive: true }) + } + } +} + +// 添加全局函数获取数据目录 +global.getAppDataPath = function() { + return appDataPath +} + +global.isPortableMode = function() { + return isPortableMode +} + +// 在应用ready事件前初始化路径 +initializeAppPaths() diff --git a/vue.config.js b/vue.config.js index aa32a77..5030dca 100644 --- a/vue.config.js +++ b/vue.config.js @@ -32,7 +32,7 @@ module.exports = { }) .end() - // 添加对mjs文件的处理 + // 添加对mjs文件的处理 - 兼容Electron 11 config.module .rule('mjs') .test(/\.mjs$/) @@ -40,6 +40,37 @@ module.exports = { .add(/node_modules/) .end() .type('javascript/auto') + }, + // Windows 7便携应用配置 - 兼容Electron 11 + win: { + target: [ + { + target: 'portable', + arch: ['ia32', 'x64'] + } + ], + icon: 'public/favicon.ico', + // 关闭压缩以提高在Windows 7上的兼容性 + compression: 'store', + // 针对Windows 7的特殊配置 + extraResources: [ + { + from: 'src/background/font', + to: 'font', + filter: ['**/*'] + }, + { + from: 'data', + to: 'data', + filter: ['.gitignore'] + } + ] + }, + // 便携应用配置 - 兼容Electron 11 + portable: { + artifactName: '统计技能考试系统_便携版_${version}_${arch}.exe', + // 启用便携模式标志 + target: 'portable' } } }