express.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. export const initExpress = () => {
  7. const app = express();
  8. app.use(express.json());
  9. app.use(express.urlencoded({ extended: false }));
  10. app.use(cors());
  11. app.get("/iframe", async (req, res) => {
  12. const { path, Path } = req.query;
  13. console.log("🚀 ~ app.get ~ req.query:", req.query);
  14. const url = path || Path;
  15. if (!url) {
  16. res.send({
  17. code: 10001,
  18. msg: "缺少path参数" + new Date(),
  19. });
  20. log.error("缺少path参数" + new Date());
  21. return;
  22. }
  23. mainWindow!.webContents.loadURL(decodeURIComponent(url));
  24. const isFocusOpenWin: number = handeGet("isFocusOpenWin");
  25. if (isFocusOpenWin == 1) {
  26. mainWindow!.show();
  27. mainWindow!.setAlwaysOnTop(true);
  28. }
  29. // mainWindow!.webContents.send("iframe", {
  30. // type: "iframe",
  31. // data: path,
  32. // });
  33. res.send({
  34. code: 200,
  35. msg: "成功" + new Date(),
  36. });
  37. });
  38. app.get("/iframe/:path", async (req, res) => {
  39. const { path } = req.params;
  40. if (!path) {
  41. res.send({
  42. code: 10001,
  43. msg: "缺少path参数" + new Date(),
  44. });
  45. log.error("缺少path参数" + new Date());
  46. return;
  47. }
  48. mainWindow!.webContents.loadURL(decodeURIComponent(path));
  49. const isFocusOpenWin: number = handeGet("isFocusOpenWin");
  50. if (isFocusOpenWin == 1) {
  51. mainWindow!.show();
  52. mainWindow!.setAlwaysOnTop(true);
  53. }
  54. // mainWindow!.webContents.send("iframe", {
  55. // type: "iframe",
  56. // data: path,
  57. // });
  58. res.send({
  59. code: 200,
  60. msg: "成功" + new Date(),
  61. });
  62. });
  63. app.post("/iframe", async (req, res) => {
  64. const body = req.body;
  65. console.log("🚀 ~ app.get ~ req.body:", body);
  66. const { path } = req.body;
  67. if (!path) {
  68. res.send({
  69. code: 10001,
  70. msg: "缺少path参数" + new Date(),
  71. });
  72. log.error("缺少path参数" + new Date());
  73. return;
  74. }
  75. mainWindow!.webContents.loadURL(decodeURIComponent(path));
  76. const isFocusOpenWin: number = handeGet("isFocusOpenWin");
  77. if (isFocusOpenWin == 1) {
  78. mainWindow!.show();
  79. mainWindow!.setAlwaysOnTop(true);
  80. }
  81. // mainWindow!.webContents.send("iframe", {
  82. // type: "iframe",
  83. // data: path,
  84. // });
  85. res.send({
  86. code: 200,
  87. msg: "成功" + new Date(),
  88. });
  89. });
  90. app.listen(3003, () => console.log("Example app listening on port 3003!"));
  91. };