This commit is contained in:
萌森 2024-07-01 14:08:06 +08:00
parent 0982fdd16e
commit 7080571a6f
6 changed files with 199 additions and 167 deletions

74
main.js
View File

@ -3,48 +3,13 @@ const path = require('node:path')
const server = require('./server/server'); const server = require('./server/server');
const log = require('electron-log') const log = require('electron-log')
const mainWin = require('./windows/app');
if (require('electron-squirrel-startup')) { if (require('electron-squirrel-startup')) {
app.quit() app.quit()
return return
} }
const createWindow = () => {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
devTools: false,
webSecurity: false,
allowRunningInsecureContent: true,
preload: path.join(__dirname, 'preload.js')
}
})
require("./menu/menu.js")
require("./menu/tray.js")
const url = require('url').format({
protocol: 'http',
slashes: true,
hostname: '127.0.0.1',
port: 8787
})
win.loadURL(url)
win.on('minimize', (event) => {
event.preventDefault()
win.hide()
})
win.on('close', (event) => {
event.preventDefault()
win.hide()
})
}
app.whenReady().then(() => { app.whenReady().then(() => {
log.transports.file.encoding = 'utf8'; log.transports.file.encoding = 'utf8';
log.transports.file.file = path.join(app.getPath('userData'), 'logs/iyuu.log'); log.transports.file.file = path.join(app.getPath('userData'), 'logs/iyuu.log');
@ -52,40 +17,27 @@ app.whenReady().then(() => {
log.info('App is starting...'); log.info('App is starting...');
server.startServer() server.startServer()
require("./menu/tray.js")
log.info("[About] 新疆萌森软件开发工作室提供技术支持"); log.info("[About] 新疆萌森软件开发工作室提供技术支持");
setTimeout(() => { setTimeout(() => {
createWindow() mainWin.createWindow()
}, 5000) }, 3000)
})
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
} else {
app.show()
BrowserWindow.getAllWindows().forEach(win => {
win.show()
})
}
}) })
app.on('window-all-closed', () => { app.on('window-all-closed', () => {
BrowserWindow.getAllWindows().forEach(win => { mainWin.hideWindows()
win.hide()
})
app.hide()
}) })
app.on('before-quit', () => { app.on('before-quit', () => {
mainWin.hideWindows()
server.stopServer() server.stopServer()
log.info('App is quitting...'); log.info('App is quitting...');
}) })
process.on('uncaughtException', (error) => { app.on('quit', () => {
log.error('Uncaught Exception:', error); app.quit()
}); log.info('App is quitted');
})
process.on('unhandledRejection', (reason, promise) => {
log.error('Unhandled Rejection at:', promise, 'reason:', reason);
});

View File

@ -1,5 +1,6 @@
const { Menu, BrowserWindow } = require("electron") const { Menu, BrowserWindow } = require("electron")
const server = require('../server/server'); const server = require('../server/server');
const mainWin = require('../windows/app');
let template = [ let template = [
{ {
@ -8,20 +9,14 @@ let template = [
{ {
label: "刷新", label: "刷新",
click: () => { click: () => {
BrowserWindow.getAllWindows().forEach(win => { mainWin.refreshUrl()
win.reload()
})
} }
}, },
{ {
label: "重启服务", label: "重启服务",
click: () => { click: () => {
mainWin.closeWindows()
server.restartServer() server.restartServer()
setTimeout(() => {
BrowserWindow.getAllWindows().forEach(win => {
win.reload()
})
}, 2000)
} }
}, },

View File

@ -1,29 +1,21 @@
const { app, Menu, Tray, BrowserWindow } = require("electron") const { app, Menu, Tray } = require("electron")
const path = require('node:path') const path = require('node:path')
const server = require('../server/server'); const server = require('../server/server');
const mainWin = require('../windows/app');
const tray = new Tray('iyuu.ico'); const tray = new Tray('iyuu.ico');
const contextMenu = Menu.buildFromTemplate([{ const contextMenu = Menu.buildFromTemplate([
{
label: '打开主窗口', label: '打开主窗口',
click: () => { click: () => {
BrowserWindow.getAllWindows().forEach(win => { mainWin.showWindows()
win.show()
})
} }
}, { },
{
label: '重启服务', label: '重启服务',
click: () => { click: () => {
BrowserWindow.getAllWindows().forEach(win => {
win.hide()
})
server.restartServer() server.restartServer()
setTimeout(() => {
BrowserWindow.getAllWindows().forEach(win => {
win.show()
win.reload()
})
}, 1000)
} }
}, },
{ {
@ -38,6 +30,9 @@ tray.setToolTip('IYUU');
tray.setContextMenu(contextMenu); tray.setContextMenu(contextMenu);
tray.on('double-click', () => { tray.on('double-click', () => {
win.isVisible() ? win.hide() : win.show() if (mainWin.visibleWindows()) {
win.isVisible() ? win.setSkipTaskbar(false) : win.setSkipTaskbar(true); mainWin.showWindows()
} else {
mainWin.hideWindows()
}
}); });

View File

@ -31,6 +31,7 @@
"dependencies": { "dependencies": {
"electron-log": "^5.1.5", "electron-log": "^5.1.5",
"electron-squirrel-startup": "^1.0.1", "electron-squirrel-startup": "^1.0.1",
"jquery": "^3.7.1",
"tree-kill": "^1.2.2" "tree-kill": "^1.2.2"
} }
} }

View File

@ -2,6 +2,9 @@ const { spawn, exec, execFile } = require('child_process');
const os = require('os'); const os = require('os');
const path = require('path'); const path = require('path');
const log = require('electron-log') const log = require('electron-log')
const mainWin = require('../windows/app')
let serverProcess = null;
function getPhpPath(resourcePath) { function getPhpPath(resourcePath) {
const arch = os.arch(); const arch = os.arch();
@ -15,9 +18,8 @@ function getPhpPath(resourcePath) {
} }
} }
let serverProcess = null;
function startServer() { function startServer() {
if (!serverProcess) {
let resourcePath = process.resourcesPath; let resourcePath = process.resourcesPath;
if (resourcePath.includes('node_modules')) { if (resourcePath.includes('node_modules')) {
resourcePath = process.cwd(); resourcePath = process.cwd();
@ -31,45 +33,28 @@ function startServer() {
log.info(`[IYUU] 工作目录: ${workingDir}`); log.info(`[IYUU] 工作目录: ${workingDir}`);
// serverProcess = exec(`cmd /c ${batPath}`, { serverProcess = execFile('cmd', ['/c', 'windows.bat'], {
// cwd: workingDirectory,
// env: env,
// windowsHide: true
// }, (error, stdout, stderr) => {
// if (error) {
// log.error(`[IYUU] 服务启动错误: ${error.message}`);
// return;
// }
// if (stderr) {
// log.error(`[IYUU] 服务启动 stderr: ${stderr}`);
// return;
// }
// log.info(`[IYUU] 服务启动成功: ${stdout}`);
// });
// serverProcess = execFile('cmd', ['/c', batPath], {
// cwd: workingDirectory,
// env: env,
// killSignal: 'SIGTERM',
// windowsHide: true,
// }, (error, stdout, stderr) => {
// if (error) {
// log.error(`[IYUU] 服务启动错误: ${error.message}`);
// } else {
// log.error(`[IYUU] 服务启动 stderr: ${stderr}`);
// log.info(`[IYUU] 服务启动成功: ${stdout}`);
// }
// });
serverProcess = spawn('cmd', ['/k', 'windows.bat'], {
cwd: workingDir, cwd: workingDir,
stdio: 'pipe',
env: env, env: env,
detached: true, killSignal: 'SIGTERM',
windowsHide: false windowsHide: true,
}, (error, stdout, stderr) => {
if (error) {
log.error(`[IYUU] 服务启动错误: ${error.message}`);
} else {
log.error(`[IYUU] 服务启动 stderr: ${stderr}`);
log.info(`[IYUU] 服务启动成功: ${stdout}`);
}
}); });
// serverProcess = spawn('cmd', ['/c', 'windows.bat'], {
// cwd: workingDir,
// stdio: 'pipe',
// env: env,
// detached: true,
// windowsHide: true
// });
serverProcess.unref(); serverProcess.unref();
if (serverProcess.stdout) { if (serverProcess.stdout) {
@ -80,8 +65,6 @@ function startServer() {
serverProcess.stderr.on('data', (data) => { serverProcess.stderr.on('data', (data) => {
log.error(`[IYUU] ${data}`); log.error(`[IYUU] ${data}`);
}); });
} }
serverProcess.on('error', (err) => { serverProcess.on('error', (err) => {
@ -92,18 +75,19 @@ function startServer() {
log.info("[IYUU] 服务退出:" + code); log.info("[IYUU] 服务退出:" + code);
}); });
} }
}
function stopServer() { function stopServer() {
if (serverProcess) { if (serverProcess) {
mainWin.closeWindows()
log.info("Killing server process with PID:", serverProcess.pid); log.info("Killing server process with PID:", serverProcess.pid);
try { try {
process.kill(-serverProcess.pid, 'SIGTERM'); process.kill(-serverProcess.pid, 'SIGTERM');
log.info("后台服务已关闭..."); log.info("后台服务已关闭...");
serverProcess = null; serverProcess = null;
} catch (err) { } catch (err) {
log.error('Error using process.kill:', err); log.error('Error using process.kill:', err);
exec(`taskkill /PID ${serverProcess.pid} /T /F`, (err, stdout, stderr) => { exec(`taskkill /PID ${serverProcess.pid} /T /F`, (err, _stdout, _stderr) => {
if (err) { if (err) {
log.error('Error using taskkill:', err); log.error('Error using taskkill:', err);
} else { } else {
@ -121,6 +105,9 @@ function restartServer() {
stopServer(); stopServer();
setTimeout(() => { setTimeout(() => {
startServer(); startServer();
setTimeout(() => {
mainWin.showWindows()
}, 2000)
}, 1000); }, 1000);
} }

102
windows/app.js Normal file
View File

@ -0,0 +1,102 @@
const { BrowserWindow } = require('electron')
const path = require('node:path')
const log = require('electron-log')
const url = require('url').format({
protocol: 'http',
hostname: '127.0.0.1',
port: 8787
})
/** @type {BrowserWindow | null} */
let mainWindow = null
const createWindow = () => {
if (mainWindow === null) {
log.info('Create main window')
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, '..', 'preload.js')
}
})
// mainWindow.webContents.openDevTools()
require("../menu/menu")
mainWindow.webContents.loadURL(url)
// mainWindow.loadURL(url)
mainWindow.on('minimize', (event) => {
event.preventDefault()
mainWindow.hide()
})
mainWindow.on('close', (event) => {
event.preventDefault()
mainWindow.hide()
})
} else {
log.info('Main window already created')
}
}
const showWindows = () => {
log.info('Show main window')
if (!mainWindow) {
createWindow()
return
}
if (!mainWindow.isVisible()) {
mainWindow.show()
}
}
const hideWindows = () => {
log.info('Hide main window')
if (!mainWindow) {
createWindow()
}
if (visibleWindows()) {
mainWindow.hide()
}
}
const closeWindows = () => {
log.info('Close main window')
if (visibleWindows()) {
mainWindow.close()
mainWindow = null
}
}
const refreshWindows = () => {
log.info('Refresh main window')
hideWindows()
if (visibleWindows()) {
mainWindow.reload()
}
}
const refreshUrl = () => {
log.info('Refresh main window')
if (visibleWindows()) {
mainWindow.loadURL(url)
}
}
const rebootWindows = () => {
log.info('Reboot main window')
hideWindows()
if (visibleWindows()) {
mainWindow.reload()
}
}
const visibleWindows = () => {
return mainWindow && mainWindow.isVisible()
}
module.exports = { hideWindows, showWindows, createWindow, closeWindows, refreshWindows, rebootWindows, visibleWindows, refreshUrl };