express.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.send("iframe", {
  24. type: "iframe",
  25. data: path,
  26. });
  27. // mainWindow!.webContents.loadURL(decodeURIComponent(url));
  28. const isFocusOpenWin: number = handeGet("isFocusOpenWin");
  29. if (isFocusOpenWin == 1) {
  30. mainWindow!.show();
  31. mainWindow!.setAlwaysOnTop(true);
  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.send("iframe", {
  49. type: "iframe",
  50. data: path,
  51. });
  52. // mainWindow!.webContents.loadURL(decodeURIComponent(path));
  53. const isFocusOpenWin: number = handeGet("isFocusOpenWin");
  54. if (isFocusOpenWin == 1) {
  55. mainWindow!.show();
  56. mainWindow!.setAlwaysOnTop(true);
  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.send("iframe", {
  76. type: "iframe",
  77. data: path,
  78. });
  79. // mainWindow!.webContents.loadURL(decodeURIComponent(path));
  80. const isFocusOpenWin: number = handeGet("isFocusOpenWin");
  81. if (isFocusOpenWin == 1) {
  82. mainWindow!.show();
  83. mainWindow!.setAlwaysOnTop(true);
  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. };