express.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import express from "express";
  2. import cors from "cors";
  3. import { mainWindow } from "../win/mainWin";
  4. import log from "electron-log/main";
  5. import { handeGet } from "./store";
  6. const sendNavigateMessage = (path: string) => {
  7. mainWindow!.webContents.send("navigate", {
  8. type: "navigate",
  9. path: `/?iframePath=${encodeURIComponent(path)}`, // 通过 URL 参数传递
  10. setIframeFlag: true,
  11. });
  12. };
  13. export const initExpress = () => {
  14. const app = express();
  15. app.use(express.json());
  16. app.use(express.urlencoded({ extended: false }));
  17. app.use(cors());
  18. app.get("/iframe", async (req, res) => {
  19. const { path, Path } = req.query;
  20. console.log("🚀 ~ app.get ~ req.query:", req.query);
  21. const url = path || Path;
  22. if (!url) {
  23. res.send({
  24. code: 10001,
  25. msg: "缺少path参数" + new Date(),
  26. });
  27. log.error("缺少path参数" + new Date());
  28. return;
  29. }
  30. sendNavigateMessage(path);
  31. mainWindow!.webContents.send("iframe", {
  32. type: "iframe",
  33. data: path,
  34. });
  35. // mainWindow!.webContents.loadURL(decodeURIComponent(url));
  36. const isFocusOpenWin: number = handeGet("isFocusOpenWin");
  37. if (isFocusOpenWin == 1) {
  38. mainWindow!.show();
  39. mainWindow!.setAlwaysOnTop(true);
  40. }
  41. res.send({
  42. code: 200,
  43. msg: "成功" + new Date(),
  44. });
  45. });
  46. app.get("/iframe/:path", async (req, res) => {
  47. const { path } = req.params;
  48. if (!path) {
  49. res.send({
  50. code: 10001,
  51. msg: "缺少path参数" + new Date(),
  52. });
  53. log.error("缺少path参数" + new Date());
  54. return;
  55. }
  56. sendNavigateMessage(path);
  57. mainWindow!.webContents.send("iframe", {
  58. type: "iframe",
  59. data: path,
  60. });
  61. // mainWindow!.webContents.loadURL(decodeURIComponent(path));
  62. const isFocusOpenWin: number = handeGet("isFocusOpenWin");
  63. if (isFocusOpenWin == 1) {
  64. mainWindow!.show();
  65. mainWindow!.setAlwaysOnTop(true);
  66. }
  67. res.send({
  68. code: 200,
  69. msg: "成功" + new Date(),
  70. });
  71. });
  72. app.post("/iframe", async (req, res) => {
  73. const body = req.body;
  74. console.log("🚀 ~ app.get ~ req.body:", body);
  75. const { path } = req.body;
  76. if (!path) {
  77. res.send({
  78. code: 10001,
  79. msg: "缺少path参数" + new Date(),
  80. });
  81. log.error("缺少path参数" + new Date());
  82. return;
  83. }
  84. sendNavigateMessage(path);
  85. mainWindow!.webContents.send("iframe", {
  86. type: "iframe",
  87. data: path,
  88. });
  89. // mainWindow!.webContents.loadURL(decodeURIComponent(path));
  90. const isFocusOpenWin: number = handeGet("isFocusOpenWin");
  91. if (isFocusOpenWin == 1) {
  92. mainWindow!.show();
  93. mainWindow!.setAlwaysOnTop(true);
  94. }
  95. res.send({
  96. code: 200,
  97. msg: "成功" + new Date(),
  98. });
  99. });
  100. app.listen(3003, () => console.log("Example app listening on port 3003!"));
  101. };