main.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import { app, BrowserWindow, dialog, ipcMain } from 'electron'
  2. import { resolve } from 'path'
  3. import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-assembler'
  4. import path from 'path'
  5. import fs from 'fs'
  6. if (!app.requestSingleInstanceLock()) {
  7. app.quit()
  8. process.exit(0)
  9. }
  10. // async function createWindow() {
  11. // const loadingView = new BrowserWindow({
  12. // width: 520,
  13. // height: 150,
  14. // frame: false,
  15. // icon: path.join(__dirname, './icons/icon.png'),
  16. // resizable: false,
  17. // transparent: true,
  18. // show: true,
  19. // // webPreferences: {
  20. // // preload: path.join(__dirname, '../preload/preload'),
  21. // // },
  22. // })
  23. // loadingView.loadFile(resolve(__dirname, 'loading.html'))
  24. // const mainWin = new BrowserWindow({
  25. // show: false,
  26. // width: 1680,
  27. // height: 900,
  28. // frame: false,
  29. // center: true,
  30. // icon: path.join(__dirname, './icons/icon.png'),
  31. // resizable: false,
  32. // fullscreen: app.isPackaged ? true : false,
  33. // fullscreenable: true,
  34. // alwaysOnTop: false,
  35. // useContentSize: true,
  36. // transparent: true,
  37. // webPreferences: {
  38. // devTools: true,
  39. // nodeIntegration: false,
  40. // contextIsolation: true,
  41. // webSecurity: false,
  42. // experimentalFeatures: true,
  43. // navigateOnDragDrop: false,
  44. // disableHtmlFullscreenWindowResize: false,
  45. // // preload: path.join(__dirname, '../preload/preload'),
  46. // },
  47. // })
  48. // mainWin.setMenu(null)
  49. // if (!app.isPackaged) {
  50. // mainWin.webContents.on('before-input-event', (event, input) => {
  51. // if (input.key === 'F12') {
  52. // mainWin.webContents.openDevTools()
  53. // }
  54. // })
  55. // }
  56. // mainWin.on('ready-to-show', () => {
  57. // loadingView.destroy()
  58. // mainWin.show()
  59. // })
  60. // if (app.isPackaged) {
  61. // mainWin.loadURL('http://cet-test.markingtool.cn')
  62. // // mainWin.loadURL('http://192.168.10.177:81')
  63. // } else {
  64. // installDevTool(VUEJS_DEVTOOLS)
  65. // process.env.WEB_DEV_INDEX_URL && mainWin.loadURL(process.env.WEB_DEV_INDEX_URL)
  66. // }
  67. // }
  68. let mainWin: any = null
  69. async function createWindow() {
  70. const loadingView = new BrowserWindow({
  71. width: 520,
  72. height: 150,
  73. frame: false,
  74. icon: path.join(__dirname, './icons/icon.ico'),
  75. resizable: false,
  76. transparent: true,
  77. show: true,
  78. // webPreferences: {
  79. // preload: path.join(__dirname, '../preload/preload'),
  80. // },
  81. })
  82. loadingView.loadFile(resolve(__dirname, 'loading.html'))
  83. // if (!app.isPackaged) {
  84. // try {
  85. // await installExtension(VUEJS_DEVTOOLS)
  86. // } catch (e) {
  87. // console.error('Vue Devtools failed to install:', e.toString())
  88. // }
  89. // }
  90. mainWin = new BrowserWindow({
  91. show: false,
  92. width: 1680,
  93. height: 900,
  94. frame: false,
  95. center: true,
  96. icon: path.join(__dirname, './icons/icon.ico'),
  97. resizable: false,
  98. fullscreen: app.isPackaged ? true : true,
  99. fullscreenable: true,
  100. alwaysOnTop: false,
  101. useContentSize: true,
  102. transparent: true,
  103. webPreferences: {
  104. devTools: true,
  105. nodeIntegration: false,
  106. contextIsolation: true,
  107. webSecurity: false,
  108. experimentalFeatures: true,
  109. navigateOnDragDrop: false,
  110. disableHtmlFullscreenWindowResize: false,
  111. preload: path.join(__dirname, '../preload/preload'),
  112. },
  113. })
  114. mainWin.setMenu(null)
  115. // if (!app.isPackaged) {
  116. mainWin.webContents.on('before-input-event', (event, input) => {
  117. if (input.key === 'F12' && input.control) {
  118. mainWin.webContents.openDevTools()
  119. }
  120. })
  121. // }
  122. mainWin.on('ready-to-show', () => {
  123. loadingView.destroy()
  124. mainWin.show()
  125. })
  126. if (app.isPackaged) {
  127. const filePath = path.join(process.env.PORTABLE_EXECUTABLE_DIR as string, 'config.json')
  128. if (!fs.existsSync(filePath)) {
  129. dialog.showErrorBox('找不到配置文件', 'config.json文件不存在!')
  130. app.quit()
  131. } else {
  132. fs.readFile(filePath, 'utf-8', function (err, data) {
  133. if (!err) {
  134. let da: any = {}
  135. try {
  136. da = JSON.parse(data)
  137. } catch (error) {
  138. dialog.showErrorBox('错误', '您的config.json文件内容格式可能不正确,请检查!')
  139. app.quit()
  140. }
  141. const url = da.url || ''
  142. if (!!url) {
  143. mainWin.loadURL(url)
  144. }
  145. } else {
  146. dialog.showErrorBox('错误', JSON.stringify(err))
  147. app.quit()
  148. }
  149. })
  150. }
  151. } else {
  152. // mainWin.loadURL('http://cet-test.markingtool.cn')
  153. process.env.WEB_DEV_INDEX_URL && mainWin.loadURL(process.env.WEB_DEV_INDEX_URL)
  154. }
  155. }
  156. ipcMain.on('window-min', () => {
  157. mainWin.minimize()
  158. })
  159. app.on('window-all-closed', () => {
  160. if (process.platform !== 'darwin') app.quit()
  161. })
  162. app.whenReady().then(createWindow)