main.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. const { app, BrowserWindow, Menu, ipcMain, globalShortcut, dialog, shell } = require('electron');
  2. const path = require('path')
  3. const { createWriteStream, existsSync } = require('fs');
  4. const request = require('request');
  5. let win, loadingWin;
  6. Menu.setApplicationMenu(null) // 去掉菜单栏
  7. app.commandLine.appendSwitch('wm-window-animations-disabled') // 拖动闪屏
  8. /** 打开下载选择路径 */
  9. async function openFileDialog(oldPath = app.getPath('downloads')) {
  10. if (oldPath) return oldPath
  11. const { canceled, filePaths } = await dialog.showOpenDialog(this.win, { title: '选择保存位置', properties: ['openDirectory', 'createDirectory'], defaultPath: oldPath, })
  12. return !canceled ? filePaths[0] : oldPath
  13. }
  14. /**
  15. * 下载
  16. * @param url 下载地址
  17. */
  18. async function downloadFile(url) {
  19. const filePath = await this.openFileDialog()
  20. // FN2
  21. // this.win.webContents.downloadURL(url);
  22. // this.win.webContents.session.once('will-download', async (event, item, webContents) => {
  23. // const path = await this.openFileDialog()
  24. // const filePath = path.join(path,`${filename}`)
  25. // })
  26. // FN1
  27. const res = await new Promise((res, rej) => {
  28. const stream = request(url).pipe(createWriteStream(filePath))
  29. stream.on('finish', res(true))
  30. stream.on('error', rej(false))
  31. })
  32. dialog.showMessageBox(this.win, { message: res ? '下载成功' : '下载失败' })
  33. }
  34. /** 打开文件夹 */
  35. function openFileInFolder(path) {
  36. if (!existsSync(path)) return false
  37. shell.showItemInFolder(path)
  38. return true
  39. }
  40. // 创建loading 窗口
  41. const showLoading = () => {
  42. loadingWin = new BrowserWindow({
  43. frame: false, // 无边框(窗口、工具栏等),只包含网页内容
  44. width: 200,
  45. height: 200,
  46. resizable: false,
  47. center: true,
  48. alwaysOnTop: true,
  49. transparent: true // 窗口是否支持透明,如果想做高级效果最好为true
  50. })
  51. loadingWin.loadFile('loading.html')
  52. loadingWin.on('close', () => {
  53. loadingWin = null
  54. })
  55. }
  56. // 创建主程序 窗口
  57. const createWindow = () => {
  58. win = new BrowserWindow({
  59. minWidth: 1620,
  60. minHeight: 900,
  61. webPreferences: {
  62. contextIsolation: true,
  63. nodeIntegration: true,
  64. webSecurity: false,
  65. preload: path.join(__dirname, './preload.js')
  66. },
  67. show: false
  68. })// 创建一个窗口
  69. // 不同环境加载不同文件
  70. if (app.isPackaged) {
  71. win.loadFile('dist/index.html')
  72. } else {
  73. win.loadURL('http://localhost:6547/')
  74. }
  75. // 事件监听
  76. win.on('close', () => {
  77. // 回收BrowserWindow对象
  78. win = null
  79. })
  80. }
  81. // 事件监听
  82. app.on('ready', async () => {
  83. showLoading()
  84. createWindow()
  85. // 监听渲染进行
  86. ipcMain.once('close-loading', () => {
  87. loadingWin.close()
  88. win.show()
  89. })
  90. // 在BrowserWindow创建完成后,注册全局快捷键
  91. globalShortcut.register('Control+F12', () => {
  92. win.webContents.toggleDevTools()
  93. })
  94. })
  95. app.on('window-all-closed', () => {
  96. app.quit()
  97. })
  98. app.on('activate', () => {
  99. if (win == null) {
  100. createWindow()
  101. }
  102. })