main.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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.handle('check-update', async () => {
  59. const apiUrl = 'https://your-api.com/api/app/latest'; // <-- 替换为真实接口
  60. const currentVersion = app.getVersion();
  61. return new Promise((resolve, reject) => {
  62. const req = net.request(apiUrl);
  63. let body = '';
  64. req.on('response', (res) => {
  65. res.on('data', (chunk) => (body += chunk));
  66. res.on('end', () => {
  67. try {
  68. const latest = JSON.parse(body);
  69. const update = latest.version && latest.version !== currentVersion;
  70. resolve({ update, latest, currentVersion });
  71. } catch (e) {
  72. reject(e);
  73. }
  74. });
  75. });
  76. req.on('error', (err) => reject(err));
  77. req.end();
  78. });
  79. });
  80. // 下载更新(渲染进程发起),主进程负责流式保存并推送进度
  81. ipcMain.on('download-update', (event, downloadUrl) => {
  82. const win = BrowserWindow.getAllWindows()[0];
  83. const filename = path.basename(downloadUrl).split('?')[0] || `update-${Date.now()}.exe`;
  84. const tmpPath = path.join(app.getPath('temp'), filename);
  85. const fileStream = fs.createWriteStream(tmpPath);
  86. const req = net.request(downloadUrl);
  87. let received = 0;
  88. let total = 0;
  89. req.on('response', (res) => {
  90. total = parseInt(res.headers['content-length'] || res.headers['Content-Length'] || '0');
  91. res.on('data', (chunk) => {
  92. received += chunk.length;
  93. fileStream.write(chunk);
  94. win.webContents.send('update-download-progress', { received, total });
  95. });
  96. res.on('end', () => {
  97. fileStream.end();
  98. win.webContents.send('update-downloaded', { path: tmpPath });
  99. });
  100. });
  101. req.on('error', (err) => {
  102. win.webContents.send('update-error', { message: err.message || String(err) });
  103. });
  104. req.end();
  105. });
  106. // 安装更新(渲染进程确认安装时调用)
  107. ipcMain.on('install-update', (event, filePath) => {
  108. if (!fs.existsSync(filePath)) {
  109. event.sender.send('update-error', { message: '安装文件不存在' });
  110. return;
  111. }
  112. if (process.platform === 'win32') {
  113. // Windows:直接执行安装程序(根据你的安装包参数添加静默/等待参数)
  114. try {
  115. spawn(filePath, [], { detached: true, stdio: 'ignore' }).unref();
  116. app.quit();
  117. } catch (e) {
  118. event.sender.send('update-error', { message: e.message });
  119. }
  120. } else if (process.platform === 'darwin') {
  121. // macOS:打开 dmg 或者打开 .pkg
  122. shell.openPath(filePath).then(() => app.quit());
  123. } else {
  124. // linux:按照需要实现
  125. shell.openPath(filePath).then(() => app.quit());
  126. }
  127. });
  128. /**
  129. * 使用 7z 压缩文件/目录,支持密码和进度反馈
  130. * @param {Object} opts 压缩选项
  131. * @param {Array<string>} opts.sources 待压缩的文件或目录列表
  132. * @param {string} opts.dest 目标压缩包路径
  133. * @param {string} [opts.format] 压缩格式,'7z' 或 'zip',默认 'zip'
  134. * @param {number} [opts.level] 压缩级别,0-9,默认 9
  135. * @param {boolean} [opts.recurse] 是否递归子目录,默认 true
  136. * @param {string} [opts.password] 压缩密码
  137. * @returns {Promise<{success: boolean, dest: string}>} 压缩结果
  138. */
  139. ipcMain.handle('compress-with-7z', async (evt, opts) => {
  140. const sender = evt.sender; // 用于发送进度消息
  141. const sources = Array.isArray(opts.sources) ? opts.sources : []; // 待压缩的文件或目录列表
  142. if (!sources.length) throw new Error('no sources');
  143. const dest = path.resolve(opts.dest); // 目标压缩包路径
  144. const format = opts.format === 'zip' ? 'zip' : '7z'; // 压缩格式
  145. const level = Number.isInteger(opts.level) ? Math.max(0, Math.min(9, opts.level)) : 9; // 压缩级别
  146. const recurse = opts.recurse !== false; // 是否递归子目录
  147. // 检查源文件/目录是否存在
  148. for (const s of sources) {
  149. if (!fs.existsSync(s)) throw new Error(`source not found: ${s}`);
  150. }
  151. const sevenPath = sevenBin.path7za; // 7za 可执行路径
  152. const args = ['a', `-t${format}`, `-mx=${level}`, dest]; // 基本参数
  153. if (opts.password) {
  154. args.push(`-p${opts.password}`);
  155. // 仅当使用 7z 格式时才启用头部加密(zip 不支持 -mhe)
  156. if (format === '7z') args.push('-mhe=on'); // 加密文件名/头(仅 7z 支持)
  157. }
  158. if (recurse) args.push('-r'); // 递归
  159. // append sources (支持通配或单个路径)
  160. args.push(...sources);
  161. // 确保目标目录存在(避免 7z 无法写入导致的失败)
  162. try {
  163. fs.mkdirSync(path.dirname(dest), { recursive: true });
  164. } catch (e) {
  165. // 如果无法创建目标目录,尽早报错
  166. throw new Error(`failed to create dest directory: ${e.message}`);
  167. }
  168. return await new Promise((resolve, reject) => {
  169. let stdoutAll = '';
  170. let stderrAll = '';
  171. const child = spawn(sevenPath, args, { windowsHide: true });
  172. // 收集并转发 stdout/stderr,用作进度显示或日志
  173. child.stdout.on('data', (chunk) => {
  174. const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk));
  175. let s = '';
  176. // 7za 在 Windows 上输出常用 OEM/GBK 编码,优先用 iconv-lite 解码
  177. if (process.platform === 'win32' && iconv) {
  178. try {
  179. s = iconv.decode(buf, 'cp936');
  180. } catch (e) {
  181. s = buf.toString('utf8');
  182. }
  183. } else {
  184. s = buf.toString('utf8');
  185. }
  186. stdoutAll += s;
  187. sender.send('compress-progress', s);
  188. });
  189. child.stderr.on('data', (chunk) => {
  190. const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk));
  191. let s = '';
  192. if (process.platform === 'win32' && iconv) {
  193. try {
  194. s = iconv.decode(buf, 'cp936');
  195. } catch (e) {
  196. s = buf.toString('utf8');
  197. }
  198. } else {
  199. s = buf.toString('utf8');
  200. }
  201. stderrAll += s;
  202. sender.send('compress-stderr', s);
  203. });
  204. child.on('error', (err) => reject(err));
  205. child.on('close', (code) => {
  206. if (code === 0) {
  207. resolve({ success: true, dest });
  208. } else {
  209. // 包含 stdout/stderr 与命令信息以便排查
  210. const errMsg = `7z exited with code ${code}\ncommand: ${JSON.stringify({ sevenPath, args })}\nstdout:\n${stdoutAll}\nstderr:\n${stderrAll}`;
  211. const e = new Error(errMsg);
  212. e.code = code;
  213. e.stdout = stdoutAll;
  214. e.stderr = stderrAll;
  215. e.cmd = { sevenPath, args };
  216. return reject(e);
  217. }
  218. });
  219. });
  220. });
  221. /**
  222. * 下载文件
  223. * @param {string} url 文件 URL
  224. * @param {string} destPath 保存路径
  225. */
  226. ipcMain.handle('download-file', async (evt, { url, destPath }) => {
  227. return new Promise((resolve, reject) => {
  228. const fileStream = fs.createWriteStream(destPath); // 创建写入流
  229. const req = net.request(url);
  230. req.on('response', (res) => {
  231. res.on('data', (chunk) => {
  232. fileStream.write(chunk);
  233. });
  234. res.on('end', () => {
  235. fileStream.end();
  236. resolve(destPath);
  237. });
  238. });
  239. req.on('error', (err) => {
  240. reject(err);
  241. });
  242. req.end();
  243. });
  244. });
  245. /**
  246. * 打开文件对话框
  247. * @param {Object} opts 对话框选项
  248. * @param {string} [opts.title] 对话框标题
  249. * @param {Array<string>} [opts.properties] 对话框属性数组,默认 ['openDirectory']
  250. * @return {Promise<{canceled: boolean, filePaths: string[]}>} 文件路径 canceled 表示是否取消选择 filePaths 表示选择的文件路径数组
  251. */
  252. ipcMain.handle('dialog:openFiles', async (event, options = {}) => {
  253. const result = await dialog.showOpenDialog({
  254. title: options.title || '选择文件夹',
  255. properties: options.properties || ['openDirectory'],
  256. });
  257. return { canceled: result.canceled, filePaths: result.filePaths };
  258. });
  259. // 当所有窗口都已关闭时退出
  260. app.on('window-all-closed', () => {
  261. if (process.platform !== 'darwin') {
  262. app.quit();
  263. }
  264. });
  265. app.on('activate', () => {
  266. if (BrowserWindow.getAllWindows().length === 0) {
  267. createWindow();
  268. }
  269. });