background.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. 'use strict'
  2. import { app, protocol, BrowserWindow, Menu, ipcMain, dialog, net } from 'electron'
  3. import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
  4. import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
  5. import { autoUpdater } from 'electron-updater'
  6. import Store from 'electron-store'
  7. import { exec } from 'child_process'
  8. import cqcyCode from "@/utils/cqcyCode";
  9. Store.initRenderer()
  10. const isDevelopment = process.env.NODE_ENV !== 'production'
  11. let win
  12. let cmdStr = 'start ./public/pkg/PrintServerApp-1.0.0.exe'
  13. /**
  14. * 方案必须在应用准备就绪之前注册
  15. */
  16. protocol.registerSchemesAsPrivileged([
  17. { scheme: 'app', privileges: { secure: true, standard: true } }
  18. ])
  19. /**
  20. * 创建菜单
  21. * @param name
  22. * @returns {Electron.BrowserWindow}
  23. */
  24. function creatMenuWin(name) {
  25. return new BrowserWindow({
  26. title: name ? name : 'Easy Industrial Report',
  27. width: 300,
  28. height: 300,
  29. resizable: false,
  30. movable: false,
  31. minimizable: false,
  32. maximizable: false,
  33. webPreferences: {
  34. nodeIntegration: true
  35. }
  36. })
  37. }
  38. /**
  39. * 更新通知信息
  40. * @type {{checking: {msg: string, status: number}, updateAva: {msg: string, status: number}, updateNotAva: {msg: string, status: number}, error: {msg: string, status: number}}}
  41. */
  42. const returnData = {
  43. error: { status: -1, msg: '检测更新查询异常' },
  44. checking: { status: 0, msg: '正在检查应用程序更新' },
  45. updateAva: { status: 1, msg: '检测到新版本,正在下载,请稍后' },
  46. updateNotAva: { status: -1, msg: '您现在使用的版本为最新版本,无需更新!' }
  47. }
  48. /**
  49. * 创建通知
  50. * @param text
  51. */
  52. function sendStatusToWindow(text) {
  53. win.webContents.send('message', text);
  54. }
  55. /**
  56. * 创建主窗口
  57. * @returns {Promise<void>}
  58. */
  59. async function createWindow() {
  60. // 初始化主窗口服务
  61. win = new BrowserWindow({
  62. width: 1920,
  63. height: 1080,
  64. frame: true, // false 表示去掉顶部的导航以及最大化、最小化、关闭按钮
  65. fullscreen: false, // true 全屏模式
  66. resizable: true, // 缩放
  67. movable: true, // 移动
  68. webPreferences: {
  69. webSecurity: false,
  70. nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
  71. contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION
  72. }
  73. })
  74. // 设置窗口最大化
  75. win.maximize()
  76. if (isDevelopment) {
  77. // 开启调试模式
  78. win.webContents.openDevTools()
  79. } else {
  80. // 去掉顶部菜单
  81. win.setMenu(null)
  82. }
  83. // 菜单模板设置
  84. let templateMenu = [
  85. {
  86. label: '首页'
  87. },
  88. {
  89. label: '帮助',
  90. submenu: [
  91. {
  92. label: '检查更新',
  93. click: () => {
  94. }
  95. }
  96. ]
  97. },
  98. {
  99. label: '关于',
  100. click: () => {
  101. let winForAbout = creatMenuWin('关于')
  102. // 去掉顶部菜单
  103. winForAbout.setMenu(null)
  104. winForAbout.loadFile('yellow.html')
  105. winForAbout.on('closed', () => {
  106. winForAbout = null
  107. })
  108. }
  109. },
  110. ]
  111. // 加载菜单
  112. let m = Menu.buildFromTemplate(templateMenu)
  113. // Menu.setApplicationMenu(m)
  114. if (process.env.WEBPACK_DEV_SERVER_URL) {
  115. // 如果处于开发模式,则加载开发服务器的 url
  116. await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
  117. if (!process.env.IS_TEST) win.webContents.openDevTools()
  118. } else {
  119. createProtocol('app')
  120. // 在正式环境时加载 index.html
  121. await win.loadURL('app://./index.html')
  122. }
  123. }
  124. /**
  125. * 检查更新
  126. */
  127. autoUpdater.on('checking-for-update', (res) => {
  128. sendStatusToWindow(returnData.checking);
  129. })
  130. /**
  131. * 有更新发现
  132. */
  133. autoUpdater.on('update-available', (info) => {
  134. // sendStatusToWindow(returnData.updateAva);
  135. dialog.showMessageBox({
  136. type: 'info',
  137. title: '软件更新',
  138. message: '发现新版本, 确定更新?',
  139. buttons: ['确定', '取消']
  140. }).then(resp => {
  141. if (resp.response == 0) {
  142. createWindow().then(r => {})
  143. autoUpdater.downloadUpdate().then(r => {})
  144. }
  145. })
  146. })
  147. /**
  148. * 暂无更新可用
  149. */
  150. autoUpdater.on('update-not-available', (info) => {
  151. sendStatusToWindow(returnData.updateNotAva);
  152. })
  153. /**
  154. * 检测更新失败
  155. */
  156. autoUpdater.on('error', (err) => {
  157. sendStatusToWindow(returnData.error + err);
  158. })
  159. /**
  160. * 更新下载进度
  161. */
  162. autoUpdater.on('download-progress', (progressObj) => {
  163. // let log_message = "Download speed: " + progressObj.bytesPerSecond;
  164. // log_message = log_message + ' - Downloaded ' + progressObj.percent + '%';
  165. // log_message = log_message + ' (' + progressObj.transferred + "/" + progressObj.total + ')';
  166. // sendStatusToWindow(log_message);
  167. win.webContents.send('downloadProgress', progressObj);
  168. })
  169. /**
  170. * 更新下载完成
  171. */
  172. autoUpdater.on('update-downloaded', (info) => {
  173. // sendStatusToWindow('Update downloaded');
  174. // ipcMain.on('isUpdateNow', (e, arg) => {
  175. // // autoUpdater.quitAndInstall()方法,可实现立即关闭程序并安装
  176. // autoUpdater.quitAndInstall();
  177. // });
  178. dialog.showMessageBox({
  179. title: '下载完成',
  180. message: '最新版本已下载完成, 退出程序进行安装'
  181. }).then(() => {
  182. autoUpdater.quitAndInstall()
  183. })
  184. });
  185. /**
  186. * 关闭所有窗口后退出。
  187. */
  188. app.on('window-all-closed', () => {
  189. if (process.platform !== 'darwin') {
  190. app.quit()
  191. }
  192. })
  193. /**
  194. * 应用激活
  195. */
  196. app.on('activate', () => {
  197. if (BrowserWindow.getAllWindows().length === 0) createWindow().then(r => {})
  198. })
  199. /**
  200. * 当 Electron 完成后,将调用此方法。初始化并准备创建浏览器窗口。某些API只能在此事件发生后使用。
  201. */
  202. app.on('ready', async () => {
  203. if (isDevelopment && !process.env.IS_TEST) {
  204. try {
  205. await installExtension(VUEJS_DEVTOOLS)
  206. } catch (e) {
  207. console.error('Vue Devtools failed to install:', e.toString())
  208. }
  209. }
  210. await createWindow()
  211. runExec()
  212. })
  213. app.on('ready', function() {
  214. autoUpdater.checkForUpdatesAndNotify().then(r => {});
  215. });
  216. function runExec () {
  217. // 请求网络服务net
  218. let request = net.request(cqcyCode['checkPrintServerInstated']);
  219. request.on("response", response=> {
  220. // 获取请求状态码
  221. if (response.statusCode !== 200) {
  222. // dialog.showMessageBox({
  223. // title: '系统提示',
  224. // message: '检测到您还未安装自动打印服务,建议您进行安装。'
  225. // }).then(e => {
  226. // exec(cmdStr, {});
  227. // });
  228. exec(cmdStr, {});
  229. }
  230. });
  231. request.on('error', (error) => {
  232. // dialog.showMessageBox({
  233. // title: '系统提示',
  234. // message: JSON.stringify(error)
  235. // }).then(e => {
  236. // exec(cmdStr, {});
  237. // });
  238. exec(cmdStr, {});
  239. });
  240. request.end();
  241. }
  242. if (isDevelopment) {
  243. if (process.platform === 'win32') {
  244. process.on('message', (data) => {
  245. if (data === 'graceful-exit') {
  246. app.quit();
  247. }
  248. });
  249. } else {
  250. process.on('SIGTERM', () => {
  251. app.quit();
  252. });
  253. }
  254. }