main.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. // 7za 可执行路径,优先使用 seven-bin 提供的路径;若被打包进 app.asar,则尝试 app.asar.unpacked 路径
  152. let sevenPath = sevenBin.path7za;
  153. try {
  154. // 如果 sevenPath 在 app.asar 内,映射到 app.asar.unpacked 的对应相对路径
  155. const asarToken = `${path.sep}app.asar${path.sep}`;
  156. if (typeof sevenPath === 'string' && sevenPath.indexOf(asarToken) !== -1) {
  157. const rel = sevenPath.split(asarToken)[1]; // 例如: node_modules/7zip-bin/win/x64/7za.exe
  158. const unpackedCandidate = path.join(process.resourcesPath, 'app.asar.unpacked', rel);
  159. if (fs.existsSync(unpackedCandidate)) {
  160. sevenPath = unpackedCandidate;
  161. }
  162. }
  163. // 若上面没有找到,尝试常见的 unpacked 路径(兼容不同包结构与架构)
  164. if (!fs.existsSync(sevenPath)) {
  165. const candidates = [
  166. path.join(
  167. process.resourcesPath,
  168. 'app.asar.unpacked',
  169. 'node_modules',
  170. '7zip-bin',
  171. 'win',
  172. 'x64',
  173. path.basename(sevenPath),
  174. ),
  175. path.join(
  176. process.resourcesPath,
  177. 'app.asar.unpacked',
  178. 'node_modules',
  179. '7zip-bin',
  180. 'win',
  181. 'x86',
  182. path.basename(sevenPath),
  183. ),
  184. path.join(
  185. process.resourcesPath,
  186. 'app.asar.unpacked',
  187. 'node_modules',
  188. '7zip-bin',
  189. 'bin',
  190. path.basename(sevenPath),
  191. ),
  192. ];
  193. for (const c of candidates) {
  194. if (fs.existsSync(c)) {
  195. sevenPath = c;
  196. break;
  197. }
  198. }
  199. }
  200. } catch (err) {
  201. console.error('Error checking 7za path:', err);
  202. }
  203. if (!fs.existsSync(sevenPath)) {
  204. throw new Error(
  205. `7za executable not found: ${sevenPath}. If running from a packaged app, add "asarUnpack": ["node_modules/7zip-bin/**"] to build config so 7za is unpacked.`,
  206. );
  207. }
  208. const args = ['a', `-t${format}`, `-mx=${level}`, dest]; // 基本参数
  209. if (opts.password) {
  210. args.push(`-p${opts.password}`);
  211. // 仅当使用 7z 格式时才启用头部加密(zip 不支持 -mhe)
  212. if (format === '7z') args.push('-mhe=on'); // 加密文件名/头(仅 7z 支持)
  213. }
  214. if (recurse) args.push('-r'); // 递归
  215. // append sources (支持通配或单个路径)
  216. args.push(...sources);
  217. // 确保目标目录存在(避免 7z 无法写入导致的失败)
  218. try {
  219. fs.mkdirSync(path.dirname(dest), { recursive: true });
  220. } catch (e) {
  221. // 如果无法创建目标目录,尽早报错
  222. throw new Error(`failed to create dest directory: ${e.message}`);
  223. }
  224. return await new Promise((resolve, reject) => {
  225. let stdoutAll = '';
  226. let stderrAll = '';
  227. const child = spawn(sevenPath, args, { windowsHide: true });
  228. // 收集并转发 stdout/stderr,用作进度显示或日志
  229. child.stdout.on('data', (chunk) => {
  230. const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk));
  231. let s = '';
  232. // 7za 在 Windows 上输出常用 OEM/GBK 编码,优先用 iconv-lite 解码
  233. if (process.platform === 'win32' && iconv) {
  234. try {
  235. s = iconv.decode(buf, 'cp936');
  236. } catch (e) {
  237. s = buf.toString('utf8');
  238. }
  239. } else {
  240. s = buf.toString('utf8');
  241. }
  242. stdoutAll += s;
  243. sender.send('compress-progress', s);
  244. });
  245. child.stderr.on('data', (chunk) => {
  246. const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk));
  247. let s = '';
  248. if (process.platform === 'win32' && iconv) {
  249. try {
  250. s = iconv.decode(buf, 'cp936');
  251. } catch (e) {
  252. s = buf.toString('utf8');
  253. }
  254. } else {
  255. s = buf.toString('utf8');
  256. }
  257. stderrAll += s;
  258. sender.send('compress-stderr', s);
  259. });
  260. child.on('error', (err) => reject(err));
  261. child.on('close', (code) => {
  262. if (code === 0) {
  263. resolve({ success: true, dest });
  264. } else {
  265. // 包含 stdout/stderr 与命令信息以便排查
  266. const errMsg = `7z exited with code ${code}\ncommand: ${JSON.stringify({ sevenPath, args })}\nstdout:\n${stdoutAll}\nstderr:\n${stderrAll}`;
  267. const e = new Error(errMsg);
  268. e.code = code;
  269. e.stdout = stdoutAll;
  270. e.stderr = stderrAll;
  271. e.cmd = { sevenPath, args };
  272. return reject(e);
  273. }
  274. });
  275. });
  276. });
  277. /**
  278. * 下载文件
  279. * @param {string} url 文件 URL
  280. * @param {string} destPath 保存路径
  281. */
  282. ipcMain.handle('download-file', async (evt, { url, destPath }) => {
  283. return new Promise((resolve, reject) => {
  284. const fileStream = fs.createWriteStream(destPath); // 创建写入流
  285. const req = net.request(url);
  286. req.on('response', (res) => {
  287. res.on('data', (chunk) => {
  288. fileStream.write(chunk);
  289. });
  290. res.on('end', () => {
  291. fileStream.end();
  292. resolve(destPath);
  293. });
  294. });
  295. req.on('error', (err) => {
  296. reject(err);
  297. });
  298. req.end();
  299. });
  300. });
  301. /**
  302. * 打开文件对话框
  303. * @param {Object} opts 对话框选项
  304. * @param {string} [opts.title] 对话框标题
  305. * @param {Array<string>} [opts.properties] 对话框属性数组,默认 ['openDirectory']
  306. * @return {Promise<{canceled: boolean, filePaths: string[]}>} 文件路径 canceled 表示是否取消选择 filePaths 表示选择的文件路径数组
  307. */
  308. ipcMain.handle('dialog:openFiles', async (event, options = {}) => {
  309. const result = await dialog.showOpenDialog({
  310. title: options.title || '选择文件夹',
  311. properties: options.properties || ['openDirectory'],
  312. });
  313. return { canceled: result.canceled, filePaths: result.filePaths };
  314. });
  315. // 当所有窗口都已关闭时退出
  316. app.on('window-all-closed', () => {
  317. if (process.platform !== 'darwin') {
  318. app.quit();
  319. }
  320. });
  321. app.on('activate', () => {
  322. if (BrowserWindow.getAllWindows().length === 0) {
  323. createWindow();
  324. }
  325. });