main.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. const { app, BrowserWindow, ipcMain, net, shell, dialog } = require('electron');
  2. const path = require('path');
  3. const url = require('url');
  4. const fs = require('fs');
  5. const { spawn } = require('child_process'); // 用于启动子进程
  6. const sevenBin = require('7zip-bin');
  7. let iconv = null; // 用来在 Windows 上正确解码 7za 使用系统编码输出的中文信息
  8. try {
  9. iconv = require('iconv-lite');
  10. } catch (e) {
  11. iconv = null;
  12. }
  13. let win = null;
  14. // 创建窗口
  15. const createWindow = () => {
  16. win = new BrowserWindow({
  17. // width: 1200,
  18. // height: 800,
  19. show: false, // 是否显示窗口
  20. autoHideMenuBar: true, // 隐藏菜单栏
  21. webPreferences: {
  22. nodeIntegration: true, // 是否集成 Node.js
  23. // enableRemoteModule: true, // 是否启用 remote 模块
  24. webSecurity: false, // 是否禁用同源策略
  25. preload: path.join(__dirname, 'preload.js'), // 预加载脚本
  26. },
  27. });
  28. win.loadURL(
  29. url.format({
  30. pathname: path.join(__dirname, './dist', 'index.html'),
  31. protocol: 'file:',
  32. slashes: true, // true: file://, false: file:
  33. hash: '/login', // /image_change 图片切换页
  34. }),
  35. );
  36. win.once('ready-to-show', () => {
  37. win.maximize();
  38. win.show();
  39. });
  40. // 拦截当前窗口的导航,防止外部链接在应用内打开
  41. win.webContents.on('will-navigate', (event, url) => {
  42. if (url.startsWith('http://') || url.startsWith('https://')) {
  43. event.preventDefault();
  44. shell.openExternal(url);
  45. }
  46. });
  47. };
  48. // 当 Electron 完成初始化并准备创建浏览器窗口时调用此方法
  49. app.whenReady().then(() => {
  50. createWindow();
  51. app.on('activate', () => {
  52. if (BrowserWindow.getAllWindows().length === 0) {
  53. createWindow();
  54. }
  55. });
  56. });
  57. // 安装更新(渲染进程确认安装时调用)
  58. ipcMain.on('install-update', (event, filePath) => {
  59. if (!fs.existsSync(filePath)) {
  60. event.sender.send('update-error', { message: '安装文件不存在' });
  61. return;
  62. }
  63. if (process.platform === 'win32') {
  64. // Windows:直接执行安装程序(根据你的安装包参数添加静默/等待参数)
  65. try {
  66. spawn(filePath, [], { detached: true, stdio: 'ignore' }).unref();
  67. app.quit();
  68. } catch (e) {
  69. event.sender.send('update-error', { message: e.message });
  70. }
  71. } else if (process.platform === 'darwin') {
  72. // macOS:打开 dmg 或者打开 .pkg
  73. shell.openPath(filePath).then(() => app.quit());
  74. } else {
  75. // linux:按照需要实现
  76. shell.openPath(filePath).then(() => app.quit());
  77. }
  78. });
  79. /**
  80. * 使用 7z 压缩文件/目录,支持密码和进度反馈
  81. * @param {Object} opts 压缩选项
  82. * @param {Array<string>} opts.sources 待压缩的文件或目录列表
  83. * @param {string} opts.dest 目标压缩包路径
  84. * @param {string} [opts.format] 压缩格式,'7z' 或 'zip',默认 'zip'
  85. * @param {number} [opts.level] 压缩级别,0-9,默认 9
  86. * @param {boolean} [opts.recurse] 是否递归子目录,默认 true
  87. * @param {string} [opts.password] 压缩密码
  88. * @returns {Promise<{success: boolean, dest: string}>} 压缩结果
  89. */
  90. ipcMain.handle('compress-with-7z', async (evt, opts) => {
  91. const sender = evt.sender; // 用于发送进度消息
  92. const sources = Array.isArray(opts.sources) ? opts.sources : []; // 待压缩的文件或目录列表
  93. if (!sources.length) throw new Error('no sources');
  94. const dest = path.resolve(opts.dest); // 目标压缩包路径
  95. const format = opts.format === 'zip' ? 'zip' : '7z'; // 压缩格式
  96. const level = Number.isInteger(opts.level) ? Math.max(0, Math.min(9, opts.level)) : 9; // 压缩级别
  97. const recurse = opts.recurse !== false; // 是否递归子目录
  98. // 检查源文件/目录是否存在
  99. for (const s of sources) {
  100. if (!fs.existsSync(s)) throw new Error(`source not found: ${s}`);
  101. }
  102. // 7za 可执行路径,优先使用 seven-bin 提供的路径;若被打包进 app.asar,则尝试 app.asar.unpacked 路径
  103. let sevenPath = sevenBin.path7za;
  104. try {
  105. // 如果 sevenPath 在 app.asar 内,映射到 app.asar.unpacked 的对应相对路径
  106. const asarToken = `${path.sep}app.asar${path.sep}`;
  107. if (typeof sevenPath === 'string' && sevenPath.indexOf(asarToken) !== -1) {
  108. const rel = sevenPath.split(asarToken)[1]; // 例如: node_modules/7zip-bin/win/x64/7za.exe
  109. const unpackedCandidate = path.join(process.resourcesPath, 'app.asar.unpacked', rel);
  110. if (fs.existsSync(unpackedCandidate)) {
  111. sevenPath = unpackedCandidate;
  112. }
  113. }
  114. // 若上面没有找到,尝试常见的 unpacked 路径(兼容不同包结构与架构)
  115. if (!fs.existsSync(sevenPath)) {
  116. const candidates = [
  117. path.join(
  118. process.resourcesPath,
  119. 'app.asar.unpacked',
  120. 'node_modules',
  121. '7zip-bin',
  122. 'win',
  123. 'x64',
  124. path.basename(sevenPath),
  125. ),
  126. path.join(
  127. process.resourcesPath,
  128. 'app.asar.unpacked',
  129. 'node_modules',
  130. '7zip-bin',
  131. 'win',
  132. 'x86',
  133. path.basename(sevenPath),
  134. ),
  135. path.join(
  136. process.resourcesPath,
  137. 'app.asar.unpacked',
  138. 'node_modules',
  139. '7zip-bin',
  140. 'bin',
  141. path.basename(sevenPath),
  142. ),
  143. ];
  144. for (const c of candidates) {
  145. if (fs.existsSync(c)) {
  146. sevenPath = c;
  147. break;
  148. }
  149. }
  150. }
  151. } catch (err) {
  152. console.error('Error checking 7za path:', err);
  153. }
  154. if (!fs.existsSync(sevenPath)) {
  155. throw new Error(
  156. `7za executable not found: ${sevenPath}. If running from a packaged app, add "asarUnpack": ["node_modules/7zip-bin/**"] to build config so 7za is unpacked.`,
  157. );
  158. }
  159. const args = ['a', `-t${format}`, `-mx=${level}`, dest]; // 基本参数
  160. if (opts.password) {
  161. args.push(`-p${opts.password}`);
  162. // 仅当使用 7z 格式时才启用头部加密(zip 不支持 -mhe)
  163. if (format === '7z') args.push('-mhe=on'); // 加密文件名/头(仅 7z 支持)
  164. }
  165. if (recurse) args.push('-r'); // 递归
  166. // append sources (支持通配或单个路径)
  167. args.push(...sources);
  168. // 确保目标目录存在(避免 7z 无法写入导致的失败)
  169. try {
  170. fs.mkdirSync(path.dirname(dest), { recursive: true });
  171. } catch (e) {
  172. // 如果无法创建目标目录,尽早报错
  173. throw new Error(`failed to create dest directory: ${e.message}`);
  174. }
  175. return await new Promise((resolve, reject) => {
  176. let stdoutAll = '';
  177. let stderrAll = '';
  178. const child = spawn(sevenPath, args, { windowsHide: true });
  179. // 收集并转发 stdout/stderr,用作进度显示或日志
  180. child.stdout.on('data', (chunk) => {
  181. const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk));
  182. let s = '';
  183. // 7za 在 Windows 上输出常用 OEM/GBK 编码,优先用 iconv-lite 解码
  184. if (process.platform === 'win32' && iconv) {
  185. try {
  186. s = iconv.decode(buf, 'cp936');
  187. } catch (e) {
  188. s = buf.toString('utf8');
  189. }
  190. } else {
  191. s = buf.toString('utf8');
  192. }
  193. stdoutAll += s;
  194. sender.send('compress-progress', s);
  195. });
  196. child.stderr.on('data', (chunk) => {
  197. const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk));
  198. let s = '';
  199. if (process.platform === 'win32' && iconv) {
  200. try {
  201. s = iconv.decode(buf, 'cp936');
  202. } catch (e) {
  203. s = buf.toString('utf8');
  204. }
  205. } else {
  206. s = buf.toString('utf8');
  207. }
  208. stderrAll += s;
  209. sender.send('compress-stderr', s);
  210. });
  211. child.on('error', (err) => reject(err));
  212. child.on('close', (code) => {
  213. if (code === 0) {
  214. resolve({ success: true, dest });
  215. } else {
  216. // 包含 stdout/stderr 与命令信息以便排查
  217. const errMsg = `7z exited with code ${code}\ncommand: ${JSON.stringify({ sevenPath, args })}\nstdout:\n${stdoutAll}\nstderr:\n${stderrAll}`;
  218. const e = new Error(errMsg);
  219. e.code = code;
  220. e.stdout = stdoutAll;
  221. e.stderr = stderrAll;
  222. e.cmd = { sevenPath, args };
  223. return reject(e);
  224. }
  225. });
  226. });
  227. });
  228. /**
  229. * 下载文件
  230. * @param {string} url 文件 URL
  231. * @param {string} destPath 保存路径
  232. */
  233. ipcMain.handle('download-file', async (evt, { url, destPath }) => {
  234. return new Promise((resolve, reject) => {
  235. const fileStream = fs.createWriteStream(destPath); // 创建写入流
  236. const req = net.request(url);
  237. req.on('response', (res) => {
  238. res.on('data', (chunk) => {
  239. fileStream.write(chunk);
  240. });
  241. res.on('end', () => {
  242. fileStream.end();
  243. resolve(destPath);
  244. });
  245. });
  246. req.on('error', (err) => {
  247. reject(err);
  248. });
  249. req.end();
  250. });
  251. });
  252. /**
  253. * 打开文件对话框
  254. * @param {Object} opts 对话框选项
  255. * @param {string} [opts.title] 对话框标题
  256. * @param {Array<string>} [opts.properties] 对话框属性数组,默认 ['openDirectory']
  257. * @return {Promise<{canceled: boolean, filePaths: string[]}>} 文件路径 canceled 表示是否取消选择 filePaths 表示选择的文件路径数组
  258. */
  259. ipcMain.handle('dialog:openFiles', async (event, options = {}) => {
  260. const result = await dialog.showOpenDialog({
  261. title: options.title || '选择文件夹',
  262. properties: options.properties || ['openDirectory'],
  263. });
  264. return { canceled: result.canceled, filePaths: result.filePaths };
  265. });
  266. // 当所有窗口都已关闭时退出
  267. app.on('window-all-closed', () => {
  268. if (process.platform !== 'darwin') {
  269. app.quit();
  270. }
  271. });
  272. app.on('activate', () => {
  273. if (BrowserWindow.getAllWindows().length === 0) {
  274. createWindow();
  275. }
  276. });