main.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. obj.webContents.openDevTools()
  84. obj.on('close', () => {
  85. // 回收BrowserWindow对象
  86. this.printWin = null
  87. })
  88. return obj
  89. }
  90. /**
  91. * 发送打印机列表
  92. * @param {Obj} win 窗口对象
  93. * @param {String} eventName 需要发送事件名称
  94. */
  95. async getPrintList(win) {
  96. try {
  97. const list = await win.webContents.getPrintersAsync()
  98. win.webContents.send('getPrintList', list)
  99. } catch (error) {
  100. console.log('请升级electron版本')
  101. }
  102. }
  103. /**
  104. * 事件监听
  105. */
  106. on() {
  107. app.on('ready', async () => {
  108. this.loadingWin = this.loading()
  109. this.mainWin = this.main()
  110. this.printWin = this.print()
  111. // 监听渲染进行
  112. ipcMain.once('close-loading', () => {
  113. this.loadingWin.close()
  114. this.mainWin.show()
  115. })
  116. // 监听打开print
  117. ipcMain.on('openPrint', async (event, data) => {
  118. // TODO: 页面数据生成PDF
  119. // 回传打印机列表
  120. await this.getPrintList(this.printWin)
  121. this.printWin.show()
  122. console.log('open', data)
  123. })
  124. // 监听关闭print
  125. ipcMain.on('closePrint', (event, data) => {
  126. console.log('close', data)
  127. // 显示打印组件
  128. this.printWin.hide()
  129. })
  130. })
  131. app.on('window-all-closed', () => {
  132. app.quit()
  133. })
  134. app.on('activate', () => {
  135. if (this.mainWin == null) {
  136. this.mainWin = this.main()
  137. }
  138. })
  139. }
  140. }
  141. new InitWin()