main.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. const {
  2. app, BrowserWindow, Menu, ipcMain
  3. } = require('electron')// 引入electron
  4. // ----------------窗口init-------------
  5. let win = null
  6. let loadingWin = null
  7. let print = null
  8. Menu.setApplicationMenu(null) // 去掉菜单栏
  9. app.commandLine.appendSwitch('wm-window-animations-disabled') // 拖动闪屏
  10. // 创建loading 窗口
  11. const showLoading = () => {
  12. loadingWin = new BrowserWindow({
  13. frame: false, // 无边框(窗口、工具栏等),只包含网页内容
  14. width: 200,
  15. height: 200,
  16. resizable: false,
  17. center: true,
  18. alwaysOnTop: true,
  19. transparent: true // 窗口是否支持透明,如果想做高级效果最好为true
  20. })
  21. loadingWin.loadFile('loading/index.html')
  22. loadingWin.on('close', () => {
  23. loadingWin = null
  24. })
  25. }
  26. // 创建主程序 窗口
  27. const createWindow = () => {
  28. win = new BrowserWindow({
  29. maxWidth: 1920,
  30. maxHeight: 1080,
  31. minWidth: 1620,
  32. minHeight: 900,
  33. webPreferences: {
  34. contextIsolation: false,
  35. nodeIntegration: true,
  36. webSecurity: false // 去掉跨越
  37. },
  38. show: false
  39. })// 创建一个窗口
  40. // 在窗口内要展示的内容index.html 就是打包生成的index.html
  41. win.loadFile('dist/index.html')
  42. // 开启调试工具
  43. win.webContents.openDevTools()
  44. // 事件监听
  45. win.on('close', () => {
  46. // 回收BrowserWindow对象
  47. win = null
  48. print = null
  49. })
  50. }
  51. // 创建打印窗口=>宽高根据主窗口宽高80%定
  52. const createPrint = () => {
  53. // 获取主窗口宽高
  54. // const winWH = win.getBounds()
  55. // const winWidth = 0
  56. // const winHeight = 0
  57. // console.log(6666, winWH)
  58. print = new BrowserWindow({
  59. width: 1310,
  60. height: 860,
  61. frame: false,
  62. resizable: false,
  63. center: true,
  64. transparent: true,
  65. show: false,
  66. webPreferences: {
  67. contextIsolation: false,
  68. nodeIntegration: true,
  69. webSecurity: false
  70. }
  71. })// 创建一个窗口
  72. print.loadFile('dist/print.html')
  73. // print.webContents.openDevTools()
  74. print.on('close', () => {
  75. // 回收BrowserWindow对象
  76. print = null
  77. })
  78. }
  79. // --------------事件监听fn-------------
  80. // 事件监听
  81. app.on('ready', async () => {
  82. showLoading()
  83. createWindow()
  84. createPrint()
  85. // 监听渲染进行
  86. ipcMain.once('close-loading', () => {
  87. loadingWin.close()
  88. win.show()
  89. })
  90. // 监听打开print
  91. ipcMain.on('openPrint', (event, data) => {
  92. // TODO: 页面数据生成PDF
  93. // TODO: 数据回传打印组件
  94. // 显示打印组件
  95. print.show()
  96. console.log('open', data)
  97. })
  98. // 监听关闭print
  99. ipcMain.on('closePrint', (event, data) => {
  100. console.log('close', data)
  101. // 显示打印组件
  102. print.hide()
  103. })
  104. })
  105. app.on('window-all-closed', () => {
  106. app.quit()
  107. })
  108. app.on('activate', () => {
  109. if (win == null) {
  110. createWindow()
  111. }
  112. })