123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- import express from "express";
- import cors from "cors";
- import { mainWindow } from "../win/mainWin";
- import log from "electron-log/main";
- import { handeGet } from "./store";
- const sendNavigateMessage = (path: string) => {
- mainWindow!.webContents.send("navigate", {
- type: "navigate",
- path: `/?iframePath=${encodeURIComponent(path)}`, // 通过 URL 参数传递
- setIframeFlag: true,
- });
- };
- export const initExpress = () => {
- const app = express();
- app.use(express.json());
- app.use(express.urlencoded({ extended: false }));
- app.use(cors());
- app.get("/iframe", async (req, res) => {
- const { path, Path } = req.query;
- console.log("🚀 ~ app.get ~ req.query:", req.query);
- const url = path || Path;
- if (!url) {
- res.send({
- code: 10001,
- msg: "缺少path参数" + new Date(),
- });
- log.error("缺少path参数" + new Date());
- return;
- }
- sendNavigateMessage(path);
- mainWindow!.webContents.send("iframe", {
- type: "iframe",
- data: path,
- });
- // mainWindow!.webContents.loadURL(decodeURIComponent(url));
- const isFocusOpenWin: number = handeGet("isFocusOpenWin");
- if (isFocusOpenWin == 1) {
- mainWindow!.show();
- mainWindow!.setAlwaysOnTop(true);
- }
- res.send({
- code: 200,
- msg: "成功" + new Date(),
- });
- });
- app.get("/iframe/:path", async (req, res) => {
- const { path } = req.params;
- if (!path) {
- res.send({
- code: 10001,
- msg: "缺少path参数" + new Date(),
- });
- log.error("缺少path参数" + new Date());
- return;
- }
- sendNavigateMessage(path);
- mainWindow!.webContents.send("iframe", {
- type: "iframe",
- data: path,
- });
- // mainWindow!.webContents.loadURL(decodeURIComponent(path));
- const isFocusOpenWin: number = handeGet("isFocusOpenWin");
- if (isFocusOpenWin == 1) {
- mainWindow!.show();
- mainWindow!.setAlwaysOnTop(true);
- }
- res.send({
- code: 200,
- msg: "成功" + new Date(),
- });
- });
- app.post("/iframe", async (req, res) => {
- const body = req.body;
- console.log("🚀 ~ app.get ~ req.body:", body);
- const { path } = req.body;
- if (!path) {
- res.send({
- code: 10001,
- msg: "缺少path参数" + new Date(),
- });
- log.error("缺少path参数" + new Date());
- return;
- }
- sendNavigateMessage(path);
- mainWindow!.webContents.send("iframe", {
- type: "iframe",
- data: path,
- });
- // mainWindow!.webContents.loadURL(decodeURIComponent(path));
- const isFocusOpenWin: number = handeGet("isFocusOpenWin");
- if (isFocusOpenWin == 1) {
- mainWindow!.show();
- mainWindow!.setAlwaysOnTop(true);
- }
- res.send({
- code: 200,
- msg: "成功" + new Date(),
- });
- });
- app.listen(3003, () => console.log("Example app listening on port 3003!"));
- };
|