main.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. const {
  2. app, BrowserWindow, Menu, ipcMain
  3. } = require('electron')// 引入electron
  4. const path = require('path')
  5. const fs = require('fs')
  6. class InitWin {
  7. constructor() {
  8. Menu.setApplicationMenu(null) // 去掉菜单栏
  9. app.commandLine.appendSwitch('wm-window-animations-disabled') // 拖动闪屏
  10. this.loadingWin = null
  11. this.mainWin = null
  12. this.printWin = null
  13. this.filePath = path.join(__dirname, './dist/print.pdf')
  14. this.on()
  15. }
  16. /**
  17. * 加载窗口
  18. * @returns
  19. */
  20. loading() {
  21. const obj = new BrowserWindow({
  22. frame: false, // 无边框(窗口、工具栏等),只包含网页内容
  23. width: 200,
  24. height: 200,
  25. resizable: false,
  26. center: true,
  27. alwaysOnTop: true,
  28. transparent: true // 窗口是否支持透明,如果想做高级效果最好为true
  29. })
  30. obj.loadFile('loading/index.html')
  31. obj.on('close', () => {
  32. this.loadingWin = null
  33. })
  34. return obj
  35. }
  36. /**
  37. * 主窗口
  38. * @returns
  39. */
  40. main() {
  41. const obj = new BrowserWindow({
  42. maxWidth: 1920,
  43. maxHeight: 1080,
  44. minWidth: 1620,
  45. minHeight: 900,
  46. useContentSize: true,
  47. center: true,
  48. webPreferences: {
  49. contextIsolation: false,
  50. nodeIntegration: true,
  51. webSecurity: false // 去掉跨越
  52. },
  53. show: false
  54. })// 创建一个窗口
  55. // 在窗口内要展示的内容index.html 就是打包生成的index.html
  56. obj.loadFile('dist/index.html')
  57. // 开启调试工具
  58. // obj.webContents.openDevTools()
  59. // 事件监听
  60. obj.on('close', () => {
  61. // 回收BrowserWindow对象
  62. this.mainWin = null
  63. })
  64. return obj
  65. }
  66. /**
  67. * 打印窗口
  68. * @returns
  69. */
  70. print() {
  71. const obj = new BrowserWindow({
  72. width: 970,
  73. height: 760,
  74. frame: false,
  75. resizable: false,
  76. center: true,
  77. transparent: true,
  78. show: false,
  79. webPreferences: {
  80. contextIsolation: false,
  81. nodeIntegration: true,
  82. webSecurity: false
  83. }
  84. })// 创建一个窗口
  85. obj.loadFile('dist/print.html')
  86. obj.webContents.openDevTools()
  87. obj.on('close', () => {
  88. // 回收BrowserWindow对象
  89. this.printWin = null
  90. })
  91. return obj
  92. }
  93. /**
  94. * 保存打印文件默认保存在dist中
  95. * @param {object} win
  96. * @param {object} option
  97. */
  98. async SavePrintData(win, option) {
  99. if (!option || !Object.keys(option).length) {
  100. option = {
  101. marginsType: 1, // 默认边距
  102. pageSize: 'A4',
  103. printBackground: true, // 文件中包含 CSS 背景
  104. printSelectionOnly: false,
  105. landscape: false// 纵向模式
  106. }
  107. }
  108. const buffer = await win.webContents.printToPDF(option)
  109. return new Promise((resolve, reject) => {
  110. fs.writeFile(this.filePath, buffer, (err) => {
  111. if (err) {
  112. console.log('save error')
  113. return reject(err)
  114. }
  115. return resolve(this.filePath)
  116. })
  117. })
  118. }
  119. /**
  120. * 事件监听
  121. */
  122. on() {
  123. app.on('ready', async () => {
  124. this.loadingWin = this.loading()
  125. this.mainWin = this.main()
  126. this.printWin = this.print()
  127. // 监听渲染进行
  128. ipcMain.once('close-loading', () => {
  129. this.loadingWin.close()
  130. this.mainWin.show()
  131. })
  132. // 监听打开print
  133. ipcMain.on('openPrint', async (event) => {
  134. try {
  135. // 获取当前BW实例打印机列表
  136. const currentWin = event.sender.getOwnerBrowserWindow()
  137. const list = await currentWin.webContents.getPrintersAsync()
  138. this.printWin.webContents.send('getData', list)
  139. this.printWin.show()
  140. // 转成PDF文件
  141. const filePath = await this.SavePrintData(currentWin)
  142. // 渲染进程加载PDF
  143. this.printWin.webContents.send('loadingPDF', filePath)
  144. } catch (error) {
  145. console.log(error)
  146. this.printWin.webContents.send('loadingPDF', '')
  147. }
  148. })
  149. // 监听关闭print
  150. ipcMain.on('closePrint', (event, data) => {
  151. console.log('close', data)
  152. // 显示打印组件
  153. this.printWin.hide()
  154. })
  155. })
  156. app.on('window-all-closed', () => {
  157. app.quit()
  158. })
  159. app.on('activate', () => {
  160. if (this.mainWin == null) {
  161. this.mainWin = this.main()
  162. }
  163. })
  164. }
  165. }
  166. new InitWin()