提交
This commit is contained in:
88
src/main/app.ts
Normal file
88
src/main/app.ts
Normal file
@ -0,0 +1,88 @@
|
||||
let net = require("net");
|
||||
const express = require("express");
|
||||
const path = require("path");
|
||||
const { ipcMain } = require('electron');
|
||||
// const bodyParser = require("body-parser");
|
||||
import { GetHomeDir } from "./config";
|
||||
|
||||
let SERVER_PORTS = 55110;
|
||||
|
||||
/**
|
||||
* 检测端口是否被占用
|
||||
* @param port
|
||||
* @param cb
|
||||
*/
|
||||
|
||||
function portIsOccupied(port, cb) {
|
||||
// 创建服务并监听该端口
|
||||
port = Number(port);
|
||||
let server = net.createServer().listen(port);
|
||||
|
||||
server.on("listening", function() {
|
||||
// 执行这块代码说明端口未被占用
|
||||
server.close(); // 关闭服务
|
||||
console.log("端口【" + port + "】 可用"); // 控制台输出信息
|
||||
SERVER_PORTS = port;
|
||||
cb(port);
|
||||
});
|
||||
|
||||
server.on("error", function(err) {
|
||||
if (err.code === "EADDRINUSE") {
|
||||
// 端口已经被使用
|
||||
console.log("端口【" + port + "】 被占用");
|
||||
portIsOccupied(++port, cb);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
let apps;
|
||||
let availablePort
|
||||
|
||||
function createApp() {
|
||||
apps = express();
|
||||
// app.use(bodyParser.json());
|
||||
// app.use(bodyParser.urlencoded({ extended: true }));
|
||||
|
||||
let prefix =
|
||||
process.env.NODE_ENV === "development"
|
||||
? ""
|
||||
: "resources/app.asar/out/renderer/";
|
||||
let static_path = ["./", "src/renderer/public"];
|
||||
let home_dir = GetHomeDir();
|
||||
static_path.forEach((p) => {
|
||||
apps.use(
|
||||
express.static(
|
||||
path.join(path.join(home_dir, prefix), p) /*{maxAge: 60 * 1000 * 5}*/
|
||||
)
|
||||
);
|
||||
});
|
||||
}
|
||||
function start() {
|
||||
createApp();
|
||||
portIsOccupied(SERVER_PORTS, (port) => {
|
||||
// avilablePort挂载全局
|
||||
// console.log(apps);
|
||||
apps.listen(port);
|
||||
availablePort = port;
|
||||
// console.log("global.sharedObject", global.avilablePort);
|
||||
global.sharedObject.availablePort = port;
|
||||
// ipcRenderer.invoke("set-global-variable", "avilablePort", avilablePort);
|
||||
});
|
||||
}
|
||||
//获取项目根目录
|
||||
// function GetHomeDir() {
|
||||
// let HOME_DIR = process.cwd()
|
||||
|
||||
// if (process.env.NODE_ENV === 'production') {
|
||||
// let arr = process.execPath.replaceAll("\\", "/").split("/")
|
||||
// arr.pop()
|
||||
// HOME_DIR = arr.join("/")
|
||||
// }
|
||||
// return HOME_DIR
|
||||
// }
|
||||
|
||||
ipcMain.handle('get-available-port', () => {
|
||||
return availablePort;
|
||||
});
|
||||
|
||||
export { apps, start };
|
||||
22
src/main/config.ts
Normal file
22
src/main/config.ts
Normal file
@ -0,0 +1,22 @@
|
||||
//获取项目根目录
|
||||
function GetHomeDir() {
|
||||
let HOME_DIR = process.cwd();
|
||||
// console.log("process.env.NODE_ENV", process.env.NODE_ENV);
|
||||
if (process.env.NODE_ENV === "production") {
|
||||
let arr = process.execPath.replaceAll("\\", "/").split("/");
|
||||
arr.pop();
|
||||
HOME_DIR = arr.join("/");
|
||||
}
|
||||
return HOME_DIR;
|
||||
}
|
||||
//获取项目根目录
|
||||
function GetAsar() {
|
||||
let HOME_DIR = ''
|
||||
if (process.env.NODE_ENV === "production") {
|
||||
let arr = process.execPath.replaceAll("\\", "/").split("/");
|
||||
arr.pop();
|
||||
HOME_DIR = arr.join("/");
|
||||
}
|
||||
return HOME_DIR;
|
||||
}
|
||||
export { GetHomeDir, GetAsar };
|
||||
@ -6,6 +6,8 @@ import { Recorder } from "../preload/recorder";
|
||||
import fs from 'fs'
|
||||
import { exec } from 'child_process'
|
||||
import dayjs from 'dayjs'
|
||||
import { GetHomeDir } from './config'
|
||||
import { start } from "./app";
|
||||
|
||||
|
||||
// 开发环境路径处理 - 确保添加正确的file协议
|
||||
@ -68,6 +70,8 @@ stopBatPath = process.platform === 'win32' ? stopBatPath.replace(/^(\w:)/, '/$1'
|
||||
function createWindow(): void {
|
||||
// Create the browser window.
|
||||
|
||||
start();
|
||||
|
||||
// 创建启动窗口
|
||||
const splashWindow = new BrowserWindow({
|
||||
width: 896,
|
||||
@ -194,21 +198,65 @@ function createWindow(): void {
|
||||
}
|
||||
});
|
||||
});
|
||||
ipcMain.on("requireGEMarkerName", (event, obj) => {
|
||||
/*
|
||||
* obj= {
|
||||
dirName: "GEMarker",
|
||||
dirName1s: "GEMarker1s"
|
||||
}
|
||||
* */
|
||||
|
||||
console.log('GetHomeDir()', GetHomeDir())
|
||||
let data = {};
|
||||
for (const objKey in obj) {
|
||||
let files = fs.readdirSync(
|
||||
path.join(
|
||||
global.__static ? global.__static : GetHomeDir() + "/src/renderer/public",
|
||||
obj[objKey]
|
||||
)
|
||||
);
|
||||
console.log(files);
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
files[i] = obj[objKey] + "/" + files[i];
|
||||
}
|
||||
data[obj[objKey]] = files;
|
||||
}
|
||||
|
||||
// let files = fs.readdirSync(path.join(global.__static ? global.__static : GetHomeDir() + "/static", dirName))
|
||||
// data[dirName] = files
|
||||
|
||||
event.sender.send("dirFiles", data);
|
||||
});
|
||||
|
||||
let _winMap = new Map();
|
||||
// 监听渲染进程创建新窗口的请求
|
||||
// @ts-ignore
|
||||
ipcMain.handle('create-new-window', async (event, params, url, option) => {
|
||||
ipcMain.handle('create-new-window', async (event, params, url, option, id) => {
|
||||
try {
|
||||
const newWindow = await new BrowserWindow(params)
|
||||
if (url) {
|
||||
await newWindow.loadURL(url)
|
||||
await newWindow.webContents.send("data", option)
|
||||
newWindow.on('closed', () => {
|
||||
_winMap.delete(id);
|
||||
// a.delete(newWindow.id)
|
||||
// closeCB(newWindow.id)
|
||||
});
|
||||
}
|
||||
return newWindow.id
|
||||
_winMap.set(id, newWindow.id);
|
||||
return _winMap
|
||||
} catch (error) {
|
||||
console.error('创建窗口失败:', error);
|
||||
throw error; // 抛出错误以便渲染进程捕获
|
||||
}
|
||||
})
|
||||
//@ts-ignore
|
||||
ipcMain.handle('show-window-by-id', async (event, id) => {
|
||||
BrowserWindow.fromId(id)?.show()
|
||||
})
|
||||
ipcMain.handle('get-_winMap',()=>{
|
||||
return _winMap
|
||||
})
|
||||
// 设置窗口标题和图标
|
||||
mainWindow.webContents.setWindowOpenHandler((details) => {
|
||||
shell.openExternal(details.url)
|
||||
@ -285,15 +333,22 @@ app.whenReady().then(() => {
|
||||
// for applications and their menu bar to stay active until the user quits
|
||||
// explicitly with Cmd + Q.
|
||||
app.on('window-all-closed', () => {
|
||||
console.log('=================================================111111111', process.platform)
|
||||
if (process.platform !== 'darwin') {
|
||||
app.quit()
|
||||
}
|
||||
})
|
||||
// 退出时注销所有快捷键
|
||||
app.on('will-quit', () => {
|
||||
globalShortcut.unregisterAll()
|
||||
console.log('222222222222222222222')
|
||||
// 关闭后台服务
|
||||
exec(stopBatPath.substring(1, 200), (error, stdout, stderr) => {
|
||||
dialog.showMessageBox({
|
||||
type: 'info',
|
||||
title: '信息1',
|
||||
message: 'process.platform',
|
||||
buttons: ['确定']
|
||||
})
|
||||
if (error) {
|
||||
console.error(`执行错误: ${error.message}`);
|
||||
return;
|
||||
@ -304,7 +359,13 @@ app.on('will-quit', () => {
|
||||
}
|
||||
console.log(`批处理输出: ${stdout}`);
|
||||
});
|
||||
globalShortcut.unregisterAll()
|
||||
})
|
||||
|
||||
console.log('=================================================')
|
||||
global.sharedObject = {
|
||||
hasService: false,
|
||||
};
|
||||
|
||||
// In this file you can include the rest of your app's specific main process
|
||||
// code. You can also put them in separate files and require them here.
|
||||
|
||||
Reference in New Issue
Block a user