main.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. shell.openPath(filePath).then(() => app.quit());
  76. }
  77. });
  78. /**
  79. * 使用 7z 压缩文件/目录,支持密码和进度反馈
  80. * @param {Object} opts 压缩选项
  81. * @param {Array<string>} opts.sources 待压缩的文件或目录列表
  82. * @param {string} opts.dest 目标压缩包路径
  83. * @param {string} [opts.format] 压缩格式,'7z' 或 'zip',默认 'zip'
  84. * @param {number} [opts.level] 压缩级别,0-9,默认 9
  85. * @param {boolean} [opts.recurse] 是否递归子目录,默认 true
  86. * @param {string} [opts.password] 压缩密码
  87. * @returns {Promise<{success: boolean, dest: string}>} 压缩结果
  88. */
  89. ipcMain.handle('compress-with-7z', async (evt, opts) => {
  90. const sender = evt.sender; // 用于发送进度消息
  91. const sources = Array.isArray(opts.sources) ? opts.sources : []; // 待压缩的文件或目录列表
  92. if (!sources.length) throw new Error('no sources');
  93. const dest = path.resolve(opts.dest); // 目标压缩包路径
  94. const format = opts.format === 'zip' ? 'zip' : '7z'; // 压缩格式
  95. const level = Number.isInteger(opts.level) ? Math.max(0, Math.min(9, opts.level)) : 9; // 压缩级别
  96. const recurse = opts.recurse !== false; // 是否递归子目录
  97. // 检查源文件/目录是否存在
  98. for (const s of sources) {
  99. if (!fs.existsSync(s)) throw new Error(`source not found: ${s}`);
  100. }
  101. // 7za 可执行路径,优先使用 seven-bin 提供的路径;若被打包进 app.asar,则尝试 app.asar.unpacked 路径
  102. let sevenPath = sevenBin.path7za;
  103. try {
  104. // 如果 sevenPath 在 app.asar 内,映射到 app.asar.unpacked 的对应相对路径
  105. const asarToken = `${path.sep}app.asar${path.sep}`;
  106. if (typeof sevenPath === 'string' && sevenPath.indexOf(asarToken) !== -1) {
  107. const rel = sevenPath.split(asarToken)[1]; // 例如: node_modules/7zip-bin/win/x64/7za.exe
  108. const unpackedCandidate = path.join(process.resourcesPath, 'app.asar.unpacked', rel);
  109. if (fs.existsSync(unpackedCandidate)) {
  110. sevenPath = unpackedCandidate;
  111. }
  112. }
  113. // 若上面没有找到,尝试常见的 unpacked 路径(兼容不同包结构与架构)
  114. if (!fs.existsSync(sevenPath)) {
  115. const candidates = [
  116. path.join(
  117. process.resourcesPath,
  118. 'app.asar.unpacked',
  119. 'node_modules',
  120. '7zip-bin',
  121. 'win',
  122. 'x64',
  123. path.basename(sevenPath),
  124. ),
  125. path.join(
  126. process.resourcesPath,
  127. 'app.asar.unpacked',
  128. 'node_modules',
  129. '7zip-bin',
  130. 'win',
  131. 'x86',
  132. path.basename(sevenPath),
  133. ),
  134. path.join(
  135. process.resourcesPath,
  136. 'app.asar.unpacked',
  137. 'node_modules',
  138. '7zip-bin',
  139. 'bin',
  140. path.basename(sevenPath),
  141. ),
  142. ];
  143. for (const c of candidates) {
  144. if (fs.existsSync(c)) {
  145. sevenPath = c;
  146. break;
  147. }
  148. }
  149. }
  150. } catch (err) {
  151. console.error('Error checking 7za path:', err);
  152. }
  153. if (!fs.existsSync(sevenPath)) {
  154. throw new Error(
  155. `7za executable not found: ${sevenPath}. If running from a packaged app, add "asarUnpack": ["node_modules/7zip-bin/**"] to build config so 7za is unpacked.`,
  156. );
  157. }
  158. const args = ['a', `-t${format}`, `-mx=${level}`, dest]; // 基本参数
  159. if (opts.password) {
  160. args.push(`-p${opts.password}`);
  161. // 仅当使用 7z 格式时才启用头部加密(zip 不支持 -mhe)
  162. if (format === '7z') args.push('-mhe=on'); // 加密文件名/头(仅 7z 支持)
  163. }
  164. if (recurse) args.push('-r'); // 递归
  165. // append sources (支持通配或单个路径)
  166. args.push(...sources);
  167. // 确保目标目录存在(避免 7z 无法写入导致的失败)
  168. try {
  169. fs.mkdirSync(path.dirname(dest), { recursive: true });
  170. } catch (e) {
  171. // 如果无法创建目标目录,尽早报错
  172. throw new Error(`failed to create dest directory: ${e.message}`);
  173. }
  174. return await new Promise((resolve, reject) => {
  175. let stdoutAll = '';
  176. let stderrAll = '';
  177. const child = spawn(sevenPath, args, { windowsHide: true });
  178. // 收集并转发 stdout/stderr,用作进度显示或日志
  179. child.stdout.on('data', (chunk) => {
  180. const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk));
  181. let s = '';
  182. // 7za 在 Windows 上输出常用 OEM/GBK 编码,优先用 iconv-lite 解码
  183. if (process.platform === 'win32' && iconv) {
  184. try {
  185. s = iconv.decode(buf, 'cp936');
  186. } catch (e) {
  187. s = buf.toString('utf8');
  188. }
  189. } else {
  190. s = buf.toString('utf8');
  191. }
  192. stdoutAll += s;
  193. sender.send('compress-progress', s);
  194. });
  195. child.stderr.on('data', (chunk) => {
  196. const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk));
  197. let s = '';
  198. if (process.platform === 'win32' && iconv) {
  199. try {
  200. s = iconv.decode(buf, 'cp936');
  201. } catch (e) {
  202. s = buf.toString('utf8');
  203. }
  204. } else {
  205. s = buf.toString('utf8');
  206. }
  207. stderrAll += s;
  208. sender.send('compress-stderr', s);
  209. });
  210. child.on('error', (err) => reject(err));
  211. child.on('close', (code) => {
  212. if (code === 0) {
  213. resolve({ success: true, dest });
  214. } else {
  215. // 包含 stdout/stderr 与命令信息以便排查
  216. const errMsg = `7z exited with code ${code}\ncommand: ${JSON.stringify({ sevenPath, args })}\nstdout:\n${stdoutAll}\nstderr:\n${stderrAll}`;
  217. const e = new Error(errMsg);
  218. e.code = code;
  219. e.stdout = stdoutAll;
  220. e.stderr = stderrAll;
  221. e.cmd = { sevenPath, args };
  222. return reject(e);
  223. }
  224. });
  225. });
  226. });
  227. /**
  228. * 下载文件
  229. * @param {string} url 文件 URL
  230. * @param {string} destPath 保存路径
  231. */
  232. ipcMain.handle('download-file', async (evt, { url, destPath }) => {
  233. return new Promise((resolve, reject) => {
  234. const fileStream = fs.createWriteStream(destPath); // 创建写入流
  235. const req = net.request(url);
  236. req.on('response', (res) => {
  237. const contentLength = Number(res.headers['content-length'] || '0');
  238. let bytesReceived = 0;
  239. res.on('data', (chunk) => {
  240. fileStream.write(chunk);
  241. bytesReceived += chunk.length;
  242. // 在主进程计算百分比并发送
  243. const percentage = contentLength > 0 ? Math.round((bytesReceived / contentLength) * 100) : 0;
  244. evt.sender.send('download-progress', {
  245. bytesReceived, // 已接收字节数
  246. contentLength, // 总字节数
  247. percentage, // 百分比
  248. });
  249. });
  250. res.on('end', () => {
  251. fileStream.end(() => {
  252. // 结束时确保发送 100%(若已知总长度)
  253. const finalPercentage = contentLength > 0 ? 100 : 0;
  254. evt.sender.send('download-progress', {
  255. url,
  256. bytesReceived,
  257. contentLength,
  258. percentage: finalPercentage,
  259. });
  260. resolve(destPath);
  261. });
  262. });
  263. res.on('aborted', () => {
  264. reject(new Error('response aborted'));
  265. });
  266. });
  267. req.on('error', (err) => {
  268. reject(err);
  269. });
  270. fileStream.on('error', (err) => {
  271. reject(err);
  272. });
  273. req.end();
  274. });
  275. });
  276. /**
  277. * 打开文件对话框
  278. * @param {Object} opts 对话框选项
  279. * @param {string} [opts.title] 对话框标题
  280. * @param {Array<string>} [opts.properties] 对话框属性数组,默认 ['openDirectory']
  281. * @return {Promise<{canceled: boolean, filePaths: string[]}>} 文件路径 canceled 表示是否取消选择 filePaths 表示选择的文件路径数组
  282. */
  283. ipcMain.handle('dialog:openFiles', async (event, options = {}) => {
  284. const result = await dialog.showOpenDialog({
  285. title: options.title || '选择文件夹',
  286. properties: options.properties || ['openDirectory'],
  287. });
  288. return { canceled: result.canceled, filePaths: result.filePaths };
  289. });
  290. // 当所有窗口都已关闭时退出
  291. app.on('window-all-closed', () => {
  292. if (process.platform !== 'darwin') {
  293. app.quit();
  294. }
  295. });
  296. app.on('activate', () => {
  297. if (BrowserWindow.getAllWindows().length === 0) {
  298. createWindow();
  299. }
  300. });