gaoshuaixing 5 gadi atpakaļ
vecāks
revīzija
441b566845
2 mainītis faili ar 54 papildinājumiem un 15 dzēšanām
  1. 22 1
      electron/config.js
  2. 32 14
      main.js

+ 22 - 1
electron/config.js

@@ -5,6 +5,23 @@ const dayjs = require('dayjs');
 const storage = require('./storage');
 
 const config = {
+  developmentMode: {
+    default: 'vue',
+    mode: {
+      vue: {
+        hostname: 'localhost',
+        port: 8080
+      },
+      react: {
+        hostname: 'localhost',
+        port: 3000
+      },
+      ejs: {
+        hostname: 'localhost',
+        port: 7068 // The same as the egg port
+      }
+    }
+  },
   log: {
     file: {
       fileName: path.normalize('./logs/electron-' + dayjs().format('YYYY-MM-DD') + '.log'),
@@ -30,7 +47,7 @@ const config = {
     title: 'electron-egg',
     env: 'prod',
     port: 7068,
-    hostname: '0.0.0.0',
+    hostname: 'localhost',
     workers: 1
   },
   autoUpdate: {
@@ -44,6 +61,10 @@ const config = {
 
 exports.get = function (flag = '', env = 'prod') {
   console.log('[config] [get] flag:', flag);
+  if (flag === 'developmentMode') {
+    return config.developmentMode;
+  }
+
   if (flag === 'log') {
     return config.log;
   }

+ 32 - 14
main.js

@@ -26,14 +26,6 @@ eggConfig.env = ENV
 
 if (process.mas) app.setName('electron-egg')
 
-// Open url with the default browser
-app.on('web-contents-created', (e, webContents) => {
-    webContents.on('new-window', (event, url) => {
-        event.preventDefault()
-        shell.openExternal(url)
-    });
-});
-
 async function initialize () {
   app.whenReady().then(() => {
     createWindow()
@@ -50,6 +42,14 @@ async function initialize () {
       app.quit()
     }
   })
+
+  // Open url with the default browser
+  // app.on('web-contents-created', (e, webContents) => {
+  //   webContents.on('new-window', (event, url) => {
+  //       event.preventDefault()
+  //       shell.openExternal(url)
+  //   });
+  // });
 }
 
 async function createWindow () {
@@ -72,9 +72,7 @@ async function createWindow () {
   MAIN_WINDOW.loadURL(path.join('file://', __dirname, '/app/public/loading.html'))
   
   // egg server
-  setTimeout(function(){
-    startServer(eggConfig)
-  }, 100)
+  await startServer(eggConfig)
 
   // check update
   const updateConfig = electronConfig.get('autoUpdate')
@@ -86,16 +84,36 @@ async function createWindow () {
 }
 
 async function startServer (options) {
-  let startRes = null
   ELog.info('[main] [startServer] options', options)
+  const protocol = 'http://'
+  let startRes = null
+  let url = null
+  if (ENV === 'prod') {
+    url = protocol + options.hostname + ':' + options.port
+  } else {
+    const developmentModeConfig = electronConfig.get('developmentMode', ENV)
+    const selectMode = developmentModeConfig.default
+    const modeInfo = developmentModeConfig.mode[selectMode]
+    switch (selectMode) {
+      case 'vue' :
+        url = protocol + modeInfo.hostname + ':' + modeInfo.port
+        break
+      case 'react' :
+        url = protocol + modeInfo.hostname + ':' + modeInfo.port
+        break
+      case 'ejs' :
+        url = protocol + modeInfo.hostname + ':' + modeInfo.port
+        break
+    }
+  }
+  ELog.info('[main] [url]:', url)
   startRes = await eggLauncher.start(options).then((res) => res, (err) => err)
   ELog.info('[main] [startServer] startRes:', startRes)
   if (startRes === 'success') {
-    let url = 'http://localhost:' + options.port
     MAIN_WINDOW.loadURL(url)
-
     return true
   }
+  
   app.relaunch()
 }