main.js 3.1 KB

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