tray.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { Menu, Tray, app } from "electron";
  2. import path from "node:path";
  3. import { DIST, ICON, PUBLIC, readIniFile, url, writeIniFile } from "./utils";
  4. import { showMainWin, extra, mainWindow } from "../win/mainWin";
  5. import log from "electron-log/main";
  6. import { handeGet, handeSet } from "./store";
  7. import io from "socket.io-client";
  8. export const initTray = () => {
  9. const handlePath = (icon: string) => {
  10. return url ? path.join(PUBLIC!, icon) : path.join(DIST, icon);
  11. };
  12. const ICONS = {
  13. default: ICON,
  14. icon1: handlePath("./icon-1.png"),
  15. icon2: handlePath("./icon-2.png"),
  16. icon3: handlePath("./icon-3.png"),
  17. icon4: handlePath("./icon-4.png"),
  18. icon5: handlePath("./icon-5.png"),
  19. icon6: handlePath("./icon-6.png"),
  20. icon7: handlePath("./icon-7.png"),
  21. icon8: handlePath("./icon-8.png"),
  22. icon9: handlePath("./icon-9.png"),
  23. icon9plus: handlePath("./icon-9plus.png"),
  24. };
  25. let appIcon = new Tray(ICONS.default);
  26. const isFocusOpenWin: number = handeGet("isFocusOpenWin");
  27. let contextMenu = Menu.buildFromTemplate([
  28. {
  29. id: "open-win",
  30. label: `${isFocusOpenWin == 1 ? "关闭" : "开启"}主动弹窗`,
  31. click: () => {
  32. updateItem();
  33. },
  34. },
  35. {
  36. label: "打开弹窗",
  37. click: () => {
  38. showMainWin();
  39. },
  40. },
  41. {
  42. label: "退出弹窗",
  43. click: () => {
  44. extra.activeClose = true;
  45. setTimeout(() => {
  46. app.quit();
  47. });
  48. },
  49. },
  50. ]);
  51. appIcon.setToolTip("菁苗健康");
  52. appIcon.setContextMenu(contextMenu);
  53. // 服务端地址
  54. const SOCKET_URL = "http://114.215.252.134:2120";
  55. const socket = io(SOCKET_URL, {
  56. reconnection: true,
  57. reconnectionDelay: 1000,
  58. timeout: 20000,
  59. });
  60. // socket连接后以uid登录
  61. socket.on("connect", function () {
  62. console.log("Connected via CDN ES6");
  63. socket.emit("login", 123);
  64. });
  65. // 后端推送来消息时
  66. socket.on("new_msg", function (msg) {
  67. const fixedData = msg.replace(/"/g, '"');
  68. const userObj = JSON.parse(fixedData);
  69. const msgCount = userObj.count;
  70. if (msgCount <= 9) {
  71. appIcon.setImage(ICONS[`icon${msgCount}`]);
  72. } else {
  73. appIcon.setImage(ICONS[`icon9plus`]);
  74. }
  75. appIcon.setToolTip(`菁苗健康-未读消息数量(${msgCount})`);
  76. if (userObj.msg_type !== 2) {
  77. // 向渲染进程发送消息
  78. mainWindow?.webContents.send("new-notification", {
  79. ...userObj,
  80. is_read: 0,
  81. });
  82. }
  83. });
  84. appIcon.addListener("click", function () {
  85. showMainWin();
  86. });
  87. // 更新item
  88. const updateItem = async () => {
  89. let isFocusOpenWin: number = handeGet("isFocusOpenWin");
  90. const openMenuItem = contextMenu.getMenuItemById("open-win");
  91. if (isFocusOpenWin == 1) {
  92. isFocusOpenWin = 2;
  93. openMenuItem!.label = `开启主动弹窗`;
  94. } else {
  95. isFocusOpenWin = 1;
  96. openMenuItem!.label = `关闭主动弹窗`;
  97. }
  98. contextMenu = Menu.buildFromTemplate(contextMenu.items);
  99. appIcon.setContextMenu(contextMenu);
  100. handeSet("isFocusOpenWin", isFocusOpenWin);
  101. log.info("isFocusOpenWin==>", isFocusOpenWin);
  102. };
  103. return appIcon;
  104. };