|
|
const { app, BrowserWindow } = require("electron");
|
|
|
const { ipcMain } = require("electron");
|
|
|
const path = require("path");
|
|
|
const http = require("http");
|
|
|
const httpProxy = require("http-proxy"); // 需要安装:npm install http-proxy
|
|
|
const {
|
|
|
updateConfigValue,
|
|
|
getConfigValue,
|
|
|
bulkInsertSystemMessages
|
|
|
} = require("./dbHandler");
|
|
|
|
|
|
// 创建代理服务器
|
|
|
const proxyServer = httpProxy.createProxyServer({});
|
|
|
|
|
|
// 定义目标服务
|
|
|
const proxyTargets = {
|
|
|
"/live-digital-avatar-manage":
|
|
|
"http://192.168.10.25:9909/live-digital-avatar-manage"
|
|
|
};
|
|
|
|
|
|
// 创建本地 HTTP 服务器
|
|
|
const server = http.createServer((req, res) => {
|
|
|
// 根据请求路径匹配目标服务
|
|
|
for (const pathPrefix in proxyTargets) {
|
|
|
if (req.url.startsWith(pathPrefix)) {
|
|
|
const target = proxyTargets[pathPrefix];
|
|
|
// 重写路径(移除前缀)
|
|
|
req.url = req.url.replace(pathPrefix, "");
|
|
|
// 转发请求
|
|
|
proxyServer.web(req, res, { target });
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
res.statusCode = 404;
|
|
|
res.end("Not Found");
|
|
|
});
|
|
|
// 注册IPC通信处理
|
|
|
function registerIpcHandlers() {
|
|
|
// 更新backend_token
|
|
|
ipcMain.handle("update-config", async (event, key, token) => {
|
|
|
try {
|
|
|
return await updateConfigValue(key, token);
|
|
|
} catch (error) {
|
|
|
console.error("更新backend_token失败:", error);
|
|
|
throw error;
|
|
|
}
|
|
|
});
|
|
|
|
|
|
ipcMain.handle("get-config", async (event, key) => {
|
|
|
try {
|
|
|
return await getConfigValue(key);
|
|
|
} catch (error) {
|
|
|
console.error("获取失败:", error);
|
|
|
throw error;
|
|
|
}
|
|
|
});
|
|
|
ipcMain.handle("bulk-insert-system-messages", async (event, messages) => {
|
|
|
try {
|
|
|
return await bulkInsertSystemMessages(messages);
|
|
|
} catch (error) {
|
|
|
console.error("更新失败:", error);
|
|
|
throw error;
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
// 启动服务器
|
|
|
server.listen(3000, () => {
|
|
|
console.log("Proxy server running on port 3000");
|
|
|
});
|
|
|
|
|
|
function createWindow() {
|
|
|
const win = new BrowserWindow({
|
|
|
width: 1920,
|
|
|
height: 1080,
|
|
|
webPreferences: {
|
|
|
preload: path.join(__dirname, "preload.js"), // 使用计算后的 __dirname
|
|
|
nodeIntegration: false,
|
|
|
contextIsolation: true
|
|
|
}
|
|
|
});
|
|
|
|
|
|
// 捕获 preload 加载错误
|
|
|
win.webContents.on("did-fail-load", (event, errorCode, errorDescription) => {
|
|
|
console.error("Preload 加载失败:", errorDescription);
|
|
|
});
|
|
|
win.webContents.openDevTools();
|
|
|
// 加载应用
|
|
|
if (process.env.NODE_ENV === "development") {
|
|
|
win.loadURL("http://localhost:8848/#/login");
|
|
|
win.webContents.openDevTools();
|
|
|
} else {
|
|
|
win.loadFile("dist/index.html");
|
|
|
}
|
|
|
// win.loadURL("http://localhost:8848");
|
|
|
// win.webContents.openDevTools();
|
|
|
}
|
|
|
|
|
|
app.whenReady().then(() => {
|
|
|
createWindow();
|
|
|
// 注册IPC处理
|
|
|
registerIpcHandlers();
|
|
|
app.on("activate", () => {
|
|
|
if (BrowserWindow.getAllWindows().length === 0) createWindow();
|
|
|
});
|
|
|
});
|
|
|
|
|
|
app.on("window-all-closed", () => {
|
|
|
if (process.platform !== "darwin") app.quit();
|
|
|
});
|